-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.php
59 lines (59 loc) · 2.31 KB
/
cron.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
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
require __DIR__ . '/vendor/autoload.php';
/**
* This script will be called periodically as a cron job.
*/
use oSoc\Smartflanders\Datasets;
use oSoc\Smartflanders\Filesystem;
use GO\Scheduler;
use oSoc\Smartflanders\View;
use Dotenv\Dotenv;
// Scheduler setup
// https://github.com/peppeocchi/php-cron-scheduler
// If this script is called with argument "debug", it will simply acquire and write data once
if ($argc == 1) {
$scheduler = new Scheduler();
$scheduler->call(function() {
acquire_data();
sleep(30);
acquire_data();
})->at('* * * * *')->output(__DIR__.'/log/cronjob.log');
$scheduler->run();
} else if ($argv[1] === "debug") {
acquire_data();
}
/**
* This function simply periodically saves the entire turtle file with the current ISO timestamp as filename
* + triples for timestamp and filename of previous file
*/
function acquire_data() {
$dotenv = new Dotenv(__DIR__);
$dotenv->load();
$datasets = explode(',', $_ENV["DATASETS_GATHER"]);
$processors = array();
foreach($datasets as $dataset) {
try {
$dotenv->required($dataset . "_PATH");
$class = $_ENV[$dataset . "_PATH"];
array_push($processors, new $class);
} catch (Exception $e) {
error_log("Invalid .env configuration: dataset " . $dataset . " was has no corresponding class path."
. " Please add the variable " . $dataset . "_PATH.");
}
}
foreach ($processors as $processor) {
if ($processor->mustQuery()) {
$interval = 60*60*3; // 3 hour interval results in files of a few 100 KB
// TODO add out and resources directories to .env. Right now Dutch dataset class has dependency on this.
$fs = new Filesystem\FileWriter(__DIR__ . "/out", __DIR__ . "/resources", $interval, $processor);
echo "Fetching graph for " . $processor->getName() . "\n";
$graph = $processor->getDynamicGraph();
$now = time();
echo "Writing data for " . $processor->getName() . "\n";
$fs->writeToFile($now, $graph);
// Temporarily disabling range gates, semantically incorrect
echo "Updating statistical summary for " . $processor->getName() . "\n";
$fs->updateStatisticalSummary($now, $graph);
}
}
}