diff --git a/composer.json b/composer.json index 52fd0820..ec34d395 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "erusev/parsedown": "^1.7", "nikic/fast-route": "^1.3", "matomo/device-detector": "^6.0", - "illuminate/database": "^10.18" + "illuminate/database": "^10.18", + "peppeocchi/php-cron-scheduler": "4.*" }, "require-dev": { "filp/whoops": "^2.15" diff --git a/system/bin/cronjob.php b/system/bin/cronjob.php new file mode 100644 index 00000000..0bea2b03 --- /dev/null +++ b/system/bin/cronjob.php @@ -0,0 +1,19 @@ +<?php + +require_once __DIR__ . '/../../common.php'; +require_once SYSTEM . 'functions.php'; +require_once SYSTEM . 'init.php'; +require_once SYSTEM . 'hooks.php'; + +$hooks = new Hooks(); +$hooks->load(); + +use GO\Scheduler; + +// Create a new scheduler +$scheduler = new Scheduler(); + +$hooks->trigger(HOOK_CRONJOB, ['scheduler' => $scheduler]); + +// Let the scheduler execute jobs which are due. +$scheduler->run(); diff --git a/system/bin/install_cronjob.php b/system/bin/install_cronjob.php new file mode 100644 index 00000000..dff2604a --- /dev/null +++ b/system/bin/install_cronjob.php @@ -0,0 +1,50 @@ +<?php + +require_once __DIR__ . '/../../common.php'; +require_once SYSTEM . 'functions.php'; +require_once SYSTEM . 'init.php'; + +if(!IS_CLI) { + echo 'This script can be run only in command line mode.' . PHP_EOL; + exit(1); +} + +if (MYAAC_OS !== 'LINUX') { + echo 'This script can be run only on linux.' . PHP_EOL; + exit(1); +} + +$job = '* * * * * /usr/bin/php ' . SYSTEM . 'bin/cronjob.php >> ' . SYSTEM . 'logs/cron.log 2>&1'; + +if (cronjob_exists($job)) { + echo 'MyAAC cronjob already installed.' . PHP_EOL; + exit(0); +} + +exec ('crontab -l', $content); + +$content = implode(' ', $content); +$content .= PHP_EOL . $job; + +file_put_contents(CACHE . 'cronjob', $content . PHP_EOL); +exec('crontab ' . CACHE. 'cronjob'); + +echo 'Installed crontab successfully.' . PHP_EOL; + +function cronjob_exists($command) +{ + $cronjob_exists=false; + + exec('crontab -l', $crontab); + if(isset($crontab)&&is_array($crontab)) { + + $crontab = array_flip($crontab); + + if(isset($crontab[$command])){ + $cronjob_exists = true; + } + + } + + return $cronjob_exists; +} diff --git a/system/hooks.php b/system/hooks.php index 0ef6827d..b6480ae4 100644 --- a/system/hooks.php +++ b/system/hooks.php @@ -69,6 +69,7 @@ define('HOOK_ADMIN_LOGIN_AFTER_PASSWORD', ++$i); define('HOOK_ADMIN_LOGIN_AFTER_SIGN_IN', ++$i); define('HOOK_ADMIN_ACCOUNTS_SAVE_POST', ++$i); define('HOOK_ADMIN_SETTINGS_BEFORE_SAVE', ++$i); +define('HOOK_CRONJOB', ++$i); define('HOOK_EMAIL_CONFIRMED', ++$i); define('HOOK_GUILDS_BEFORE_GUILD_HEADER', ++$i); define('HOOK_GUILDS_AFTER_GUILD_HEADER', ++$i);