Skip to content

Commit 55fc9f0

Browse files
committed
refactor(CronTask): decoupling data sources tasks
1 parent 63bfc47 commit 55fc9f0

File tree

7 files changed

+279
-32
lines changed

7 files changed

+279
-32
lines changed

install/install/create_automatic_actions.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@
3232

3333
use GlpiPlugin\Carbon\CronTask;
3434
use CronTask as GlpiCronTask;
35+
use GlpiPlugin\Carbon\DataSource\CronTaskProvider;
3536

36-
$automatic_actions = [
37+
$cron_task_classes = CronTaskProvider::getCronTaskTypes();
38+
$automatic_actions = [];
39+
foreach ($cron_task_classes as $cron_task_class) {
40+
$automatic_actions = array_merge($automatic_actions, $cron_task_class::enumerateTasks());
41+
}
42+
43+
$automatic_actions = array_merge($automatic_actions, [
3744
[
3845
'itemtype' => CronTask::class,
3946
'name' => 'LocationCountryCode',
@@ -58,18 +65,18 @@
5865
'param' => 10000, // Maximum rows to generate per execution
5966
]
6067
],
61-
[
62-
'itemtype' => CronTask::class,
63-
'name' => 'DownloadRte',
64-
'frequency' => DAY_TIMESTAMP,
65-
'options' => [
66-
'mode' => GlpiCronTask::MODE_EXTERNAL,
67-
'allowmode' => GlpiCronTask::MODE_INTERNAL + GlpiCronTask::MODE_EXTERNAL,
68-
'logs_lifetime' => 30,
69-
'comment' => __('Collect carbon intensities from RTE', 'carbon'),
70-
'param' => 10000, // Maximum rows to generate per execution
71-
]
72-
],
68+
// [
69+
// 'itemtype' => CronTask::class,
70+
// 'name' => 'DownloadRte',
71+
// 'frequency' => DAY_TIMESTAMP,
72+
// 'options' => [
73+
// 'mode' => GlpiCronTask::MODE_EXTERNAL,
74+
// 'allowmode' => GlpiCronTask::MODE_INTERNAL + GlpiCronTask::MODE_EXTERNAL,
75+
// 'logs_lifetime' => 30,
76+
// 'comment' => __('Collect carbon intensities from RTE', 'carbon'),
77+
// 'param' => 10000, // Maximum rows to generate per execution
78+
// ]
79+
// ],
7380
[
7481
'itemtype' => CronTask::class,
7582
'name' => 'DownloadElectricityMap',
@@ -94,7 +101,7 @@
94101
'param' => 10000, // Maximum rows to generate per execution
95102
]
96103
],
97-
];
104+
]);
98105

99106
foreach ($automatic_actions as $action) {
100107
$task = new GlpiCronTask();
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* -------------------------------------------------------------------------
5+
* Carbon plugin for GLPI
6+
*
7+
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
8+
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
9+
* @link https://github.com/pluginsGLPI/carbon
10+
*
11+
* -------------------------------------------------------------------------
12+
*
13+
* LICENSE
14+
*
15+
* This file is part of Carbon plugin for GLPI.
16+
*
17+
* This program is free software: you can redistribute it and/or modify
18+
* it under the terms of the GNU General Public License as published by
19+
* the Free Software Foundation, either version 3 of the License, or
20+
* (at your option) any later version.
21+
*
22+
* This program is distributed in the hope that it will be useful,
23+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
* GNU General Public License for more details.
26+
*
27+
* You should have received a copy of the GNU General Public License
28+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
29+
*
30+
* -------------------------------------------------------------------------
31+
*/
32+
33+
use CronTask as GLPICronTask;
34+
35+
$task = new GlpiCronTask();
36+
$task->getFromDBByCrit([
37+
'itemtype' => 'GlpiPlugin\\Carbon\\CronTask',
38+
'name' => 'DownloadRte',
39+
]);
40+
if (!$task->isNewItem()) {
41+
$task->update([
42+
'id' => $task->getID(),
43+
'itemtype' => 'GlpiPlugin\\Carbon\\DataSource\\CarbonIntensity\\Rte\\CronTask',
44+
]);
45+
}

src/CronTask.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ public static function cronInfo(string $name)
6565
'parameter' => __('Maximum number of locations to solve', 'carbon'),
6666
];
6767

68-
case 'DownloadRte':
69-
return [
70-
'description' => __('Download carbon emissions from RTE', 'carbon'),
71-
'parameter' => __('Maximum number of entries to download', 'carbon'),
72-
];
68+
// case 'DownloadRte':
69+
// return [
70+
// 'description' => __('Download carbon emissions from RTE', 'carbon'),
71+
// 'parameter' => __('Maximum number of entries to download', 'carbon'),
72+
// ];
7373

