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,98 +90,139 @@ 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.',
INSERT IGNORE INTO `msz_roles` 'type' => 'sql',
(`role_id`, `role_name`, `role_hierarchy`, `role_colour`, `role_description`, `role_created`) 'run' => $runLowFreq,
VALUES 'command' => "
(1, 'Member', 1, 1073741824, NULL, NOW()) INSERT IGNORE INTO `msz_roles`
", (`role_id`, `role_name`, `role_hierarchy`, `role_colour`, `role_description`, `role_created`)
VALUES
// Ensures all users are in the main role. (1, 'Member', 1, 1073741824, NULL, NOW())
" ",
INSERT INTO `msz_user_roles` ],
(`user_id`, `role_id`) [
SELECT `user_id`, 1 FROM `msz_users` as u 'name' => 'Ensures all users are in the main role.',
WHERE NOT EXISTS ( 'type' => 'sql',
SELECT 1 'run' => $runLowFreq,
FROM `msz_user_roles` as ur 'command' => "
WHERE `role_id` = 1 INSERT INTO `msz_user_roles`
AND u.`user_id` = ur.`user_id` (`user_id`, `role_id`)
) SELECT `user_id`, 1 FROM `msz_users` as u
", WHERE NOT EXISTS (
SELECT 1
// Ensures all display_role values are correct with `msz_user_roles` FROM `msz_user_roles` as ur
" WHERE `role_id` = 1
UPDATE `msz_users` as u AND u.`user_id` = ur.`user_id`
SET `display_role` = ( )
SELECT ur.`role_id` ",
FROM `msz_user_roles` as ur ],
LEFT JOIN `msz_roles` as r [
ON r.`role_id` = ur.`role_id` 'name' => 'Ensures all display_role values are correct with `msz_user_roles`.',
WHERE ur.`user_id` = u.`user_id` 'type' => 'sql',
ORDER BY `role_hierarchy` DESC 'run' => $runLowFreq,
LIMIT 1 'command' => "
) UPDATE `msz_users` as u
WHERE NOT EXISTS ( SET `display_role` = (
SELECT 1 SELECT ur.`role_id`
FROM `msz_user_roles` as ur FROM `msz_user_roles` as ur
WHERE ur.`role_id` = u.`display_role` LEFT JOIN `msz_roles` as r
AND `ur`.`user_id` = u.`user_id` ON r.`role_id` = ur.`role_id`
) WHERE ur.`user_id` = u.`user_id`
", ORDER BY `role_hierarchy` DESC
LIMIT 1
// Deletes expired sessions )
" WHERE NOT EXISTS (
DELETE FROM `msz_sessions` SELECT 1
WHERE `session_expires` < NOW() FROM `msz_user_roles` as ur
", WHERE ur.`role_id` = u.`display_role`
AND `ur`.`user_id` = u.`user_id`
// Remove old password reset records, left for a week for possible review )
" ",
DELETE FROM `msz_users_password_resets` ],
WHERE `reset_requested` < NOW() - INTERVAL 1 WEEK [
", 'name' => 'Remove expired sessions.',
'type' => 'sql',
// Cleans up the login history table 'run' => true,
" 'command' => "
DELETE FROM `msz_login_attempts` DELETE FROM `msz_sessions`
WHERE `attempt_created` < NOW() - INTERVAL 6 MONTH WHERE `session_expires` < NOW()
", ",
],
// Cleans up the audit log table [
" 'name' => 'Remove old password reset records.',
DELETE FROM `msz_audit_log` 'type' => 'sql',
WHERE `log_created` < NOW() - INTERVAL 6 MONTH 'run' => true,
", 'command' => "
DELETE FROM `msz_users_password_resets`
// Delete ignored forum tracking entries WHERE `reset_requested` < NOW() - INTERVAL 1 WEEK
" ",
DELETE tt FROM `msz_forum_topics_track` as tt ],
LEFT JOIN `msz_forum_topics` as t [
ON t.`topic_id` = tt.`topic_id` 'name' => 'Clean up login history.',
WHERE t.`topic_bumped` < NOW() - INTERVAL 1 MONTH 'type' => 'sql',
", 'run' => true,
'command' => "
// Synchronise forum_id DELETE FROM `msz_login_attempts`
" WHERE `attempt_created` < NOW() - INTERVAL 6 MONTH
UPDATE `msz_forum_posts` AS p ",
INNER JOIN `msz_forum_topics` AS t ],
ON t.`topic_id` = p.`topic_id` [
SET p.`forum_id` = t.`forum_id` 'name' => 'Clean up audit log.',
", 'type' => 'sql',
'run' => true,
'command' => "
DELETE FROM `msz_audit_log`
WHERE `log_created` < NOW() - INTERVAL 6 MONTH
",
],
[
'name' => 'Remove stale forum tracking entries.',
'type' => 'sql',
'run' => true,
'command' => "
DELETE tt FROM `msz_forum_topics_track` as tt
LEFT JOIN `msz_forum_topics` as t
ON t.`topic_id` = tt.`topic_id`
WHERE t.`topic_bumped` < NOW() - INTERVAL 1 MONTH
",
],
[
'name' => 'Synchronise forum_id.',
'type' => 'sql',
'run' => $runLowFreq,
'command' => "
UPDATE `msz_forum_posts` AS p
INNER JOIN `msz_forum_topics` AS t
ON t.`topic_id` = p.`topic_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;