136 lines
3.8 KiB
PHP
136 lines
3.8 KiB
PHP
<?php
|
|
|
|
class stTaskScheluder
|
|
{
|
|
/**
|
|
* Lista zadań
|
|
*
|
|
* @var stTaskInterface[]
|
|
*/
|
|
protected static $tasks = null;
|
|
|
|
/**
|
|
* Tworzy listę zadania do wykonania na podstawie konfiguracji
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function initialize()
|
|
{
|
|
$tasks = stTaskConfiguration::getTasks();
|
|
|
|
$integrity_hash = md5(serialize($tasks));
|
|
|
|
$config = stConfig::getInstance('stTaskScheluderBackend');
|
|
|
|
if ($config->get('integrity_hash') != $integrity_hash)
|
|
{
|
|
$tasks = self::getTasks();
|
|
|
|
stEventDispatcher::getInstance()->notify(new sfEvent(null, 'stTaskScheluder.initialize', array('tasks' => $tasks)));
|
|
|
|
$taskIds = array_keys($tasks);
|
|
|
|
$c = new Criteria();
|
|
$c->add(TaskPeer::TASK_ID, $taskIds, Criteria::NOT_IN);
|
|
|
|
if (TaskPeer::doDelete($c))
|
|
{
|
|
$fc = stFunctionCache::getInstance('stTaskScheluder');
|
|
$fc->removeAll();
|
|
}
|
|
|
|
$config->set('integrity_hash', $integrity_hash);
|
|
$config->save();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Zwraca listę zadań do wykonania
|
|
*
|
|
* @return stTaskInterface[]
|
|
*/
|
|
public static function getTasks(): array
|
|
{
|
|
if (null === self::$tasks)
|
|
{
|
|
$tasksConfiguration = stTaskConfiguration::getTasks();
|
|
|
|
$tasks = array();
|
|
|
|
foreach ($tasksConfiguration as $taskId => $taskConfiguration)
|
|
{
|
|
$tasks[$taskId] = stTaskFactory::createTaskById($taskId);
|
|
}
|
|
|
|
self::$tasks = $tasks;
|
|
}
|
|
|
|
return self::$tasks;
|
|
}
|
|
|
|
/**
|
|
* Zwraca zadanie do wykonania o podanym ID
|
|
*
|
|
* @param string $taskId Id zadania
|
|
* @return stTaskInterface
|
|
*/
|
|
public static function getTask(string $taskId): stTaskInterface
|
|
{
|
|
$tasksConfiguration = stTaskConfiguration::getTasks();
|
|
|
|
if (!isset($tasksConfiguration[$taskId]))
|
|
{
|
|
throw new stTaskScheluderException(sprintf('The task with id "%s" does not exist', $taskId));
|
|
}
|
|
|
|
return stTaskFactory::createTaskById($taskId);
|
|
}
|
|
|
|
/**
|
|
* Wymusza ponowną initializacje zadań
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function forceTaskInitialization()
|
|
{
|
|
$config = stConfig::getInstance('stTaskScheluderBackend');
|
|
$config->set('integrity_hash', null);
|
|
$config->save();
|
|
}
|
|
|
|
/**
|
|
* Zwraca błąd jaki wystąpił podczas uruchomienia
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function getRuntimeError()
|
|
{
|
|
$config = stConfig::getInstance('stTaskScheluderBackend');
|
|
|
|
$error = $config->get('backend_cron_error_message');
|
|
|
|
if (!$error && !$config->get('backend_last_execute_timestamp') || time() - $config->get('backend_last_execute_timestamp') >= stTaskConfiguration::TIME_INTERVAL_10MIN + 10)
|
|
{
|
|
$error = array(
|
|
'message' => 'Zadanie cron nie zostało poprawnie skonfigurowane. Zadanie cron musi być uruchamiane co %interval% minut',
|
|
'params' => array('%interval%' => 10),
|
|
);
|
|
}
|
|
|
|
// $frontendError = $config->get('frontend_cron_error_message');
|
|
|
|
// if (!$frontendError && !$config->get('frontend_last_execute_timestamp') || time() - $config->get('frontend_last_execute_timestamp') >= stTaskConfiguration::TIME_INTERVAL_10MIN + 10)
|
|
// {
|
|
// $error = array(
|
|
// 'message' => 'Zadanie cron nie zostało poprawnie skonfigurowane. Zadanie cron musi być uruchamiane co %interval% minut',
|
|
// 'params' => array('%interval%' => 10),
|
|
// );
|
|
// }
|
|
// elseif ($frontendError)
|
|
// {
|
|
// $error = $frontendError;
|
|
// }
|
|
|
|
return $error;
|
|
}
|
|
} |