Files
api/cron/Cron.php
T
Jepp9350 d3e9391160 Add new cron tasks and enhance user synchronization logic
Introduced cron tasks for syncing user economic customer details and discounts. Enhanced caching mechanisms for users and departments, added Slack messaging capabilities, and updated CLI/script routing for better flexibility. New routes and utility methods improve cron execution and logging.
2025-01-24 13:04:10 +01:00

97 lines
2.4 KiB
PHP

<?php
// prevent direct access
use objects\bookings_o;
use objects\logs_o;
use objects\users_o;
if (!defined('WD')) {
exit;
}
// Define the warning function
function warn($message): void
{
echo "\n\033[33m$message\033[0m\n";
}
$response_cron = [];
// Define the cron tasks
$cron_tasks = [
'CheckUnfulfilledBookings' => [
'interval' => 86400, // 24 hours
'last_run' => 0,
'next_run' => 0,
'time' => '15:00',
'function' => 'checkUnfulfilledBookings',
],
'SyncBookings' => [
'interval' => 60, // 1 minute
'last_run' => 0,
'next_run' => 0,
'function' => 'syncBookings',
],
'SyncLogs' => [
'interval' => 300, // 5 minutes
'last_run' => 0,
'next_run' => 0,
'function' => 'syncLogsToDatabase',
],
'SyncUserEconomicCustomerDiscounts' => [
'interval' => 600, // 10 minutes
'last_run' => 0,
'next_run' => 0,
'function' => 'SyncUserEconomicCustomerDiscounts',
],
'SyncUserEconomicCustomerDetails' => [
'interval' => 43200, // 12 hours
'last_run' => 0,
'next_run' => 0,
'function' => 'SyncUserEconomicCustomerDetails',
],
];
function checkUnfulfilledBookings(): void
{
$bookings_o = new bookings_o();
$bookings_o->checkUnfulfilledBookings();
}
function syncBookings(): void
{
$bookings_o = new bookings_o();
$response_cron[] = $bookings_o->syncBookings();
}
function syncLogsToDatabase(): void
{
$logs_o = new logs_o();
$logs_o->syncLogsToDatabase();
}
function SyncUserEconomicCustomerDiscounts(): void
{
$users_o = new users_o();
$users_o->clearAllUsersEconomicCustomerDiscountsFromCache();
}
function SyncUserEconomicCustomerDetails(): void
{
$users_o = new users_o();
$users_o->clearAllUsersEconomicCustomerDetailsFromCache();
$users_o->syncAllUsersEconomicCustomerDetails();
}
foreach ( $cron_tasks as $task => $data ) {
$lastRun = redis->get_last_crond_run($task) === null ? 0 : redis->get_last_crond_run($task);
$nextRun = $lastRun + $data['interval'];
$cron_tasks[$task]['last_run'] = $lastRun;
$cron_tasks[$task]['next_run'] = $nextRun;
if ($nextRun <= time()) {
$data['function']();
redis->set_last_crond_run($task, time());
} else {
$response_cron[] = $task . ' is not due to run yet, next run is at ' . date('Y-m-d H:i:s', $nextRun) . ' (' . ($nextRun - time()) . ' seconds)';
}
}