163 lines
4.2 KiB
PHP
163 lines
4.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Subclass for representing a row from the 'st_task_scheluder_task' table.
|
|
*
|
|
*
|
|
*
|
|
* @package plugins.stTaskPlugin.lib.model
|
|
*/
|
|
class Task extends BaseTask
|
|
{
|
|
public function __toString()
|
|
{
|
|
return $this->getName();
|
|
}
|
|
|
|
public function isAdminGeneratorActionVisible($name)
|
|
{
|
|
$visible = true;
|
|
|
|
if ($name == 'execute' && null !== $this->getTaskConfigurationParameter('parent'))
|
|
{
|
|
$visible = false;
|
|
}
|
|
|
|
$event = stEventDispatcher::getInstance()->filter(new sfEvent($this, 'Task.isAdminGeneratorActionVisible', ['name' => $name]), $visible);
|
|
|
|
return null !== $event->getReturnValue() ? $event->getReturnValue() : $visible;
|
|
}
|
|
|
|
/**
|
|
* Sprawdz czy zadanie jest gotowe do wykonania
|
|
*
|
|
* @param bool $checkNextExecuteDate
|
|
* @return bool
|
|
*/
|
|
public function isReadyToExecute($checkNextExecuteDate = true)
|
|
{
|
|
$ready = $this->getIsActive() && $this->status == stTask::STATUS_PENDING && (!$checkNextExecuteDate || null === $this->getLastExecutedAt() || strtotime($this->getNextExecuteDate()) <= time());
|
|
|
|
return $ready || $this->getIsActive() && $this->status != stTask::STATUS_PENDING && time() - strtotime($this->getLastActiveAt()) >= stTaskConfiguration::TIME_INTERVAL_10MIN;
|
|
}
|
|
|
|
public function getStatus()
|
|
{
|
|
return $this->isReadyToExecute() ? stTask::STATUS_PENDING : $this->status;
|
|
}
|
|
|
|
public function doFinish()
|
|
{
|
|
$this->setLastFinishedAt(time());
|
|
$this->setStatus(stTask::STATUS_PENDING);
|
|
$this->save();
|
|
}
|
|
|
|
public function doStart()
|
|
{
|
|
$this->setLastExecutedAt(time());
|
|
$this->setStatus(stTask::STATUS_RUNNING);
|
|
$this->save();
|
|
}
|
|
|
|
public function getStatusLabel()
|
|
{
|
|
$statuses = TaskPeer::getStatuses();
|
|
|
|
return $statuses[$this->getStatus()];
|
|
}
|
|
|
|
public function getName()
|
|
{
|
|
$name = $this->getTaskConfigurationParameter('name');
|
|
$i18n = sfContext::getInstance()->getI18N();
|
|
$className = $this->getClassName();
|
|
|
|
return $i18n->__($name, null, $className);
|
|
}
|
|
|
|
public function getClassName()
|
|
{
|
|
return $this->getTaskConfigurationParameter('class_name');
|
|
}
|
|
|
|
public function getNextExecuteDate()
|
|
{
|
|
if (null === $this->last_executed_at)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
$executeDate = date('d-m-Y H:i:s', $this->last_executed_at + $this->getTimeInterval());
|
|
|
|
if ($this->getTimeInterval() >= stTaskConfiguration::TIME_INTERVAL_1DAY)
|
|
{
|
|
$executeDate = date('d-m-Y', strtotime($executeDate)) . ' ' . $this->getExecuteAt();
|
|
}
|
|
|
|
return $executeDate;
|
|
}
|
|
|
|
public function getTimeInterval()
|
|
{
|
|
$timeInterval = parent::getTimeInterval();
|
|
|
|
if (null === $timeInterval)
|
|
{
|
|
$timeInterval = $this->getDefaultTimeInterval();
|
|
}
|
|
|
|
return $timeInterval;
|
|
}
|
|
|
|
public function getDefaultTimeInterval()
|
|
{
|
|
return $this->getTaskConfigurationParameter('time_interval');
|
|
}
|
|
|
|
public function getExecuteAt($format = 'H:i:s')
|
|
{
|
|
$executeAt = parent::getExecuteAt($format);
|
|
|
|
if (null === $executeAt)
|
|
{
|
|
$executeAt = $this->getTaskConfigurationParameter('execute_at');
|
|
}
|
|
|
|
return $executeAt;
|
|
}
|
|
|
|
public function save($con = null)
|
|
{
|
|
if ($this->isNew())
|
|
{
|
|
$this->setTaskPriority($this->getTaskConfigurationParameter('priority'));
|
|
$this->setIsActive($this->getTaskConfigurationParameter('is_active'));
|
|
}
|
|
|
|
$cacheClear = $this->isModified();
|
|
|
|
$result = parent::save($con);
|
|
|
|
if ($cacheClear)
|
|
{
|
|
$fc = stFunctionCache::getInstance('stTaskScheluder');
|
|
$fc->removeById($this->task_id);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function getIsSystemDefault()
|
|
{
|
|
return $this->getTaskConfigurationParameter('is_system');
|
|
}
|
|
|
|
public function getTaskConfigurationParameter($name)
|
|
{
|
|
$config = stTaskConfiguration::getTask($this->getTaskId());
|
|
|
|
return isset($config[$name]) ? $config[$name] : null;
|
|
}
|
|
}
|