-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron-tasks.php
46 lines (38 loc) · 1.86 KB
/
cron-tasks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/**
* Mise à jour automatique des sources d'informations dans Majestic Start
* @copyright 2025 Quentin Pugeat
* @license MIT License
*/
$log_path = __DIR__ . "/logs/cron-task-" . date("YmdHis") . "-" . getmypid() . ".log";
$log = fopen($log_path, "a");
include(__DIR__ . "/init.php");
set_error_handler(function (int $errno, string $errstr, string $errfile = null, int $errline = null, array $errcontext = null) use (&$log) {
echo "$errstr ($errfile:$errline)" . PHP_EOL;
if ($log) fwrite($log, date('Y-m-d H:i:s') . " $errstr ($errfile:$errline)" . PHP_EOL);
});
set_exception_handler(function ($ex) use (&$log) {
echo $ex->__toString() . PHP_EOL;
if ($log) fwrite($log, date('Y-m-d H:i:s') . " " . str_replace(PHP_EOL, " ", $ex->getMessage()) . PHP_EOL);
});
DatabaseConnection::instance()->start_transaction();
model('NewsPostModel')->delete_all();
// Lire les fluxs un par un
$newsFeeds = model('NewsFeedModel')->select_all();
foreach ($newsFeeds as $newsFeed) {
try {
// Lire et parser le flux RSS
$rss = NewsAggregator::load_rss($newsFeed['uuid'], $newsFeed['rss_feed_url']);
$transformed = NewsAggregator::transform($rss->channel->item, $newsFeed['uuid']);
// Insérer les articles nouvellement découverts dans la base de données
$inserted = model('NewsPostModel')->insert($transformed);
// Marquer la source comme étant en fonction
model('NewsFeedModel')->update_one($newsFeed['uuid'], ["access_ok" => 1]);
} catch (Exception $ex) {
// En cas de problème avec un flux, marquer la source comme étant en panne
model('NewsFeedModel')->update_one($newsFeed['uuid'], ["access_ok" => 0]);
if ($log) fwrite($log, date('Y-m-d H:i:s') . " " . str_replace(PHP_EOL, " ", $ex->getMessage()) . PHP_EOL);
}
}
DatabaseConnection::instance()->commit();
if ($log) fclose($log);