View source
<?php
namespace Drupal\ultimate_cron\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\Core\Utility\Error;
use Drupal\ultimate_cron\CronJobInterface;
class CronJob extends ConfigEntityBase implements CronJobInterface {
public static $signals;
public static $currentJob;
public $progressUpdated = 0;
public $settings;
protected $id;
protected $uuid;
protected $status = TRUE;
protected $weight = 0;
protected $title;
protected $callback;
protected $module;
protected $scheduler = array(
'id' => 'simple',
);
protected $launcher = array(
'id' => 'serial',
);
protected $logger = array(
'id' => 'database',
);
protected $plugins = [];
protected $classResolver;
protected $moduleExtensionList;
public function __construct(array $values, $entity_type) {
parent::__construct($values, $entity_type);
$this->classResolver = \Drupal::service('class_resolver');
$this->moduleExtensionList = \Drupal::service('extension.list.module');
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if ($update && empty($this->dont_log)) {
$log = $this
->startLog(uniqid($this
->id(), TRUE), '', ULTIMATE_CRON_LOG_TYPE_ADMIN);
$log
->log('Job modified by ' . $log
->formatUser(), array(), RfcLogLevel::INFO);
$log
->finish();
}
}
public function setConfiguration($plugin_type, array $configuration) {
$this->{$plugin_type}['configuration'] = $configuration;
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
foreach ($entities as $entity) {
if (empty($entity->dont_log)) {
$log = $entity
->startLog(uniqid($entity
->id(), TRUE), 'modification', ULTIMATE_CRON_LOG_TYPE_ADMIN);
$log
->log('Job deleted by ' . $log
->formatUser(), array(), RfcLogLevel::INFO);
$log
->finish();
}
}
}
public function isValid() {
return is_callable($this
->getCallback());
}
public function peekSignal($signal) {
if (isset(self::$signals[$this
->id()][$signal])) {
return TRUE;
}
$signal = \Drupal::service('ultimate_cron.signal');
return $signal
->peek($this
->id(), $signal);
}
public function getSignal($signal) {
if (isset(self::$signals[$this
->id()][$signal])) {
unset(self::$signals[$this
->id()][$signal]);
return TRUE;
}
$service = \Drupal::service('ultimate_cron.signal');
return $service
->get($this
->id(), $signal);
}
public function sendSignal($signal, $persist = FALSE) {
if ($persist) {
$signal = \Drupal::service('ultimate_cron.signal');
$signal
->set($this
->id(), $signal);
}
else {
self::$signals[$this
->id()][$signal] = TRUE;
}
}
public function clearSignal($signal) {
unset(self::$signals[$this
->id()][$signal]);
$signal = \Drupal::service('ultimate_cron.signal');
$signal
->clear($this
->id(), $signal);
}
public function clearSignals() {
unset(self::$signals[$this
->id()]);
$signal = \Drupal::service('ultimate_cron.signal');
$signal
->flush($this
->id());
}
public function getPlugin($plugin_type, $name = NULL) {
if ($name) {
return ultimate_cron_plugin_load($plugin_type, $name);
}
if ($name) {
}
elseif (!empty($this->{$plugin_type}['id'])) {
$name = $this->{$plugin_type}['id'];
}
else {
$name = $this->hook[$plugin_type]['name'];
}
$manager = \Drupal::service('plugin.manager.ultimate_cron.' . $plugin_type);
$this->plugins[$plugin_type] = $manager
->createInstance($name, isset($this->{$plugin_type}['configuration']) ? $this->{$plugin_type}['configuration'] : array());
return $this->plugins[$plugin_type];
}
public function getConfiguration($plugin_type) {
if (!isset($this->{$plugin_type}['configuration'])) {
$this->{$plugin_type}['configuration'] = $this
->getPlugin($plugin_type)
->defaultConfiguration();
}
return $this->{$plugin_type}['configuration'];
}
public function signal($item, $plugin_type, $plugin_name, $signal) {
$plugin = ultimate_cron_plugin_load($plugin_type, $plugin_name);
return $plugin
->signal($item, $signal);
}
protected function invokeCallback() {
$callback = $this
->getCallback();
return call_user_func($callback, $this);
}
protected function resolveCallback($callback) {
$count = substr_count($callback, ':');
if ($count == 1) {
list($class_or_service, $method) = explode(':', $callback, 2);
}
elseif (strpos($callback, '::') !== FALSE) {
list($class_or_service, $method) = explode('::', $callback, 2);
}
else {
return $callback;
}
$callback = $this->classResolver
->getInstanceFromDefinition($class_or_service);
return array(
$callback,
$method,
);
}
public function isScheduled() {
\Drupal::moduleHandler()
->invokeAll('cron_pre_schedule', array(
$this,
));
$result = $this
->status() && !$this
->isLocked() && $this
->getPlugin('scheduler')
->isScheduled($this);
\Drupal::moduleHandler()
->invokeAll('cron_post_schedule', array(
$this,
));
return $result;
}
public function isBehindSchedule() {
return $this
->getPlugin('scheduler')
->isBehind($this);
}
public function lock() {
$launcher = $this
->getPlugin('launcher');
$lock_id = $launcher
->lock($this);
if (!$lock_id) {
\Drupal::logger('ultimate_cron')
->error('Could not get lock for job @name', array(
'@name' => $this
->id(),
));
return FALSE;
}
$this
->sendMessage('lock', array(
'lock_id' => $lock_id,
));
return $lock_id;
}
public function unlock($lock_id = NULL, $manual = FALSE) {
$result = NULL;
if (!$lock_id) {
$lock_id = $this
->isLocked();
}
if ($lock_id) {
$result = $this
->getPlugin('launcher')
->unlock($lock_id, $manual);
}
$this
->sendMessage('unlock', array(
'lock_id' => $lock_id,
));
return $result;
}
public function isLocked() {
return $this
->getPlugin('launcher')
->isLocked($this);
}
public static function isLockedMultiple($jobs) {
$launchers = array();
foreach ($jobs as $job) {
$launchers[$job
->getPlugin('launcher')->name][$job
->id()] = $job;
}
$locked = array();
foreach ($launchers as $launcher => $jobs) {
$locked += ultimate_cron_plugin_load('launcher', $launcher)
->isLockedMultiple($jobs);
}
return $locked;
}
public function run($init_message = NULL) {
if (!$init_message) {
$init_message = t('Launched manually');
}
$lock_id = $this
->lock();
if (!$lock_id) {
return FALSE;
}
$log_entry = $this
->startLog($lock_id, $init_message);
$accountSwitcher = \Drupal::service('account_switcher');
try {
$this
->clearSignals();
$this
->initializeProgress();
\Drupal::moduleHandler()
->invokeAll('cron_pre_run', array(
$this,
));
$accountSwitcher
->switchTo(new AnonymousUserSession());
self::$currentJob = $this;
$this
->invokeCallback();
} catch (\Error $e) {
$variables = Error::decodeException($e);
unset($variables['backtrace']);
$log_entry
->log('%type: @message in %function (line %line of %file).', $variables, RfcLogLevel::ERROR);
return FALSE;
} catch (\Exception $e) {
$variables = Error::decodeException($e);
unset($variables['backtrace']);
$log_entry
->log('%type: @message in %function (line %line of %file).', $variables, RfcLogLevel::ERROR);
return FALSE;
} finally {
self::$currentJob = NULL;
\Drupal::moduleHandler()
->invokeAll('cron_post_run', array(
$this,
));
$this
->finishProgress();
$accountSwitcher
->switchBack();
$log_entry
->finish();
$this
->unlock($lock_id);
}
return TRUE;
}
public function getLogEntries($log_types = ULTIMATE_CRON_LOG_TYPE_ALL, $limit = 10) {
$log_types = $log_types == ULTIMATE_CRON_LOG_TYPE_ALL ? _ultimate_cron_define_log_type_all() : $log_types;
return $this
->getPlugin('logger')
->getLogEntries($this
->id(), $log_types, $limit);
}
public function loadLogEntry($lock_id) {
return $this
->getPlugin('logger')
->load($this
->id(), $lock_id);
}
public function loadLatestLogEntry($log_types = array(
ULTIMATE_CRON_LOG_TYPE_NORMAL,
)) {
return $this
->getPlugin('logger')
->load($this
->id(), NULL, $log_types);
}
public static function loadLatestLogEntries($jobs, $log_types = array(
ULTIMATE_CRON_LOG_TYPE_NORMAL,
)) {
$loggers = array();
foreach ($jobs as $job) {
$loggers[$job
->getPlugin('logger')->name][$job
->id()] = $job;
}
$log_entries = array();
foreach ($loggers as $logger => $jobs) {
$log_entries += ultimate_cron_plugin_load('logger', $logger)
->loadLatestLogEntries($jobs, $log_types);
}
return $log_entries;
}
public function startLog($lock_id, $init_message = '', $log_type = ULTIMATE_CRON_LOG_TYPE_NORMAL) {
$logger = $this
->getPlugin('logger');
$log_entry = $logger
->createEntry($this
->id(), $lock_id, $init_message, $log_type);
\Drupal::service('logger.ultimate_cron')
->catchMessages($log_entry);
return $log_entry;
}
public function resumeLog($lock_id) {
$logger = $this
->getPlugin('logger');
$log_entry = $logger
->load($this
->id(), $lock_id);
$log_entry->finished = FALSE;
\Drupal::service('logger.ultimate_cron')
->catchMessages($log_entry);
return $log_entry;
}
public function getModuleName() {
static $names = array();
if (!isset($names[$this->module])) {
$info = $this->moduleExtensionList
->getExtensionInfo($this->module);
$names[$this->module] = $info && !empty($info['name']) ? $info['name'] : $this->module;
}
return $names[$this->module];
}
public function getModuleDescription() {
static $descs = array();
if (!isset($descs[$this->module])) {
$info = $this->moduleExtensionList
->getExtensionInfo($this->module);
$descs[$this->module] = $info && !empty($info['description']) ? $info['description'] : '';
}
return $descs[$this->module];
}
public function initializeProgress() {
return $this
->getPlugin('launcher')
->initializeProgress($this);
}
public function finishProgress() {
return $this
->getPlugin('launcher')
->finishProgress($this);
}
public function getProgress() {
return $this
->getPlugin('launcher')
->getProgress($this);
}
public static function getProgressMultiple($jobs) {
$launchers = array();
foreach ($jobs as $job) {
$launchers[$job
->getPlugin('launcher')->name][$job
->id()] = $job;
}
$progresses = array();
foreach ($launchers as $launcher => $jobs) {
$progresses += ultimate_cron_plugin_load('launcher', $launcher)
->getProgressMultiple($jobs);
}
return $progresses;
}
public function setProgress($progress) {
if ($this
->getPlugin('launcher')
->setProgress($this, $progress)) {
$this
->sendMessage('setProgress', array(
'progress' => $progress,
));
return TRUE;
}
return FALSE;
}
public function formatProgress($progress = NULL) {
if (!isset($progress)) {
$progress = isset($this->progress) ? $this->progress : $this
->getProgress();
}
return $this
->getPlugin('launcher')
->formatProgress($this, $progress);
}
public function getUniqueID() {
return isset($this->ids[$this
->id()]) ? $this->ids[$this
->id()] : ($this->ids[$this
->id()] = hexdec(substr(sha1($this
->id()), -8)));
}
public function sendMessage($action, $data = array()) {
if (FALSE && \Drupal::moduleHandler()
->moduleExists('nodejs')) {
$settings = ultimate_cron_plugin_load('settings', 'general')
->getDefaultSettings();
if (empty($settings['nodejs'])) {
return;
}
$elements = array();
$build = clone $this;
$cell_idxs = array();
switch ($action) {
case 'lock':
$logger = $build
->getPlugin('logger');
if (empty($data['log_entry'])) {
$build->lock_id = $data['lock_id'];
$build->log_entry = $logger
->factoryLogEntry($build->name);
$build->log_entry
->setData(array(
'lid' => $data['lock_id'],
'start_time' => microtime(TRUE),
));
}
else {
$build->log_entry = $data['log_entry'];
}
$cell_idxs = array(
'tr#' . $build->name . ' .ctools-export-ui-start-time' => 3,
'tr#' . $build->name . ' .ctools-export-ui-duration' => 4,
'tr#' . $build->name . ' .ctools-export-ui-status' => 5,
'tr#' . $build->name . ' .ctools-export-ui-operations' => 7,
);
break;
case 'unlock':
$build->log_entry = $build
->loadLogEntry($data['lock_id']);
$build->lock_id = FALSE;
$cell_idxs = array(
'tr#' . $build->name . ' .ctools-export-ui-start-time' => 3,
'tr#' . $build->name . ' .ctools-export-ui-duration' => 4,
'tr#' . $build->name . ' .ctools-export-ui-status' => 5,
'tr#' . $build->name . ' .ctools-export-ui-operations' => 7,
);
break;
case 'setProgress':
$build->lock_id = $build
->isLocked();
$build->log_entry = $build
->loadLogEntry($build->lock_id);
$cell_idxs = array(
'tr#' . $build->name . ' .ctools-export-ui-start-time' => 3,
'tr#' . $build->name . ' .ctools-export-ui-duration' => 4,
'tr#' . $build->name . ' .ctools-export-ui-status' => 5,
);
break;
}
$cells = $build
->rebuild_ctools_export_ui_table_row();
foreach ($cell_idxs as $selector => $cell_idx) {
$elements[$selector] = $cells[$cell_idx];
}
$message = (object) array(
'channel' => 'ultimate_cron',
'data' => (object) array(
'action' => $action,
'job' => $build,
'timestamp' => microtime(TRUE),
'elements' => $elements,
),
'callback' => 'nodejsUltimateCron',
);
nodejs_send_content_channel_message($message);
}
}
public function calculateDependencies() {
parent::calculateDependencies();
$this
->addDependency('module', $this
->getModule());
return $this->dependencies;
}
public function getTitle() {
return $this->title;
}
public function getCallback() {
if (is_callable($this->callback)) {
return $this->callback;
}
else {
return $this
->resolveCallback($this->callback);
}
}
public function getModule() {
return $this->module;
}
public function getSchedulerId() {
return $this->scheduler;
}
public function getLauncherId() {
return $this->launcher['id'];
}
public function getLoggerId() {
return $this->logger['id'];
}
public function setTitle($title) {
$this
->set('title', $title);
return $this;
}
public function setCallback($callback) {
$this
->set('callback', $callback);
return $this;
}
public function setModule($module) {
$this
->set('module', $module);
return $this;
}
public function setSchedulerId($scheduler_id) {
$this->scheduler['id'] = $scheduler_id;
return $this;
}
public function setLauncherId($launcher_id) {
$this->launcher['id'] = $launcher_id;
return $this;
}
public function setLoggerId($logger_id) {
$this->logger['id'] = $logger_id;
return $this;
}
}