7474
case 'DownloadElectricityMap':
7575
return [
@@ -198,16 +198,16 @@ public static function cronEmbodiedImpact(GlpiCronTask $task): int
198198
return ($count > 0 ? 1 : 0);
199199
}
200200

201-
/**
202-
* Automatic action for RTE datasource
203-
*
204-
* @return int
205-
*/
206-
public static function cronDownloadRte(GlpiCronTask $task): int
207-
{
208-
$client = ClientFactory::create('Rte');
209-
return self::downloadCarbonIntensityFromSource($task, $client, new CarbonIntensity());
210-
}
201+
// /**
202+
// * Automatic action for RTE datasource
203+
// *
204+
// * @return int
205+
// */
206+
// public static function cronDownloadRte(GlpiCronTask $task): int
207+
// {
208+
// $client = ClientFactory::create('Rte');
209+
// return self::downloadCarbonIntensityFromSource($task, $client, new CarbonIntensity());
210+
// }
211211

212212
/**
213213
* Automatic action for ElectricityMap datasource
@@ -239,7 +239,7 @@ public static function cronDownloadWatttime(GlpiCronTask $task): int
239239
* @param CarbonIntensity $intensity
240240
* @return integer
241241
*/
242-
protected static function downloadCarbonIntensityFromSource(GlpiCronTask $task, ClientInterface $data_source, CarbonIntensity $intensity): int
242+
public static function downloadCarbonIntensityFromSource(GlpiCronTask $task, ClientInterface $data_source, CarbonIntensity $intensity): int
243243
{
244244
$task->setVolume(0); // start with zero
245245
$remaining = $task->fields['param'];
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/**
4+
* -------------------------------------------------------------------------
5+
* Carbon plugin for GLPI
6+
*
7+
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
8+
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
9+
* @link https://github.com/pluginsGLPI/carbon
10+
*
11+
* -------------------------------------------------------------------------
12+
*
13+
* LICENSE
14+
*
15+
* This file is part of Carbon plugin for GLPI.
16+
*
17+
* This program is free software: you can redistribute it and/or modify
18+
* it under the terms of the GNU General Public License as published by
19+
* the Free Software Foundation, either version 3 of the License, or
20+
* (at your option) any later version.
21+
*
22+
* This program is distributed in the hope that it will be useful,
23+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
* GNU General Public License for more details.
26+
*
27+
* You should have received a copy of the GNU General Public License
28+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
29+
*
30+
* -------------------------------------------------------------------------
31+
*/
32+
33+
namespace GlpiPlugin\Carbon\DataSource\CarbonIntensity\Rte;
34+
35+
use CronTask as GlpiCronTask;
36+
use GlpiPlugin\Carbon\CarbonIntensity;
37+
use GlpiPlugin\Carbon\CronTask as CarbonCronTask;
38+
use GlpiPlugin\Carbon\DataSource\CarbonIntensity\ClientFactory;
39+
use GlpiPlugin\Carbon\DataSource\CronTaskInterface;
40+
41+
class CronTask implements CronTaskInterface
42+
{
43+
public static function enumerateTasks(): array
44+
{
45+
// TODO: This data shoud replace the occurrence in CronTask::cronInfo()
46+
return [
47+
[
48+
'itemtype' => self::class,
49+
'name' => 'DownloadRte',
50+
'frequency' => DAY_TIMESTAMP,
51+
'options' => [
52+
'mode' => GlpiCronTask::MODE_EXTERNAL,
53+
'allowmode' => GlpiCronTask::MODE_INTERNAL + GlpiCronTask::MODE_EXTERNAL,
54+
'logs_lifetime' => 30,
55+
'comment' => __('Compute carbon emissions of computers', 'carbon'),
56+
'param' => 10000, // Maximum rows to generate per execution
57+
]
58+
]
59+
];
60+
}
61+
62+
/**
63+
* Get description of an automatic action
64+
*
65+
* @param string $name
66+
* @return array
67+
*/
68+
public static function cronInfo(string $name)
69+
{
70+
switch ($name) {
71+
case 'DownloadRte':
72+
return [
73+
'description' => __('Download carbon emissions from RTE', 'carbon'),
74+
'parameter' => __('Maximum number of entries to download', 'carbon'),
75+
];
76+
}
77+
return [];
78+
}
79+
80+
/**
81+
* Automatic action for RTE datasource
82+
*
83+
* @return int
84+
*/
85+
public static function cronDownloadRte(GlpiCronTask $task): int
86+
{
87+
$client = ClientFactory::create('Rte');
88+
return CarbonCronTask::downloadCarbonIntensityFromSource($task, $client, new CarbonIntensity());
89+
}
90+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* -------------------------------------------------------------------------
5+
* Carbon plugin for GLPI
6+
*
7+
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
8+
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
9+
* @link https://github.com/pluginsGLPI/carbon
10+
*
11+
* -------------------------------------------------------------------------
12+
*
13+
* LICENSE
14+
*
15+
* This file is part of Carbon plugin for GLPI.
16+
*
17+
* This program is free software: you can redistribute it and/or modify
18+
* it under the terms of the GNU General Public License as published by
19+
* the Free Software Foundation, either version 3 of the License, or
20+
* (at your option) any later version.
21+
*
22+
* This program is distributed in the hope that it will be useful,
23+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
* GNU General Public License for more details.
26+
*
27+
* You should have received a copy of the GNU General Public License
28+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
29+
*
30+
* -------------------------------------------------------------------------
31+
*/
32+
33+
namespace GlpiPlugin\Carbon\DataSource;
34+
35+
interface CronTaskInterface
36+
{
37+
public static function enumerateTasks(): array;
38+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* -------------------------------------------------------------------------
5+
* Carbon plugin for GLPI
6+
*
7+
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
8+
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
9+
* @link https://github.com/pluginsGLPI/carbon
10+
*
11+
* -------------------------------------------------------------------------
12+
*
13+
* LICENSE
14+
*
15+
* This file is part of Carbon plugin for GLPI.
16+
*
17+
* This program is free software: you can redistribute it and/or modify
18+
* it under the terms of the GNU General Public License as published by
19+
* the Free Software Foundation, either version 3 of the License, or
20+
* (at your option) any later version.
21+
*
22+
* This program is distributed in the hope that it will be useful,
23+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
* GNU General Public License for more details.
26+
*
27+
* You should have received a copy of the GNU General Public License
28+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
29+
*
30+
* -------------------------------------------------------------------------
31+
*/
32+
33+
namespace GlpiPlugin\Carbon\DataSource;
34+
35+
class CronTaskProvider
36+
{
37+
public static function getCronTaskTypes(): array
38+
{
39+
$subdirs = ['CarbonIntensity', 'Lca'];
40+
$types = [];
41+
foreach ($subdirs as $subdir) {
42+
foreach (glob(__DIR__ . '/' . $subdir . '/*', GLOB_ONLYDIR) as $connector_dir) {
43+
$dir = basename($connector_dir);
44+
$class_name = 'GlpiPlugin\\Carbon\\DataSource\\' . $subdir . '\\'
45+
. $dir . '\\CronTask';
46+
if (!class_exists($class_name)) {
47+
continue;
48+
}
49+
if (!is_subclass_of($class_name, CronTaskInterface::class)) {
50+
continue;
51+
}
52+
$types[$dir] = $class_name;
53+
}
54+
}
55+
56+
return $types;
57+
}
58+
}

tests/install/PluginInstallTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
use Glpi\DBAL\QueryExpression as QueryExpression;
5353
use Glpi\Plugin\Hooks;
5454
use Glpi\System\Diagnostic\DatabaseSchemaIntegrityChecker;
55+
use GlpiPlugin\Carbon\DataSource\CarbonIntensity\Rte\CronTask as RteCronTask;
5556
use GlpiPlugin\Carbon\CarbonIntensity;
5657
use GlpiPlugin\Carbon\Source;
5758
use GlpiPlugin\Carbon\Source_Zone;
@@ -232,7 +233,7 @@ private function checkAutomaticAction()
232233
{
233234
$cronTask = new GLPICronTask();
234235
$rows = $cronTask->find([
235-
'itemtype' => ['LIKE', '%' . 'Carbon' . '%'],
236+
'itemtype' => ['LIKE', 'GlpiPlugin\\\\Carbon\\\\%'],
236237
]);
237238
$this->assertEquals(5, count($rows));
238239

@@ -254,9 +255,17 @@ private function checkAutomaticAction()
254255

255256
$cronTask = new GLPICronTask();
256257
$cronTask->getFromDBByCrit([
257-
'itemtype' => CronTask::class,
258+
'itemtype' => RteCronTask::class,
258259
'name' => 'DownloadRte',
259260
]);
261+
// global $DB;
262+
// $it = $DB->request([
263+
// 'FROM' => GLPICronTask::getTable(),
264+
// 'WHERE' => [
265+
// 'name' => 'DownloadRte',
266+
// ],
267+
// ]);
268+
// fwrite(STDERR, var_export(iterator_to_array($it), true));
260269
$this->assertFalse($cronTask->isNewItem());
261270

262271
$cronTask = new GLPICronTask();

0 commit comments

Comments
 (0)