ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ilCronJob Class Reference
+ Inheritance diagram for ilCronJob:
+ Collaboration diagram for ilCronJob:

Public Member Functions

 setDateTimeProvider (?Closure $date_time_provider)
 
 isDue (?DateTimeImmutable $last_run, ?int $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 (?int $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 ()
 

Data Fields

const SCHEDULE_TYPE_DAILY = 1
 This will be replaced with an ENUM in ILIAS 9 More...
 
const SCHEDULE_TYPE_IN_MINUTES = 2
 This will be replaced with an ENUM in ILIAS 9 More...
 
const SCHEDULE_TYPE_IN_HOURS = 3
 This will be replaced with an ENUM in ILIAS 9 More...
 
const SCHEDULE_TYPE_IN_DAYS = 4
 This will be replaced with an ENUM in ILIAS 9 More...
 
const SCHEDULE_TYPE_WEEKLY = 5
 This will be replaced with an ENUM in ILIAS 9 More...
 
const SCHEDULE_TYPE_MONTHLY = 6
 This will be replaced with an ENUM in ILIAS 9 More...
 
const SCHEDULE_TYPE_QUARTERLY = 7
 This will be replaced with an ENUM in ILIAS 9 More...
 
const SCHEDULE_TYPE_YEARLY = 8
 This will be replaced with an ENUM in ILIAS 9 More...
 

Protected Attributes

int $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, ?int $schedule_type, ?int $schedule_value)
 

Detailed Description

Definition at line 21 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.

Parameters
ilDBInterface$db
ilSetting$setting
bool$a_currently_active
Returns
void

Definition at line 300 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().

300  : void
301  {
302  }
+ 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 279 of file class.ilCronJob.php.

279  : void
280  {
281  }

◆ addToExternalSettingsForm()

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

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

288  : void
289  {
290  }

◆ checkSchedule()

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

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

References $date_time_provider, and checkWeeklySchedule().

Referenced by isDue().

82  : bool
83  {
84  if (null === $schedule_type) {
85  return false;
86  }
87 
88  if (null === $last_run) {
89  return true;
90  }
91 
92  if ($this->date_time_provider === null) {
93  $now = new DateTimeImmutable('@' . time(), new DateTimeZone(date_default_timezone_get()));
94  } else {
95  $now = ($this->date_time_provider)();
96  }
97 
98  switch ($schedule_type) {
99  case self::SCHEDULE_TYPE_DAILY:
100  $last = $last_run->format('Y-m-d');
101  $ref = $now->format('Y-m-d');
102  return ($last !== $ref);
103 
104  case self::SCHEDULE_TYPE_WEEKLY:
105  return $this->checkWeeklySchedule($last_run, $now);
106 
107  case self::SCHEDULE_TYPE_MONTHLY:
108  $last = $last_run->format('Y-n');
109  $ref = $now->format('Y-n');
110  return ($last !== $ref);
111 
112  case self::SCHEDULE_TYPE_QUARTERLY:
113  $last = $last_run->format('Y') . '-' . ceil(((int) $last_run->format('n')) / 3);
114  $ref = $now->format('Y') . '-' . ceil(((int) $now->format('n')) / 3);
115  return ($last !== $ref);
116 
117  case self::SCHEDULE_TYPE_YEARLY:
118  $last = $last_run->format('Y');
119  $ref = $now->format('Y');
120  return ($last !== $ref);
121 
122  case self::SCHEDULE_TYPE_IN_MINUTES:
123  $diff = floor(($now->getTimestamp() - $last_run->getTimestamp()) / 60);
124  return ($diff >= $schedule_value);
125 
126  case self::SCHEDULE_TYPE_IN_HOURS:
127  $diff = floor(($now->getTimestamp() - $last_run->getTimestamp()) / (60 * 60));
128  return ($diff >= $schedule_value);
129 
130  case self::SCHEDULE_TYPE_IN_DAYS:
131  $diff = floor(($now->getTimestamp() - $last_run->getTimestamp()) / (60 * 60 * 24));
132  return ($diff >= $schedule_value);
133  }
134 
135  return false;
136  }
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 44 of file class.ilCronJob.php.

References ILIAS\Repository\int().

Referenced by checkSchedule().

44  : bool
45  {
46  $last_year = (int) $last_run->format('Y');
47  $now_year = (int) $now->format('Y');
48 
49  if ($last_year > $now_year) {
50  // Should never happen, don't execute otherwise
51  return false;
52  }
53 
54  $last_week = $last_run->format('W');
55  $now_week = $now->format('W');
56 
57  if ($last_week !== $now_week) {
58  // Week differs, always execute the job
59  return true;
60  }
61 
62  // For all following cases, the week number is always identical
63 
64  $last_month = (int) $last_run->format('m');
65  $now_month = (int) $now->format('m');
66 
67  $is_within_same_week_in_same_year = ($last_year . '-' . $last_week) === ($now_year . '-' . $now_week);
68  if ($is_within_same_week_in_same_year) {
69  // Same week in same year, only execute if the month differs (2022-52 is valid for January and December)
70  return $last_month !== $now_month && $now->diff($last_run)->d > 7;
71  }
72 
73  if ($now_year - $last_year > 1) {
74  // Always execute if the difference of years is greater than 1
75  return true;
76  }
77 
78  // 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)
79  return $last_month === $now_month;
80  }
+ 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
int[]

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

Referenced by getValidScheduleTypes().

234  : array
235  {
236  return [
237  self::SCHEDULE_TYPE_DAILY,
238  self::SCHEDULE_TYPE_WEEKLY,
239  self::SCHEDULE_TYPE_MONTHLY,
240  self::SCHEDULE_TYPE_QUARTERLY,
241  self::SCHEDULE_TYPE_YEARLY,
242  self::SCHEDULE_TYPE_IN_MINUTES,
243  self::SCHEDULE_TYPE_IN_HOURS,
244  self::SCHEDULE_TYPE_IN_DAYS,
245  ];
246  }
+ 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)

