Skip to content

Commit 40caa41

Browse files
committed
Fix job loop
1 parent 5a785d1 commit 40caa41

1 file changed

Lines changed: 41 additions & 14 deletions

File tree

src/Cron/CronWorker.php

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,18 @@ private function calculateNextRun($job) {
520520
if (empty($job['interval_seconds'])) {
521521
throw new \Exception("Interval seconds is required for interval schedule type");
522522
}
523-
return time() + $job['interval_seconds'];
523+
524+
// Check if this is the first run (no completed executions)
525+
$sql = "SELECT MAX(completed_at) as last_completed FROM CronJobExecution WHERE cron_job = ? AND status = 'completed'";
526+
$result = DBI::prepared_query($sql, 'i', [$job['id']]);
527+
528+
if (!empty($result) && $result[0]['last_completed'] !== null) {
529+
// Schedule based on last completion
530+
return $result[0]['last_completed'] + $job['interval_seconds'];
531+
} else {
532+
// First run - schedule immediately
533+
return time();
534+
}
524535

525536
case 'daily':
526537
return $this->calculateDailyNextRun($job);
@@ -693,19 +704,35 @@ private function calculateMonthlyNextRun($job) {
693704
* Create new execution record
694705
*/
695706
private function createExecution($job, $scheduledTime) {
696-
$sql = "
697-
INSERT INTO CronJobExecution (
698-
cron_job, scheduled_time, next_run_time, status, application, kyte_account, date_created
699-
) VALUES (?, ?, ?, 'pending', ?, ?, UNIX_TIMESTAMP())
700-
";
701-
702-
DBI::prepared_query($sql, 'iiiii', [
703-
$job['id'],
704-
$scheduledTime,
705-
$scheduledTime,
706-
$job['application'] ?? null,
707-
$job['kyte_account']
708-
]);
707+
// Handle NULL application - can't use 'i' type for NULL in mysqli
708+
if ($job['application'] === null || $job['application'] === '') {
709+
$sql = "
710+
INSERT INTO CronJobExecution (
711+
cron_job, scheduled_time, next_run_time, status, application, kyte_account, date_created
712+
) VALUES (?, ?, ?, 'pending', NULL, ?, UNIX_TIMESTAMP())
713+
";
714+
715+
DBI::prepared_query($sql, 'iiii', [
716+
$job['id'],
717+
$scheduledTime,
718+
$scheduledTime,
719+
$job['kyte_account']
720+
]);
721+
} else {
722+
$sql = "
723+
INSERT INTO CronJobExecution (
724+
cron_job, scheduled_time, next_run_time, status, application, kyte_account, date_created
725+
) VALUES (?, ?, ?, 'pending', ?, ?, UNIX_TIMESTAMP())
726+
";
727+
728+
DBI::prepared_query($sql, 'iiiii', [
729+
$job['id'],
730+
$scheduledTime,
731+
$scheduledTime,
732+
$job['application'],
733+
$job['kyte_account']
734+
]);
735+
}
709736
}
710737

711738
/**

0 commit comments

Comments
 (0)