ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
ilCronJob Class Reference
+ Inheritance diagram for ilCronJob:
+ Collaboration diagram for ilCronJob:

Public Member Functions

 setDateTimeProvider (?Closure $date_time_provider)
 
 isDue (?DateTimeImmutable $last_run, ?CronJobScheduleType $schedule_type, ?int $schedule_value, bool $is_manually_executed=false)
 
 getScheduleType ()
 Get current schedule type (if flexible) More...
 
 getScheduleValue ()
 Get current schedule value (if flexible) More...
 
 setSchedule (?CronJobScheduleType $a_type, ?int $a_value)
 Update current schedule (if flexible) More...
 
 getAllScheduleTypes ()
 Get all available schedule types. More...
 
 getScheduleTypesWithValues ()
 
 getValidScheduleTypes ()
 Returns a collection of all valid schedule types for a specific job. More...
 
 isManuallyExecutable ()
 
 hasCustomSettings ()
 
 addCustomSettingsToForm (ilPropertyFormGUI $a_form)
 
 saveCustomSettings (ilPropertyFormGUI $a_form)
 
 addToExternalSettingsForm (int $a_form_id, array &$a_fields, bool $a_is_active)
 
 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 ist NOT executed. More...
 
 getId ()
 
 getTitle ()
 
 getDescription ()
 
 hasAutoActivation ()
 Is to be activated on "installation", does only work for ILIAS core cron jobs. More...
 
 hasFlexibleSchedule ()
 
 getDefaultScheduleType ()
 
 getDefaultScheduleValue ()
 
 run ()
 

Protected Attributes

CronJobScheduleType $schedule_type = null
 
int $schedule_value = null
 
Closure $date_time_provider = null
 

Private Member Functions

 checkWeeklySchedule (DateTimeImmutable $last_run, DateTimeImmutable $now)
 
 checkSchedule (?DateTimeImmutable $last_run, ?CronJobScheduleType $schedule_type, ?int $schedule_value)
 

Detailed Description

Definition at line 23 of file class.ilCronJob.php.

Member Function Documentation

◆ activationWasToggled()

ilCronJob::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 ist NOT executed.

Furthermore only few dependencies may be available in the $DIC.

Definition at line 268 of file class.ilCronJob.php.

References getDefaultScheduleType(), getDefaultScheduleValue(), getDescription(), getId(), getTitle(), hasAutoActivation(), hasFlexibleSchedule(), ILIAS\Repository\int(), and run().

Referenced by ilCronManagerImpl\activateJob(), ilCronJobRepositoryImpl\createDefaultEntry(), and ilCronManagerImpl\deactivateJob().

