Recompute next_run_at when re-enabling a paused recurring schedule (#89)
toggleActive() flipped is_active without touching next_run_at. If a schedule was disabled while next_run_at was still in the future, then re-enabled after that date had passed, the next cron tick saw next_run_at <= NOW() and fired immediately — surprising for an admin expecting a re-enabled "daily" schedule to wait until its next natural occurrence. Now recomputes next_run_at from the current time when transitioning to active, matching what a fresh schedule creation would produce; disabling is unchanged. Verified against a local MariaDB instance: re-enabling a schedule whose next_run_at was in 2020 recomputed it to tomorrow at the scheduled time; disabling leaves next_run_at untouched. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MGDKHiU5RJdo3dqQUDow3X
This commit is contained in:
@@ -231,9 +231,33 @@ class RecurringTicketModel
|
|||||||
*/
|
*/
|
||||||
public function toggleActive($recurringId)
|
public function toggleActive($recurringId)
|
||||||
{
|
{
|
||||||
$sql = "UPDATE recurring_tickets SET is_active = NOT is_active WHERE recurring_id = ?";
|
$recurring = $this->getById($recurringId);
|
||||||
$stmt = $this->conn->prepare($sql);
|
if (!$recurring) {
|
||||||
$stmt->bind_param('i', $recurringId);
|
return ['success' => false];
|
||||||
|
}
|
||||||
|
|
||||||
|
$newActive = $recurring['is_active'] ? 0 : 1;
|
||||||
|
|
||||||
|
if ($newActive) {
|
||||||
|
// Re-enabling: recompute next_run_at from now, as if the schedule
|
||||||
|
// were freshly created. Otherwise a schedule paused while
|
||||||
|
// next_run_at was still in the future, then re-enabled after that
|
||||||
|
// date has passed, would fire immediately on the next cron tick
|
||||||
|
// instead of waiting for its next natural occurrence.
|
||||||
|
$nextRun = $this->calculateNextRunTime(
|
||||||
|
$recurring['schedule_type'],
|
||||||
|
$recurring['schedule_day'],
|
||||||
|
$recurring['schedule_time']
|
||||||
|
);
|
||||||
|
$sql = "UPDATE recurring_tickets SET is_active = ?, next_run_at = ? WHERE recurring_id = ?";
|
||||||
|
$stmt = $this->conn->prepare($sql);
|
||||||
|
$stmt->bind_param('isi', $newActive, $nextRun, $recurringId);
|
||||||
|
} else {
|
||||||
|
$sql = "UPDATE recurring_tickets SET is_active = ? WHERE recurring_id = ?";
|
||||||
|
$stmt = $this->conn->prepare($sql);
|
||||||
|
$stmt->bind_param('ii', $newActive, $recurringId);
|
||||||
|
}
|
||||||
|
|
||||||
$success = $stmt->execute();
|
$success = $stmt->execute();
|
||||||
$stmt->close();
|
$stmt->close();
|
||||||
return ['success' => $success];
|
return ['success' => $success];
|
||||||
|
|||||||
Reference in New Issue
Block a user