Skip to content

Commit 6ce6a15

Browse files
committed
Component/Cron: init cron jobs via SetupAgent and Component's seek
1 parent 753ae4b commit 6ce6a15

12 files changed

Lines changed: 377 additions & 12 deletions

components/ILIAS/Authentication/Authentication.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,11 @@ public function offsetUnset(mixed $offset): void
6767
new Component\Resource\Endpoint($this, "sessioncheck.php");
6868
$contribute[Component\Resource\PublicAsset::class] = fn() =>
6969
new Component\Resource\ComponentJS($this, "session_reminder.js");
70+
71+
$contribute[\ILIAS\Cron\CronJob::class] = static fn() =>
72+
new \ilAuthDestroyExpiredSessionsCron(
73+
'components\\' . self::class,
74+
$use[\ILIAS\Language\Language::class]
75+
);
7076
}
7177
}

components/ILIAS/Authentication/classes/Cron/class.ilAuthDestroyExpiredSessionsCron.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020

2121
class ilAuthDestroyExpiredSessionsCron extends ilCronJob
2222
{
23-
protected ilLanguage $lng;
24-
25-
public function __construct()
26-
{
27-
global $DIC;
28-
29-
$this->lng = $DIC->language();
23+
public function __construct(
24+
string $component,
25+
\ILIAS\Language\Language $lng,
26+
) {
27+
parent::__construct($component, $lng);
3028
$this->lng->loadLanguageModule('auth');
3129
}
3230

components/ILIAS/Component/classes/Setup/class.ilComponentDefinitionsStoredObjective.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,14 @@ public function write(): void
143143
new \ilCOPageDefinitionProcessor($db),
144144
new \ilComponentInfoDefinitionProcessor(),
145145
new \ilLoggingDefinitionProcessor($db),
146+
/*
146147
new \ilCronDefinitionProcessor(
147148
$db,
148149
$settings_factory->settingsFor(),
149150
$component_repository,
150151
$component_factory
151152
),
153+
*/
152154
new \ilMailTemplateContextDefinitionProcessor($db),
153155
new \ilObjectDefinitionProcessor($db),
154156
new \ilSystemCheckDefinitionProcessor($db),

components/ILIAS/Cron/Cron.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public function init(
3232
array | \ArrayAccess &$pull,
3333
array | \ArrayAccess &$internal,
3434
): void {
35-
// ...
35+
$contribute[\ILIAS\Setup\Agent::class] = static fn() =>
36+
new \ilCronJobSetupAgent(
37+
$seek[\ILIAS\Cron\CronJob::class]
38+
);
3639
}
3740
}

components/ILIAS/Cron/classes/Setup/class.ilCronDefinitionProcessor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function __construct(
3434
ilComponentFactory $componentFactory
3535
) {
3636
$this->has_cron = [];
37+
//throw new Exception("CRON JOB DEFINITION", 1);
3738

3839
$this->cronRepository = new ilCronJobRepositoryImpl(
3940
$this->db,
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/**
4+
* This file is part of ILIAS, a powerful learning management system
5+
* published by ILIAS open source e-Learning e.V.
6+
*
7+
* ILIAS is licensed with the GPL-3.0,
8+
* see https://www.gnu.org/licenses/gpl-3.0.en.html
9+
* You should have received a copy of said license along with the
10+
* source code, too.
11+
*
12+
* If this is not the case or you just want to try ILIAS, you'll find
13+
* us at:
14+
* https://www.ilias.de
15+
* https://github.com/ILIAS-eLearning
16+
*
17+
*********************************************************************/
18+
19+
declare(strict_types=1);
20+
21+
use ILIAS\Setup;
22+
use ILIAS\Refinery;
23+
24+
class ilCronJobSetupAgent implements Setup\Agent
25+
{
26+
use Setup\Agent\HasNoNamedObjective;
27+
28+
public function __construct(
29+
private array $cronjobs
30+
) {
31+
}
32+
33+
public function hasConfig(): bool
34+
{
35+
return false;
36+
}
37+
38+
public function getArrayToConfigTransformation(): Refinery\Transformation
39+
{
40+
throw new LogicException('Agent has no config.');
41+
}
42+
43+
public function getInstallObjective(Setup\Config $config = null): Setup\Objective
44+
{
45+
return new ilCronjobsRegisteredObjective(
46+
$this->cronjobs
47+
);
48+
}
49+
50+
public function getUpdateObjective(Setup\Config $config = null): Setup\Objective
51+
{
52+
return new ilCronjobsRegisteredObjective(
53+
$this->cronjobs
54+
);
55+
}
56+
57+
public function getBuildObjective(): Setup\Objective
58+
{
59+
return new Setup\Objective\NullObjective();
60+
}
61+
62+
public function getStatusObjective(Setup\Metrics\Storage $storage): Setup\Objective
63+
{
64+
return new ilCronJobsMetricsCollectedObjective($storage);
65+
}
66+
67+
public function getMigrations(): array
68+
{
69+
return [];
70+
}
71+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of ILIAS, a powerful learning management system
7+
* published by ILIAS open source e-Learning e.V.
8+
*
9+
* ILIAS is licensed with the GPL-3.0,
10+
* see https://www.gnu.org/licenses/gpl-3.0.en.html
11+
* You should have received a copy of said license along with the
12+
* source code, too.
13+
*
14+
* If this is not the case or you just want to try ILIAS, you'll find
15+
* us at:
16+
* https://www.ilias.de
17+
* https://github.com/ILIAS-eLearning
18+
*
19+
********************************************************************
20+
*/
21+
22+
use ILIAS\Setup;
23+
24+
class ilCronJobsMetricsCollectedObjective extends Setup\Metrics\CollectedObjective
25+
{
26+
/**
27+
* @inheritDoc
28+
*/
29+
protected function getTentativePreconditions(Setup\Environment $environment): array
30+
{
31+
return [
32+
new ilIniFilesLoadedObjective(),
33+
new ilDatabaseInitializedObjective(),
34+
new ilComponentRepositoryExistsObjective(),
35+
new ilComponentFactoryExistsObjective()
36+
];
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
protected function collectFrom(Setup\Environment $environment, Setup\Metrics\Storage $storage): void
43+
{
44+
$db = $environment->getResource(Setup\Environment::RESOURCE_DATABASE);
45+
$component_repository = $environment->getResource(Setup\Environment::RESOURCE_COMPONENT_REPOSITORY);
46+
$component_factory = $environment->getResource(Setup\Environment::RESOURCE_COMPONENT_FACTORY);
47+
$settings_factory = $environment->getResource(Setup\Environment::RESOURCE_SETTINGS_FACTORY);
48+
49+
$mock_lng = new class () implements \ILIAS\Language\Language {
50+
public function txt(string $a_topic, string $a_default_lang_fallback_mod = ""): string
51+
{
52+
return '';
53+
}
54+
public function loadLanguageModule(string $a_module): void
55+
{
56+
}
57+
};
58+
59+
$repo = new ilCronJobRepositoryImpl(
60+
$db,
61+
$settings_factory->settingsFor(),
62+
new ILIAS\components\Logging\NullLogger(),
63+
$component_repository,
64+
$component_factory,
65+
$mock_lng
66+
);
67+
68+
//@var ilCronJobEntity[]
69+
$collection = $repo->findAll()->toArray();
70+
$cron_jobs = [];
71+
foreach ($collection as $entity) {
72+
$active = new Setup\Metrics\Metric(
73+
Setup\Metrics\Metric::STABILITY_VOLATILE,
74+
Setup\Metrics\Metric::TYPE_BOOL,
75+
(bool) $entity->getJobStatus(),
76+
"Is the job active?"
77+
);
78+
$component = new Setup\Metrics\Metric(
79+
Setup\Metrics\Metric::STABILITY_STABLE,
80+
Setup\Metrics\Metric::TYPE_TEXT,
81+
$entity->getComponent()
82+
);
83+
$cron_jobs[$entity->getJobId()] = new Setup\Metrics\Metric(
84+
Setup\Metrics\Metric::STABILITY_MIXED,
85+
Setup\Metrics\Metric::TYPE_COLLECTION,
86+
[
87+
"component" => $component,
88+
"active" => $active
89+
]
90+
);
91+
}
92+
$cron_jobs = new Setup\Metrics\Metric(
93+
Setup\Metrics\Metric::STABILITY_MIXED,
94+
Setup\Metrics\Metric::TYPE_COLLECTION,
95+
$cron_jobs
96+
);
97+
98+
$cron_jobs_count = new Setup\Metrics\Metric(
99+
Setup\Metrics\Metric::STABILITY_STABLE,
100+
Setup\Metrics\Metric::TYPE_GAUGE,
101+
count($collection)
102+
);
103+
$storage->store(
104+
"number of cron jobs",
105+
$cron_jobs_count
106+
);
107+
$storage->store(
108+
"cron jobs",
109+
$cron_jobs
110+
);
111+
}
112+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/**
4+
* This file is part of ILIAS, a powerful learning management system
5+
* published by ILIAS open source e-Learning e.V.
6+
*
7+
* ILIAS is licensed with the GPL-3.0,
8+
* see https://www.gnu.org/licenses/gpl-3.0.en.html
9+
* You should have received a copy of said license along with the
10+
* source code, too.
11+
*
12+
* If this is not the case or you just want to try ILIAS, you'll find
13+
* us at:
14+
* https://www.ilias.de
15+
* https://github.com/ILIAS-eLearning
16+
*
17+
*********************************************************************/
18+
19+
declare(strict_types=1);
20+
21+
use ILIAS\Setup;
22+
23+
class ilCronjobsRegisteredObjective implements Setup\Objective
24+
{
25+
public function __construct(
26+
private array $cronjobs
27+
) {
28+
}
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
public function getHash(): string
34+
{
35+
return hash("sha256", self::class);
36+
}
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
public function getLabel(): string
42+
{
43+
return "CronJobs are registered.";
44+
}
45+
46+
/**
47+
* @inheritdoc
48+
*/
49+
public function isNotable(): bool
50+
{
51+
return true;
52+
}
53+
54+
/**
55+
* @inheritdoc
56+
*/
57+
public function getPreconditions(Setup\Environment $environment): array
58+
{
59+
return [
60+
new \ilDatabaseUpdatedObjective()
61+
];
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
67+
public function achieve(Setup\Environment $environment): Setup\Environment
68+
{
69+
$db = $environment->getResource(Setup\Environment::RESOURCE_DATABASE);
70+
/** @var ilComponentRepository $component_repository */
71+
$component_repository = $environment->getResource(Setup\Environment::RESOURCE_COMPONENT_REPOSITORY);
72+
/** @var ilComponentFactory $component_factory */
73+
$component_factory = $environment->getResource(Setup\Environment::RESOURCE_COMPONENT_FACTORY);
74+
/** @var ilSettingsFactory $settings_factory */
75+
$settings_factory = $environment->getResource(Setup\Environment::RESOURCE_SETTINGS_FACTORY);
76+
77+
$mock_lng = new class () implements \ILIAS\Language\Language {
78+
public function txt(string $a_topic, string $a_default_lang_fallback_mod = ""): string
79+
{
80+
return '';
81+
}
82+
public function loadLanguageModule(string $a_module): void
83+
{
84+
}
85+
};
86+
87+
$repo = new ilCronJobRepositoryImpl(
88+
$db,
89+
$settings_factory->settingsFor(),
90+
new ILIAS\components\Logging\NullLogger(),
91+
$component_repository,
92+
$component_factory,
93+
$mock_lng
94+
);
95+
96+
$repo->unregisterAllJobs();
97+
98+
foreach ($this->cronjobs as $class => $job) {
99+
$repo->registerJob(
100+
$job->getComponent(),
101+
$job->getId(),
102+
get_class($job),
103+
null //path!
104+
);
105+
}
106+
107+
return $environment;
108+
}
109+
110+
/**
111+
* @inheritdoc
112+
*/
113+
public function isApplicable(Setup\Environment $environment): bool
114+
{
115+
return true;
116+
}
117+
}

components/ILIAS/Cron/classes/class.ilCronJob.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,25 @@
1919
declare(strict_types=1);
2020

2121
use ILIAS\Cron\Schedule\CronJobScheduleType;
22+
use ILIAS\Cron\CronJob;
2223

23-
abstract class ilCronJob
24+
abstract class ilCronJob implements CronJob
2425
{
2526
protected ?CronJobScheduleType $schedule_type = null;
2627
protected ?int $schedule_value = null;
2728
protected ?Closure $date_time_provider = null;
2829

30+
public function __construct(
31+
protected readonly string $component,
32+
protected readonly \ILIAS\Language\Language $lng,
33+
) {
34+
}
35+
36+
public function getComponent(): string
37+
{
38+
return $this->component;
39+
}
40+
2941
private function checkWeeklySchedule(DateTimeImmutable $last_run, DateTimeImmutable $now): bool
3042
{
3143
$last_year = (int) $last_run->format('Y');

0 commit comments

Comments
 (0)