268  : void
269  {
270  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ addCustomSettingsToForm()

ilCronJob::addCustomSettingsToForm ( ilPropertyFormGUI  $a_form)

Definition at line 251 of file class.ilCronJob.php.

251  : void
252  {
253  }

◆ addToExternalSettingsForm()

ilCronJob::addToExternalSettingsForm ( int  $a_form_id,
array &  $a_fields,
bool  $a_is_active 
)

Definition at line 260 of file class.ilCronJob.php.

260  : void
261  {
262  }

◆ checkSchedule()

ilCronJob::checkSchedule ( ?DateTimeImmutable  $last_run,
?CronJobScheduleType  $schedule_type,
?int  $schedule_value 
)
private

Definition at line 67 of file class.ilCronJob.php.

References $date_time_provider, checkWeeklySchedule(), and ILIAS\Cron\Schedule\SCHEDULE_TYPE_YEARLY.

Referenced by isDue().

67  : bool
68  {
69  if (null === $schedule_type) {
70  return false;
71  }
72 
73  if (null === $last_run) {
74  return true;
75  }
76 
77  if ($this->date_time_provider === null) {
78  $now = new DateTimeImmutable('@' . time(), new DateTimeZone(date_default_timezone_get()));
79  } else {
80  $now = ($this->date_time_provider)();
81  }
82 
83  switch ($schedule_type) {
84  case CronJobScheduleType::SCHEDULE_TYPE_DAILY:
85  $last = $last_run->format('Y-m-d');
86  $ref = $now->format('Y-m-d');
87  return ($last !== $ref);
88 
89  case CronJobScheduleType::SCHEDULE_TYPE_WEEKLY:
90  return $this->checkWeeklySchedule($last_run, $now);
91 
92  case CronJobScheduleType::SCHEDULE_TYPE_MONTHLY:
93  $last = $last_run->format('Y-n');
94  $ref = $now->format('Y-n');
95  return ($last !== $ref);
96 
97  case CronJobScheduleType::SCHEDULE_TYPE_QUARTERLY:
98  $last = $last_run->format('Y') . '-' . ceil(((int) $last_run->format('n')) / 3);
99  $ref = $now->format('Y') . '-' . ceil(((int) $now->format('n')) / 3);
100  return ($last !== $ref);
101 
103  $last = $last_run->format('Y');
104  $ref = $now->format('Y');
105  return ($last !== $ref);
106 
107  case CronJobScheduleType::SCHEDULE_TYPE_IN_MINUTES:
108  $diff = floor(($now->getTimestamp() - $last_run->getTimestamp()) / 60);
109  return ($diff >= $schedule_value);
110 
111  case CronJobScheduleType::SCHEDULE_TYPE_IN_HOURS:
112  $diff = floor(($now->getTimestamp() - $last_run->getTimestamp()) / (60 * 60));
113  return ($diff >= $schedule_value);
114 
115  case CronJobScheduleType::SCHEDULE_TYPE_IN_DAYS:
116  $diff = floor(($now->getTimestamp() - $last_run->getTimestamp()) / (60 * 60 * 24));
117  return ($diff >= $schedule_value);
118  }
119 
120  return false;
121  }
CronJobScheduleType $schedule_type
Closure $date_time_provider
checkWeeklySchedule(DateTimeImmutable $last_run, DateTimeImmutable $now)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ checkWeeklySchedule()

ilCronJob::checkWeeklySchedule ( DateTimeImmutable  $last_run,
DateTimeImmutable  $now 
)
private

Definition at line 29 of file class.ilCronJob.php.

References ILIAS\Repository\int().

Referenced by checkSchedule().

29  : bool
30  {
31  $last_year = (int) $last_run->format('Y');
32  $now_year = (int) $now->format('Y');
33 
34  if ($last_year > $now_year) {
35  // Should never happen, don't execute otherwise
36  return false;
37  }
38 
39  $last_week = $last_run->format('W');
40  $now_week = $now->format('W');
41 
42  if ($last_week !== $now_week) {
43  // Week differs, always execute the job
44  return true;
45  }
46 
47  // For all following cases, the week number is always identical
48 
49  $last_month = (int) $last_run->format('m');
50  $now_month = (int) $now->format('m');
51 
52  $is_within_same_week_in_same_year = ($last_year . '-' . $last_week) === ($now_year . '-' . $now_week);
53  if ($is_within_same_week_in_same_year) {
54  // Same week in same year, only execute if the month differs (2022-52 is valid for January and December)
55  return $last_month !== $now_month && $now->diff($last_run)->d > 7;
56  }
57 
58  if ($now_year - $last_year > 1) {
59  // Always execute if the difference of years is greater than 1
60  return true;
61  }
62 
63  // Execute for week number 52 in 2022 (last run) and week number 52 in December of 2022 (now), but not for week number 52 in January of 2022 (now)
64  return $last_month === $now_month;
65  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getAllScheduleTypes()

ilCronJob::getAllScheduleTypes ( )

Get all available schedule types.

Returns
list<CronJobScheduleType>

Definition at line 215 of file class.ilCronJob.php.

Referenced by getValidScheduleTypes().

215  : array
216  {
217  return CronJobScheduleType::cases();
218  }
+ Here is the caller graph for this function:

◆ getDefaultScheduleType()

ilCronJob::getDefaultScheduleType ( )
abstract

Referenced by activationWasToggled(), ilCronJobRepositoryImpl\createDefaultEntry(), and isDue().

+ Here is the caller graph for this function:

◆ getDefaultScheduleValue()

ilCronJob::getDefaultScheduleValue ( )
abstract

Referenced by activationWasToggled(), ilCronJobRepositoryImpl\createDefaultEntry(), and isDue().

+ Here is the caller graph for this function:

◆ getDescription()

ilCronJob::getDescription ( )
abstract

Referenced by activationWasToggled().

+ Here is the caller graph for this function:

◆ getId()

◆ getScheduleType()

ilCronJob::getScheduleType ( )

Get current schedule type (if flexible)

Definition at line 175 of file class.ilCronJob.php.

References $schedule_type, and hasFlexibleSchedule().

Referenced by CronJobScheduleTest\testWeeklySchedules().

176  {
177  if ($this->schedule_type && $this->hasFlexibleSchedule()) {
178  return $this->schedule_type;
179  }
180 
181  return null;
182  }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
hasFlexibleSchedule()
CronJobScheduleType $schedule_type
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getScheduleTypesWithValues()

ilCronJob::getScheduleTypesWithValues ( )
Returns
list<CronJobScheduleType>

Definition at line 223 of file class.ilCronJob.php.

223  : array
224  {
225  return [
226  CronJobScheduleType::SCHEDULE_TYPE_IN_MINUTES,
227  CronJobScheduleType::SCHEDULE_TYPE_IN_HOURS,
228  CronJobScheduleType::SCHEDULE_TYPE_IN_DAYS,
229  ];
230  }

◆ getScheduleValue()

ilCronJob::getScheduleValue ( )

Get current schedule value (if flexible)

Definition at line 187 of file class.ilCronJob.php.

References $schedule_value, and hasFlexibleSchedule().

Referenced by CronJobScheduleTest\testWeeklySchedules().

187  : ?int
188  {
189  if ($this->schedule_value && $this->hasFlexibleSchedule()) {
190  return $this->schedule_value;
191  }
192 
193  return null;
194  }
hasFlexibleSchedule()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getTitle()

ilCronJob::getTitle ( )
abstract

Referenced by activationWasToggled().

+ Here is the caller graph for this function:

◆ getValidScheduleTypes()

ilCronJob::getValidScheduleTypes ( )

Returns a collection of all valid schedule types for a specific job.

Returns
list<CronJobScheduleType>

Definition at line 236 of file class.ilCronJob.php.

References getAllScheduleTypes().

Referenced by setSchedule(), and ilCronJobRepositoryImpl\updateJobSchedule().

236  : array
237  {
238  return $this->getAllScheduleTypes();
239  }
getAllScheduleTypes()
Get all available schedule types.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hasAutoActivation()

ilCronJob::hasAutoActivation ( )
abstract

Is to be activated on "installation", does only work for ILIAS core cron jobs.

Referenced by activationWasToggled(), and ilCronJobRepositoryImpl\createDefaultEntry().

+ Here is the caller graph for this function:

◆ hasCustomSettings()

ilCronJob::hasCustomSettings ( )

Definition at line 246 of file class.ilCronJob.php.

246  : bool
247  {
248  return false;
249  }

◆ hasFlexibleSchedule()

ilCronJob::hasFlexibleSchedule ( )
abstract

◆ isDue()

ilCronJob::isDue ( ?DateTimeImmutable  $last_run,
?CronJobScheduleType  $schedule_type,
?int  $schedule_value,
bool  $is_manually_executed = false 
)

Definition at line 154 of file class.ilCronJob.php.

References checkSchedule(), getDefaultScheduleType(), getDefaultScheduleValue(), and hasFlexibleSchedule().

Referenced by ilCronManagerImpl\runJob(), CronJobScheduleTest\testSchedule(), and CronJobScheduleTest\testWeeklySchedules().

159  : bool {
160  if ($is_manually_executed) {
161  return true;
162  }
163 
164  if (!$this->hasFlexibleSchedule()) {
167  }
168 
169  return $this->checkSchedule($last_run, $schedule_type, $schedule_value);
170  }
checkSchedule(?DateTimeImmutable $last_run, ?CronJobScheduleType $schedule_type, ?int $schedule_value)
hasFlexibleSchedule()
CronJobScheduleType $schedule_type
getDefaultScheduleType()
getDefaultScheduleValue()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isManuallyExecutable()

ilCronJob::isManuallyExecutable ( )

Definition at line 241 of file class.ilCronJob.php.

Referenced by ilCronManagerGUI\confirm().

241  : bool
242  {
243  return true;
244  }
+ Here is the caller graph for this function:

◆ run()

ilCronJob::run ( )
abstract

Referenced by activationWasToggled(), and ilCronManagerImpl\runJob().

+ Here is the caller graph for this function:

◆ saveCustomSettings()

ilCronJob::saveCustomSettings ( ilPropertyFormGUI  $a_form)

Definition at line 255 of file class.ilCronJob.php.

255  : bool
256  {
257  return true;
258  }

◆ setDateTimeProvider()

ilCronJob::setDateTimeProvider ( ?Closure  $date_time_provider)
Parameters
Closure():DateTimeInterface|null$date_time_provider

Definition at line 126 of file class.ilCronJob.php.

References $date_time_provider, and $r.

Referenced by ilCronManagerImpl\runJob().

126  : void
127  {
128  if ($date_time_provider !== null) {
130  $return_type = $r->getReturnType();
131  if ($return_type !== null) {
132  $return_type = $return_type->getName();
133  }
134  $expected_type = DateTimeInterface::class;
135  if (!is_subclass_of($return_type, $expected_type)) {
136  throw new InvalidArgumentException(sprintf(
137  'The return type of the datetime provider must be of type %s',
138  $expected_type
139  ));
140  }
141 
143  $parameters = $r->getParameters();
144  if ($parameters !== []) {
145  throw new InvalidArgumentException(
146  'The datetime provider must not define any parameters',
147  );
148  }
149  }
150 
151  $this->date_time_provider = $date_time_provider;
152  }
Closure $date_time_provider
$r
+ Here is the caller graph for this function:

◆ setSchedule()

ilCronJob::setSchedule ( ?CronJobScheduleType  $a_type,
?int  $a_value 
)

Update current schedule (if flexible)

Definition at line 199 of file class.ilCronJob.php.

References getValidScheduleTypes(), and hasFlexibleSchedule().

199  : void
200  {
201  if (
202  $a_value &&
203  $this->hasFlexibleSchedule() &&
204  in_array($a_type, $this->getValidScheduleTypes(), true)
205  ) {
206  $this->schedule_type = $a_type;
207  $this->schedule_value = $a_value;
208  }
209  }
getValidScheduleTypes()
Returns a collection of all valid schedule types for a specific job.
hasFlexibleSchedule()
+ Here is the call graph for this function:

Field Documentation

◆ $date_time_provider

Closure ilCronJob::$date_time_provider = null
protected

Definition at line 27 of file class.ilCronJob.php.

Referenced by checkSchedule(), and setDateTimeProvider().

◆ $schedule_type

CronJobScheduleType ilCronJob::$schedule_type = null
protected

Definition at line 25 of file class.ilCronJob.php.

Referenced by getScheduleType().

◆ $schedule_value

int ilCronJob::$schedule_value = null
protected

Definition at line 26 of file class.ilCronJob.php.

Referenced by getScheduleValue().


The documentation for this class was generated from the following file: