19 declare(strict_types=1);
37 return ((
int) $this->clock_factory->system()->now()->format(
'Uu')) / 1000000;
42 $this->
logger->info(
'CRON - batch start');
44 $ts = $this->clock_factory->system()->now()->getTimestamp();
45 $this->
settings->set(
'last_cronjob_start_ts', (
string) $ts);
49 $this->
logger->info(sprintf(
50 'Set last datetime to: %s',
53 $this->
logger->info(sprintf(
54 'Verification of last run datetime (read from database): %s',
62 if (!defined(
'ILIAS_HTTP_PATH')) {
67 foreach ($this->cronRepository->getCronJobData(null,
false) as $row) {
68 $job = $this->cronRepository->getJobInstanceById($row[
'job_id']);
71 $this->
runJob($job, $actor);
76 foreach ($this->cronRepository->getPluginJobs(
true) as $item) {
78 $this->
runJob($item[0], $actor);
81 $this->
logger->info(
'CRON - batch end');
88 $this->
logger->info(
'CRON - manual start (' . $jobId .
')');
90 $job = $this->cronRepository->getJobInstanceById($jobId);
92 if ($job->isManuallyExecutable()) {
93 $result = $this->
runJob($job, $actor, null,
true);
95 $this->
logger->info(
'CRON - job ' . $jobId .
' is not intended to be executed manually');
98 $this->
logger->info(
'CRON - job ' . $jobId .
' seems invalid or is inactive');
101 $this->
logger->info(
'CRON - manual end (' . $jobId .
')');
114 if (null === $jobData) {
116 $jobsData = $this->cronRepository->getCronJobData($job->
getId());
117 $jobData = array_pop($jobsData);
121 return $this->clock_factory->system()->now();
125 if ($jobData[
'alive_ts']) {
126 $this->
logger->info(
'CRON - job ' . $jobData[
'job_id'] .
' still running');
131 if ($this->clock_factory->system()->now()->getTimestamp() - ((
int) $jobData[
'alive_ts']) > $cut) {
132 $this->cronRepository->updateRunInformation($jobData[
'job_id'], 0, 0);
138 $result->setMessage(
'Cron job deactivated because it has been inactive for 3 hours');
140 $this->cronRepository->updateJobResult(
142 $this->clock_factory->system()->now(),
148 $this->
logger->info(
'CRON - job ' . $jobData[
'job_id'] .
' deactivated (assumed crash)');
153 '@' . $jobData[
'job_result_ts']
154 ))->setTimezone($this->clock_factory->system()->now()->getTimezone()) : null,
155 is_numeric($jobData[
'schedule_type']) ? CronJobScheduleType::tryFrom((
int) $jobData[
'schedule_type']) : null,
156 $jobData[
'schedule_value'] ? (
int) $jobData[
'schedule_value'] : null,
159 $this->
logger->info(
'CRON - job ' . $jobData[
'job_id'] .
' started');
161 $this->cronRepository->updateRunInformation(
163 $this->clock_factory->system()->now()->getTimestamp(),
164 $this->clock_factory->system()->now()->getTimestamp()
169 $result = $job->
run();
174 ilStr::subStr(sprintf(
'Exception: %s / %s', $e->getMessage(), $e->getTraceAsString()), 0, 400)
177 $this->
logger->error($e->getMessage());
178 $this->
logger->error($e->getTraceAsString());
185 $this->
logger->info(
'CRON - job ' . $jobData[
'job_id'] .
' invalid configuration');
191 $result->setDuration($ts_dur);
193 $this->cronRepository->updateJobResult(
195 $this->clock_factory->system()->now(),
200 $this->cronRepository->updateRunInformation($jobData[
'job_id'], 0, 0);
202 $this->
logger->info(
'CRON - job ' . $jobData[
'job_id'] .
' finished');
204 $this->
logger->info(
'CRON - job ' . $jobData[
'job_id'] .
' returned status inactive');
215 $result->setMessage(
'Cron job re-activated by admin');
217 $this->cronRepository->updateJobResult(
219 $this->clock_factory->system()->now(),
224 $this->cronRepository->resetJob($job);
231 $this->cronRepository->activateJob($job, $this->clock_factory->system()->now(), $actor, $wasManuallyExecuted);
237 $this->cronRepository->deactivateJob($job, $this->clock_factory->system()->now(), $actor, $wasManuallyExecuted);
243 $jobs_data = $this->cronRepository->getCronJobData($jobId);
245 return $jobs_data !== [] && $jobs_data[0][
'job_status'];
250 $jobs_data = $this->cronRepository->getCronJobData($jobId);
252 return $jobs_data !== [] && !((bool) $jobs_data[0][
'job_status']);
255 public function ping(
string $jobId): void
257 $this->db->manipulateF(
258 'UPDATE cron_job SET alive_ts = %s WHERE job_id = %s',
260 [$this->clock_factory->system()->now()->getTimestamp(), $jobId]
static array static setUseRelativeDates(bool $a_status)
set use relative dates
resetJob(ilCronJob $job, ilObjUser $actor)
activationWasToggled(ilDBInterface $db, ilSetting $setting, bool $a_currently_active)
Important: This method is (also) called from the setup process, where the constructor of an ilCronJob...
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ilObjUser $user=null,)
runActiveJobs(ilObjUser $actor)
setDateTimeProvider(?Closure $date_time_provider)
isDue(?DateTimeImmutable $last_run, ?CronJobScheduleType $schedule_type, ?int $schedule_value, bool $is_manually_executed=false)
final const CODE_MANUAL_RESET
static subStr(string $a_str, int $a_start, ?int $a_length=null)
isJobInactive(string $jobId)
final const STATUS_INVALID_CONFIGURATION
activateJob(ilCronJob $job, ilObjUser $actor, bool $wasManuallyExecuted=false)
static useRelativeDates()
static _lookupValue(string $a_module, string $a_keyword)
deactivateJob(ilCronJob $job, ilObjUser $actor, bool $wasManuallyExecuted=false)
final const STATUS_CRASHED
isJobActive(string $jobId)
runJob(ilCronJob $job, ilObjUser $actor, ?array $jobData=null, bool $isManualExecution=false)
Run single cron job (internal)
final const CODE_SUPPOSED_CRASH
runJobManual(string $jobId, ilObjUser $actor)
__construct(private readonly ilCronJobRepository $cronRepository, private readonly ilDBInterface $db, private readonly ilSetting $settings, private readonly ilLogger $logger, private readonly ClockFactory $clock_factory)