3241 |
rexy |
1 |
#!/usr/bin/env php
|
|
|
2 |
<?php
|
|
|
3 |
/**
|
|
|
4 |
* daemon for nfsen-ng.
|
|
|
5 |
*
|
|
|
6 |
* @phpstan-return never-return
|
|
|
7 |
*/
|
|
|
8 |
include_once implode(\DIRECTORY_SEPARATOR, [__DIR__, '..', 'vendor', 'autoload.php']);
|
|
|
9 |
|
|
|
10 |
use mbolli\nfsen_ng\common\Config;
|
|
|
11 |
use mbolli\nfsen_ng\common\Debug;
|
|
|
12 |
use mbolli\nfsen_ng\common\Import;
|
|
|
13 |
|
|
|
14 |
ini_set('display_errors', true);
|
|
|
15 |
ini_set('error_reporting', \E_ALL);
|
|
|
16 |
|
|
|
17 |
$d = Debug::getInstance();
|
|
|
18 |
try {
|
|
|
19 |
Config::initialize();
|
|
|
20 |
} catch (Exception $e) {
|
|
|
21 |
$d->log('Fatal: ' . $e->getMessage(), \LOG_ALERT);
|
|
|
22 |
exit;
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
$folder = __DIR__;
|
|
|
26 |
$lock_file = fopen($folder . '/nfsen-ng.pid', 'c');
|
|
|
27 |
$got_lock = flock($lock_file, \LOCK_EX | \LOCK_NB, $wouldblock);
|
|
|
28 |
if ($lock_file === false || (!$got_lock && !$wouldblock)) {
|
|
|
29 |
exit(128);
|
|
|
30 |
}
|
|
|
31 |
if (!$got_lock && $wouldblock) {
|
|
|
32 |
exit(129);
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
// Lock acquired; let's write our PID to the lock file for the convenience
|
|
|
36 |
// of humans who may wish to terminate the script.
|
|
|
37 |
ftruncate($lock_file, 0);
|
|
|
38 |
fwrite($lock_file, getmypid() . \PHP_EOL);
|
|
|
39 |
|
|
|
40 |
// first import missed data if available
|
|
|
41 |
$start = new DateTime();
|
|
|
42 |
$start->setDate(date('Y') - 3, (int) date('m'), (int) date('d'));
|
|
|
43 |
$i = new Import();
|
|
|
44 |
$i->setQuiet(false);
|
|
|
45 |
$i->setVerbose(true);
|
|
|
46 |
$i->setProcessPorts(true);
|
|
|
47 |
$i->setProcessPortsBySource(true);
|
|
|
48 |
$i->setCheckLastUpdate(true);
|
|
|
49 |
$i->start($start);
|
|
|
50 |
|
|
|
51 |
$d->log('Starting periodic execution', \LOG_INFO);
|
|
|
52 |
|
|
|
53 |
/* @phpstan-ignore-next-line */
|
|
|
54 |
while (1) {
|
|
|
55 |
// next import in 30 seconds
|
|
|
56 |
sleep(30);
|
|
|
57 |
|
|
|
58 |
// import from last db update
|
|
|
59 |
$i->start($start);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
// all done; blank the PID file and explicitly release the lock
|
|
|
63 |
/* @phpstan-ignore-next-line */
|
|
|
64 |
ftruncate($lock_file, 0);
|
|
|
65 |
flock($lock_file, \LOCK_UN);
|