Skip to content

Commit 54596fe

Browse files
committed
Cron: init with CronRegistry and LoggerFactory
1 parent bf5ad41 commit 54596fe

14 files changed

Lines changed: 184 additions & 190 deletions

components/ILIAS/Cron/Cron.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,16 @@ public function init(
3636
new \ilCronJobSetupAgent(
3737
$seek[\ILIAS\Cron\CronJob::class]
3838
);
39+
40+
$define[] = Cron\Registry::class;
41+
42+
$provide[Cron\Registry::class] = static fn() =>
43+
$internal[Cron\CronRegistry::class];
44+
45+
$internal[Cron\CronRegistry::class] = static fn() =>
46+
new Cron\CronRegistry(
47+
$seek[\ILIAS\Cron\CronJob::class]
48+
);
49+
3950
}
4051
}

components/ILIAS/Cron/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class MyComponent implements Component\Component
4242
): void {
4343
$contribute[\ILIAS\Cron\CronJob::class] = static fn() =>
4444
new \MyComponentCronJob(
45-
'components\\' . self::class,
45+
self::class,
4646
$use[\ILIAS\Language\Language::class]
4747
);
4848
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
class ilCronJobSetupAgent implements Setup\Agent
2525
{
26-
use Setup\Agent\HasNoNamedObjective;
27-
2826
public function __construct(
2927
private array $cronjobs
3028
) {
@@ -61,11 +59,23 @@ public function getBuildObjective(): Setup\Objective
6159

6260
public function getStatusObjective(Setup\Metrics\Storage $storage): Setup\Objective
6361
{
64-
return new ilCronJobsMetricsCollectedObjective($storage);
62+
return new Setup\Objective\NullObjective();
6563
}
6664

6765
public function getMigrations(): array
6866
{
6967
return [];
7068
}
69+
70+
public function getNamedObjectives(?Setup\Config $config = null): array
71+
{
72+
return [
73+
'registerCronJobs' => new Setup\ObjectiveConstructor(
74+
'registers cron jobs',
75+
fn(): Setup\Objective => new ilCronjobsRegisteredObjective(
76+
$this->cronjobs
77+
)
78+
)
79+
];
80+
}
7181
}

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

Lines changed: 0 additions & 112 deletions
This file was deleted.

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public function isNotable(): bool
5757
public function getPreconditions(Setup\Environment $environment): array
5858
{
5959
return [
60+
new \ilSettingsFactoryExistsObjective(),
61+
new \ilComponentFactoryExistsObjective(),
6062
new \ilDatabaseUpdatedObjective()
6163
];
6264
}
@@ -82,15 +84,28 @@ public function txt(string $a_topic, string $a_default_lang_fallback_mod = ""):
8284
public function loadLanguageModule(string $a_module): void
8385
{
8486
}
87+
public function getLangKey(): string
88+
{
89+
}
90+
public function toJS($key): void
91+
{
92+
}
8593
};
8694

95+
$mock_logger_factory = new class () implements \ILIAS\Logging\LoggerFactory {
96+
};
97+
98+
$registry = new ILIAS\Cron\CronRegistry($this->cronjobs);
99+
87100
$repo = new ilCronJobRepositoryImpl(
101+
$registry,
88102
$db,
89103
$settings_factory->settingsFor(),
90104
new ILIAS\components\Logging\NullLogger(),
91105
$component_repository,
92106
$component_factory,
93-
$mock_lng
107+
$mock_lng,
108+
$mock_logger_factory
94109
);
95110

96111
$repo->unregisterAllJobs();

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,18 @@ abstract class ilCronJob implements CronJob
3030
public function __construct(
3131
protected readonly string $component,
3232
protected readonly \ILIAS\Language\Language $lng,
33+
protected readonly \ILIAS\Logging\LoggerFactory $logger_factory,
3334
) {
3435
}
3536

37+
/**
38+
* init is called when actually using the job;
39+
* once the jobs are properly constructed via Component,this is obsolete.
40+
*/
41+
public function init(): void
42+
{
43+
}
44+
3645
public function getComponent(): string
3746
{
3847
return $this->component;

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,24 @@ class ilCronJobEntity
5050
*/
5151
public function __construct(private readonly ilCronJob $job, array $record, private readonly bool $isPlugin = false)
5252
{
53-
$this->mapRecord($record);
53+
$job->init();
54+
$this->mapRecord($job, $record);
5455
}
5556

5657
/**
5758
* @param array<string, mixed> $record
5859
*/
59-
private function mapRecord(array $record): void
60+
private function mapRecord(ilCronJob $job, array $record): void
6061
{
61-
$this->jobId = (string) $record['job_id'];
62-
$this->component = (string) $record['component'];
62+
if (! array_key_exists('job_id', $record)) {
63+
//TODO: remove!
64+
var_dump($record);
65+
die();
66+
}
67+
//$this->jobId = (string) $record['job_id'];
68+
//$this->component = (string) $record['component'];
69+
$this->jobId = $job->getId();
70+
$this->component = $job->getComponent();
6371
$this->scheduleType = is_numeric($record['schedule_type']) ? CronJobScheduleType::tryFrom((int) $record['schedule_type']) : null;
6472
$this->scheduleValue = (int) $record['schedule_value'];
6573
$this->jobStatus = (int) $record['job_status'];

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

Lines changed: 24 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -19,64 +19,34 @@
1919
declare(strict_types=1);
2020

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

2324
class ilCronJobRepositoryImpl implements ilCronJobRepository
2425
{
2526
private const TYPE_PLUGINS = 'Plugins';
2627

2728
public function __construct(
29+
private readonly Registry $registry,
2830
private readonly ilDBInterface $db,
2931
private readonly ilSetting $setting,
3032
private readonly ilLogger $logger,
3133
private readonly ilComponentRepository $componentRepository,
3234
private readonly ilComponentFactory $componentFactory,
33-
private readonly ILIAS\Language\Language $lng
35+
private readonly ILIAS\Language\Language $lng,
36+
private readonly ILIAS\Logging\LoggerFactory $logger_factory
3437
) {
3538
}
3639

3740
public function getJobInstanceById(string $id): ?ilCronJob
3841
{
39-
// plugin
40-
if (str_starts_with($id, 'pl__')) {
41-
$parts = explode('__', $id);
42-
$pl_name = $parts[1];
43-
$job_id = $parts[2];
44-
45-
foreach ($this->componentRepository->getPlugins() as $pl) {
46-
if ($pl->getName() !== $pl_name || !$pl->isActive()) {
47-
continue;
48-
}
49-
50-
$plugin = $this->componentFactory->getPlugin($pl->getId());
51-
if (!$plugin instanceof ilCronJobProvider) {
52-
continue;
53-
}
54-
55-
try {
56-
$job = $plugin->getCronJobInstance($job_id);
57-
58-
// should never happen but who knows...
59-
$jobs_data = $this->getCronJobData($job_id);
60-
if ($jobs_data === []) {
61-
// as job is not 'imported' from xml
62-
$this->createDefaultEntry($job, $pl_name, self::TYPE_PLUGINS, '');
63-
}
6442

65-
return $job;
66-
} catch (OutOfBoundsException) {
67-
// Maybe a job was removed from plugin, renamed etc.
68-
}
69-
break;
70-
}
71-
} else {
72-
$jobs_data = $this->getCronJobData($id);
73-
if ($jobs_data !== [] && $jobs_data[0]['job_id'] === $id) {
74-
return $this->getJobInstance(
75-
$jobs_data[0]['job_id'],
76-
$jobs_data[0]['component'],
77-
$jobs_data[0]['class']
78-
);
79-
}
43+
$jobs_data = $this->getCronJobData($id);
44+
if ($jobs_data !== [] && $jobs_data[0]['job_id'] === $id) {
45+
return $this->getJobInstance(
46+
$jobs_data[0]['job_id'],
47+
$jobs_data[0]['component'],
48+
$jobs_data[0]['class']
49+
);
8050
}
8151

8252
$this->logger->info('CRON - job ' . $id . ' seems invalid or is inactive');
@@ -87,19 +57,15 @@ public function getJobInstanceById(string $id): ?ilCronJob
8757
public function getJobInstance(
8858
string $a_id,
8959
string $a_component,
90-
string $a_class,
91-
bool $isCreationContext = false
60+
string $a_class
9261
): ?ilCronJob {
62+
9363
if (class_exists($a_class)) {
94-
if ($isCreationContext) {
95-
$refl = new ReflectionClass($a_class);
96-
$job = $refl->newInstanceWithoutConstructor();
97-
} else {
98-
$job = new $a_class(
99-
$a_component,
100-
$this->lng
101-
);
102-
}
64+
$job = new $a_class(
65+
$a_component,
66+
$this->lng,
67+
$this->logger_factory
68+
);
10369

10470
if ($job instanceof ilCronJob && $job->getId() === $a_id) {
10571
return $job;
@@ -151,7 +117,8 @@ public function registerJob(
151117
return;
152118
}
153119

154-
$job = $this->getJobInstance($a_id, $a_component, $a_class, true);
120+
//$job = $this->getJobInstance($a_id, $a_component, $a_class, true);
121+
$job = $this->getJobInstance($a_id, $a_component, $a_class);
155122
if ($job) {
156123
$this->createDefaultEntry($job, $a_component, $a_class, $a_path);
157124
}
@@ -401,21 +368,11 @@ public function findAll(): ilCronJobCollection
401368
{
402369
$collection = new ilCronJobEntities();
403370

404-
foreach ($this->getCronJobData() as $item) {
405-
$job = $this->getJobInstance(
406-
$item['job_id'],
407-
$item['component'],
408-
$item['class']
409-
);
410-
if ($job) {
411-
$collection->add(new ilCronJobEntity($job, $item));
412-
}
371+
foreach ($this->registry->getAllJobs() as $job) {
372+
$job_data = $this->getCronJobData($job->getId());
373+
$entity = new ilCronJobEntity($job, array_shift($job_data));
374+
$collection->add($entity);
413375
}
414-
415-
foreach ($this->getPluginJobs() as $item) {
416-
$collection->add(new ilCronJobEntity($item[0], $item[1], true));
417-
}
418-
419376
return $collection;
420377
}
421378
}

0 commit comments

Comments
 (0)