View source
<?php
namespace Drupal\ultimate_cron;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Queue\QueueWorkerManagerInterface;
use Drupal\ultimate_cron\Entity\CronJob;
class CronJobDiscovery {
protected $moduleHandler;
protected $queueManager;
protected $configFactory;
protected $moduleExtensionList;
public function __construct(ModuleHandlerInterface $module_handler, QueueWorkerManagerInterface $queue_manager, ConfigFactoryInterface $config_factory, ModuleExtensionList $module_extension_list) {
$this->moduleHandler = $module_handler;
$this->queueManager = $queue_manager;
$this->configFactory = $config_factory;
$this->moduleExtensionList = $module_extension_list;
}
public function discoverCronJobs() {
foreach ($this
->getHooks() as $id => $info) {
$this
->ensureCronJobExists($info, $id);
}
if (!$this->configFactory
->get('ultimate_cron.settings')
->get('queue.enabled')) {
return;
}
foreach ($this->queueManager
->getDefinitions() as $id => $definition) {
if (!isset($definition['cron'])) {
continue;
}
$job_id = str_replace(':', '__', CronJobInterface::QUEUE_ID_PREFIX . $id);
if (!CronJob::load($job_id)) {
$values = [
'title' => t('Queue: @title', [
'@title' => $definition['title'],
]),
'id' => $job_id,
'module' => $definition['provider'],
'weight' => 10,
'callback' => 'ultimate_cron.queue_worker:queueCallback',
'scheduler' => [
'id' => 'simple',
'configuration' => [
'rules' => [
'* * * * *',
],
],
],
];
$job = CronJob::create($values);
$job
->save();
}
}
}
protected function ensureCronJobExists($info, $id) {
$job = NULL;
if (!CronJob::load($id)) {
$values = array(
'title' => $this
->getJobTitle($id),
'id' => $id,
'module' => $info['module'],
'callback' => $info['callback'],
);
$job = CronJob::create($values);
$job
->save();
}
}
protected function getJobTitle($id) {
$titles = array();
$titles['comment_cron'] = t('Store the maximum possible comments per thread');
$titles['dblog_cron'] = t('Remove expired log messages and flood control events');
$titles['field_cron'] = t('Purges deleted Field API data');
$titles['file_cron'] = t('Deletes temporary files');
$titles['history_cron'] = t('Deletes history');
$titles['search_cron'] = t('Updates indexable active search pages');
$titles['system_cron'] = t('Cleanup (caches, batch, flood, temp-files, etc.)');
$titles['update_cron'] = t('Update indexes');
$titles['node_cron'] = t('Updates search rankings for nodes');
$titles['aggregator_cron'] = t('Refresh feeds');
$titles['ultimate_cron_cron'] = t('Runs internal cleanup operations');
$titles['statistics_cron'] = t('Reset counts and clean up');
$titles['tracker_cron'] = t('Update tracker index');
if (isset($titles[$id])) {
return $titles[$id];
}
return t('Default cron handler');
}
protected function getHooks() {
$hooks = array();
$modules = array_keys($this->moduleHandler
->getModuleList());
foreach ($modules as $module) {
$hooks += $this
->getModuleHooks($module);
}
return $hooks;
}
protected function getModuleHooks($module) {
$items = array();
if ($this->moduleHandler
->implementsHook($module, 'cron')) {
$info = $this->moduleExtensionList
->getExtensionInfo($module);
$callback = "{$module}_cron";
$items[$callback] = array(
'module' => $module,
'title' => isset($titles[$callback]) ? $titles[$callback] : 'Default cron handler',
'configure' => empty($info['configure']) ? NULL : $info['configure'],
'callback' => $callback,
'tags' => array(),
'pass job argument' => FALSE,
);
$items["{$module}_cron"]['tags'][] = 'core';
}
return $items;
}
}