Run some cron tasks at a lower frequency (lofreq are run once an hour, hifreq every 10 minutes).

This commit is contained in:
flash 2019-02-26 16:05:05 +01:00
parent 5cabd79702
commit 4761c9e9f9
2 changed files with 129 additions and 88 deletions

View file

@ -31,7 +31,7 @@
"scripts": { "scripts": {
"post-install-cmd": [ "post-install-cmd": [
"php misuzu.php migrate", "php misuzu.php migrate",
"php misuzu.php cron" "php misuzu.php cron low"
] ]
}, },
"config": { "config": {

View file

@ -90,20 +90,25 @@ if (PHP_SAPI === 'cli') {
if (realpath($_SERVER['SCRIPT_FILENAME']) === __FILE__) { if (realpath($_SERVER['SCRIPT_FILENAME']) === __FILE__) {
switch ($argv[1] ?? null) { switch ($argv[1] ?? null) {
case 'cron': case 'cron':
$cronFuncs = [ $runLowFreq = (bool)(!empty($argv[2]) && $argv[2] == 'low');
'forum_count_synchronise',
];
$cronTasks = [ $cronTasks = [
// Ensure main role exists. [
" 'name' => 'Ensures main role exists.',
'type' => 'sql',
'run' => $runLowFreq,
'command' => "
INSERT IGNORE INTO `msz_roles` INSERT IGNORE INTO `msz_roles`
(`role_id`, `role_name`, `role_hierarchy`, `role_colour`, `role_description`, `role_created`) (`role_id`, `role_name`, `role_hierarchy`, `role_colour`, `role_description`, `role_created`)
VALUES VALUES
(1, 'Member', 1, 1073741824, NULL, NOW()) (1, 'Member', 1, 1073741824, NULL, NOW())
", ",
],
// Ensures all users are in the main role. [
" 'name' => 'Ensures all users are in the main role.',
'type' => 'sql',
'run' => $runLowFreq,
'command' => "
INSERT INTO `msz_user_roles` INSERT INTO `msz_user_roles`
(`user_id`, `role_id`) (`user_id`, `role_id`)
SELECT `user_id`, 1 FROM `msz_users` as u SELECT `user_id`, 1 FROM `msz_users` as u
@ -114,9 +119,12 @@ if (PHP_SAPI === 'cli') {
AND u.`user_id` = ur.`user_id` AND u.`user_id` = ur.`user_id`
) )
", ",
],
// Ensures all display_role values are correct with `msz_user_roles` [
" 'name' => 'Ensures all display_role values are correct with `msz_user_roles`.',
'type' => 'sql',
'run' => $runLowFreq,
'command' => "
UPDATE `msz_users` as u UPDATE `msz_users` as u
SET `display_role` = ( SET `display_role` = (
SELECT ur.`role_id` SELECT ur.`role_id`
@ -134,54 +142,87 @@ if (PHP_SAPI === 'cli') {
AND `ur`.`user_id` = u.`user_id` AND `ur`.`user_id` = u.`user_id`
) )
", ",
],
// Deletes expired sessions [
" 'name' => 'Remove expired sessions.',
'type' => 'sql',
'run' => true,
'command' => "
DELETE FROM `msz_sessions` DELETE FROM `msz_sessions`
WHERE `session_expires` < NOW() WHERE `session_expires` < NOW()
", ",
],
// Remove old password reset records, left for a week for possible review [
" 'name' => 'Remove old password reset records.',
'type' => 'sql',
'run' => true,
'command' => "
DELETE FROM `msz_users_password_resets` DELETE FROM `msz_users_password_resets`
WHERE `reset_requested` < NOW() - INTERVAL 1 WEEK WHERE `reset_requested` < NOW() - INTERVAL 1 WEEK
", ",
],
// Cleans up the login history table [
" 'name' => 'Clean up login history.',
'type' => 'sql',
'run' => true,
'command' => "
DELETE FROM `msz_login_attempts` DELETE FROM `msz_login_attempts`
WHERE `attempt_created` < NOW() - INTERVAL 6 MONTH WHERE `attempt_created` < NOW() - INTERVAL 6 MONTH
", ",
],
// Cleans up the audit log table [
" 'name' => 'Clean up audit log.',
'type' => 'sql',
'run' => true,
'command' => "
DELETE FROM `msz_audit_log` DELETE FROM `msz_audit_log`
WHERE `log_created` < NOW() - INTERVAL 6 MONTH WHERE `log_created` < NOW() - INTERVAL 6 MONTH
", ",
],
// Delete ignored forum tracking entries [
" 'name' => 'Remove stale forum tracking entries.',
'type' => 'sql',
'run' => true,
'command' => "
DELETE tt FROM `msz_forum_topics_track` as tt DELETE tt FROM `msz_forum_topics_track` as tt
LEFT JOIN `msz_forum_topics` as t LEFT JOIN `msz_forum_topics` as t
ON t.`topic_id` = tt.`topic_id` ON t.`topic_id` = tt.`topic_id`
WHERE t.`topic_bumped` < NOW() - INTERVAL 1 MONTH WHERE t.`topic_bumped` < NOW() - INTERVAL 1 MONTH
", ",
],
// Synchronise forum_id [
" 'name' => 'Synchronise forum_id.',
'type' => 'sql',
'run' => $runLowFreq,
'command' => "
UPDATE `msz_forum_posts` AS p UPDATE `msz_forum_posts` AS p
INNER JOIN `msz_forum_topics` AS t INNER JOIN `msz_forum_topics` AS t
ON t.`topic_id` = p.`topic_id` ON t.`topic_id` = p.`topic_id`
SET p.`forum_id` = t.`forum_id` SET p.`forum_id` = t.`forum_id`
", ",
],
[
'name' => 'Recount forum topics and posts.',
'type' => 'func',
'run' => $runLowFreq,
'command' => 'forum_count_synchronise',
],
]; ];
foreach ($cronTasks as $cronTask) { foreach ($cronTasks as $cronTask) {
db_exec($cronTask); if ($cronTask['run']) {
} echo $cronTask['name'] . PHP_EOL;
foreach ($cronFuncs as $cronTask) { switch ($cronTask['type']) {
call_user_func($cronTask); case 'sql':
db_exec($cronTask['command']);
break;
case 'func':
call_user_func($cronTask['command']);
break;
}
}
} }
break; break;