-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcron.php
More file actions
152 lines (127 loc) · 4.54 KB
/
cron.php
File metadata and controls
152 lines (127 loc) · 4.54 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
declare(strict_types=1);
require_once __DIR__ . '/autoload.php';
use Lotgd\BootstrapErrorHandler;
use Lotgd\ErrorHandler;
use Lotgd\Mail;
use Lotgd\Newday;
use Lotgd\Settings;
define('CRON_NEWDAY', 1);
define('CRON_DBCLEANUP', 2);
define('CRON_COMMENTCLEANUP', 4);
define('CRON_CHARCLEANUP', 8);
define("ALLOW_ANONYMOUS", true);
BootstrapErrorHandler::register();
$result = chdir(__DIR__);
if (!defined('CRON_TEST')) {
try {
require_once __DIR__ . '/common.php';
} catch (\Throwable $e) {
$message = sprintf(
'[%s] Cron common.php failure: %s in %s on line %d%s',
date('c'),
$e->getMessage(),
$e->getFile(),
$e->getLine(),
PHP_EOL
);
error_log($message, 3, __DIR__ . '/logs/bootstrap.log');
if (! Settings::hasInstance()) {
exit(1);
}
$settingsInstance = Settings::getInstance();
$email = $settingsInstance->getSetting('gameadminemail', '');
if ($email === '') {
exit(1);
}
$body = sprintf(
'Cronjob at %s failed to load common.php: %s',
$settingsInstance->getSetting('serverurl', ''),
$e->getMessage()
);
$mailResult = Mail::send([$email => $email], $body, 'Cronjob Error', [$email => $email], false, 'text/plain', true);
if (is_array($mailResult) && ! $mailResult['success']) {
error_log('Cron notification mail failed: ' . $mailResult['error']);
} elseif ($mailResult === false) {
error_log('Cron notification mail failed to send.');
}
exit(1);
}
}
ErrorHandler::register();
$cron_args = $_SERVER['argv'] ?? [];
if (! is_array($cron_args)) { // Handles web-triggered cron runs or register_argc_argv=Off
if (! defined('CRON_TEST')) {
error_log('[cron] Cron invoked without CLI arguments; check cron job configuration.');
}
$cron_args = [];
} else {
array_shift($cron_args);
}
if (count($cron_args) < 1) {
$usage = <<<'TXT'
Usage: php cron.php <execution_bitmask> [force_db]
Provide an integer bitmask combining:
1 (CRON_NEWDAY) Run the daily game reset
2 (CRON_DBCLEANUP) Perform database maintenance
4 (CRON_COMMENTCLEANUP) Trim commentary tables
8 (CRON_CHARCLEANUP) Remove expired characters
Set [force_db] to 1 to force database optimization even if not scheduled.
TXT;
if (! defined('CRON_TEST')) {
fwrite(STDERR, $usage . PHP_EOL);
exit(1);
}
echo $usage . PHP_EOL;
return;
}
// Write in the first argument the style - the defines above will guide your way.
$executionstyle = (int) $cron_args[0];
if (!$result) {
//ERROR, could not change the directory or directory empty
$email = $settings->getSetting('gameadminemail', '');
if ($email === '') {
if (!defined('CRON_TEST')) {
exit(0); //well, we can't go further
}
return;
}
$body = sprintf(
"Sorry, but the gamedir is not set for your cronjob setup at your game at %s.\n\nPlease correct the error or you will have *NO* server newdays.",
$settings->getSetting('serverurl', '')
);
$mailResult = Mail::send([$email => $email], $body, 'Cronjob Setup Screwed', [$email => $email], false, 'text/plain', true);
if (is_array($mailResult) && ! $mailResult['success']) {
error_log('Cron setup warning mail failed: ' . $mailResult['error']);
} elseif ($mailResult === false) {
error_log('Cron setup warning mail failed to send.');
}
if (!defined('CRON_TEST')) {
exit(0); //that's it.
}
return;
}
/* Prevent execution if no value has been entered... if it is a wrong value, it will still break!*/
if ($result) {
$settings->saveSetting('newdaySemaphore', gmdate('Y-m-d H:i:s'));
if ($executionstyle & CRON_NEWDAY) {
Newday::runOnce();
}
if ($executionstyle & CRON_DBCLEANUP) {
//db optimization every day, I think we should leave it here
//edit: we may force this issue by setting the second argument to 1 in the commandline
$force_db = 0;
if (isset($cron_args[1])) {
$force_db = (((int)$cron_args[1]) ? 1 : 0);
}
if (strtotime($settings->getSetting('lastdboptimize', date('Y-m-d H:i:s', strtotime('-1 day')))) < strtotime('-1 day') || $force_db) {
Newday::dbCleanup();
}
}
if ($executionstyle & CRON_COMMENTCLEANUP) {
Newday::commentCleanup();
}
if ($executionstyle & CRON_CHARCLEANUP) {
Newday::charCleanup();
}
}