Returns
int|null

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

References $schedule_type, and hasFlexibleSchedule().

Referenced by CronJobScheduleTest\testWeeklySchedules().

191  : ?int
192  {
193  if ($this->schedule_type && $this->hasFlexibleSchedule()) {
194  return $this->schedule_type;
195  }
196 
197  return null;
198  }
hasFlexibleSchedule()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getScheduleTypesWithValues()

ilCronJob::getScheduleTypesWithValues ( )
Returns
int[]

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

251  : array
252  {
253  return [
254  self::SCHEDULE_TYPE_IN_MINUTES,
255  self::SCHEDULE_TYPE_IN_HOURS,
256  self::SCHEDULE_TYPE_IN_DAYS,
257  ];
258  }

◆ getScheduleValue()

ilCronJob::getScheduleValue ( )

Get current schedule value (if flexible)

Returns
int|null

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

References $schedule_value, and hasFlexibleSchedule().

Referenced by CronJobScheduleTest\testWeeklySchedules().

204  : ?int
205  {
206  if ($this->schedule_value && $this->hasFlexibleSchedule()) {
207  return $this->schedule_value;
208  }
209 
210  return null;
211  }
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
int[]

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

References getAllScheduleTypes().

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

264  : array
265  {
266  return $this->getAllScheduleTypes();
267  }
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 274 of file class.ilCronJob.php.

274  : bool
275  {
276  return false;
277  }

◆ hasFlexibleSchedule()

ilCronJob::hasFlexibleSchedule ( )
abstract

◆ isDue()

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

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

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

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

174  : bool {
175  if ($is_manually_executed) {
176  return true;
177  }
178 
179  if (!$this->hasFlexibleSchedule()) {
182  }
183 
184  return $this->checkSchedule($last_run, $schedule_type, $schedule_value);
185  }
hasFlexibleSchedule()
getDefaultScheduleType()
getDefaultScheduleValue()
checkSchedule(?DateTimeImmutable $last_run, ?int $schedule_type, ?int $schedule_value)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isManuallyExecutable()

ilCronJob::isManuallyExecutable ( )

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

Referenced by ilCronManagerGUI\confirm().

269  : bool
270  {
271  return true;
272  }
+ Here is the caller graph for this function:

◆ run()

ilCronJob::run ( )
abstract

◆ saveCustomSettings()

ilCronJob::saveCustomSettings ( ilPropertyFormGUI  $a_form)

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

283  : bool
284  {
285  return true;
286  }

◆ setDateTimeProvider()

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

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

References $date_time_provider.

Referenced by ilCronManagerImpl\runJob().

141  : void
142  {
143  if ($date_time_provider !== null) {
145  $return_type = $r->getReturnType();
146  if ($return_type !== null) {
147  $return_type = $return_type->getName();
148  }
149  $expected_type = DateTimeInterface::class;
150  if (!is_subclass_of($return_type, $expected_type)) {
151  throw new InvalidArgumentException(sprintf(
152  'The return type of the datetime provider must be of type %s',
153  $expected_type
154  ));
155  }
156 
158  $parameters = $r->getParameters();
159  if ($parameters !== []) {
160  throw new InvalidArgumentException(
161  'The datetime provider must not define any parameters',
162  );
163  }
164  }
165 
166  $this->date_time_provider = $date_time_provider;
167  }
Closure $date_time_provider
+ Here is the caller graph for this function:

◆ setSchedule()

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

Update current schedule (if flexible)

Parameters
int | null$a_type
int | null$a_value

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

References getValidScheduleTypes(), and hasFlexibleSchedule().

218  : void
219  {
220  if (
221  $a_value &&
222  $this->hasFlexibleSchedule() &&
223  in_array($a_type, $this->getValidScheduleTypes(), true)
224  ) {
225  $this->schedule_type = $a_type;
226  $this->schedule_value = $a_value;
227  }
228  }
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 42 of file class.ilCronJob.php.

Referenced by checkSchedule(), and setDateTimeProvider().

◆ $schedule_type

int ilCronJob::$schedule_type = null
protected

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

Referenced by getScheduleType().

◆ $schedule_value

int ilCronJob::$schedule_value = null
protected

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

Referenced by getScheduleValue().

◆ SCHEDULE_TYPE_DAILY

◆ SCHEDULE_TYPE_IN_DAYS

◆ SCHEDULE_TYPE_IN_HOURS

◆ SCHEDULE_TYPE_IN_MINUTES

◆ SCHEDULE_TYPE_MONTHLY

◆ SCHEDULE_TYPE_QUARTERLY

◆ SCHEDULE_TYPE_WEEKLY

◆ SCHEDULE_TYPE_YEARLY


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