Commit af73d3c0 authored by Administrator's avatar Administrator

Update 7 files via Son of Anton

parent 050451a0
Pipeline #22 failed with stage
......@@ -5,6 +5,5 @@ namespace Engine\Scheduler;
interface JobInterface
{
public function execute(): void;
public function nextRunAt(): string;
public function run(): void;
}
\ No newline at end of file
......@@ -31,16 +31,30 @@ final class JobRunner
$key = $jobRecord['job_key'];
if (!isset($this->jobs[$key])) continue;
// Lock check
if ($jobRecord['last_status'] === 'running') continue;
$this->db->update('background_jobs', ['last_status' => 'running'], 'id = ?', [$jobRecord['id']]);
try {
$this->jobs[$key]->execute();
$job = $this->jobs[$key];
// Check shouldRun() if method exists
if (method_exists($job, 'shouldRun') && !$job->shouldRun()) {
$this->db->update('background_jobs', [
'last_run_at' => date('Y-m-d H:i:s'),
'next_run_at' => $this->jobs[$key]->nextRunAt(),
'next_run_at' => $this->getNextRunAt($job),
'last_status' => 'success',
'last_error' => null,
], 'id = ?', [$jobRecord['id']]);
$results[$key] = 'skipped (shouldRun=false)';
continue;
}
$job->run();
$this->db->update('background_jobs', [
'last_run_at' => date('Y-m-d H:i:s'),
'next_run_at' => $this->getNextRunAt($job),
'last_status' => 'success',
'last_error' => null,
], 'id = ?', [$jobRecord['id']]);
......@@ -48,7 +62,7 @@ final class JobRunner
} catch (\Throwable $e) {
$this->db->update('background_jobs', [
'last_run_at' => date('Y-m-d H:i:s'),
'next_run_at' => $this->jobs[$key]->nextRunAt(),
'next_run_at' => $this->getNextRunAt($this->jobs[$key]),
'last_status' => 'failed',
'last_error' => $e->getMessage(),
], 'id = ?', [$jobRecord['id']]);
......@@ -59,6 +73,15 @@ final class JobRunner
return $results;
}
private function getNextRunAt(JobInterface $job): string
{
if (method_exists($job, 'nextRunAt')) {
return $job->nextRunAt();
}
// Default: run again in 1 hour
return date('Y-m-d H:i:s', strtotime('+1 hour'));
}
public function initializeJob(string $key): void
{
$exists = $this->db->fetchOne("SELECT id FROM background_jobs WHERE job_key = ?", [$key]);
......
......@@ -16,13 +16,8 @@ final class SessionCleanupJob implements JobInterface
$this->sessions = Container::getInstance()->resolve(SessionManager::class);
}
public function execute(): void
public function run(): void
{
$this->sessions->cleanup();
}
public function nextRunAt(): string
{
return date('Y-m-d H:i:s', strtotime('+1 day'));
}
}
\ No newline at end of file
......@@ -16,7 +16,7 @@ final class AutoArchiveDoneCardsJob implements JobInterface
$this->db = Container::getInstance()->resolve(Connection::class);
}
public function execute(): void
public function run(): void
{
$boards = $this->db->fetchAll("SELECT id, auto_archive_done_days FROM boards WHERE is_archived = 0");
......@@ -33,9 +33,4 @@ final class AutoArchiveDoneCardsJob implements JobInterface
);
}
}
public function nextRunAt(): string
{
return date('Y-m-d 03:00:00', strtotime('+1 day'));
}
}
\ No newline at end of file
......@@ -20,9 +20,8 @@ final class SendDeadlineRemindersJob implements JobInterface
$this->notif = $c->resolve(NotificationManager::class);
}
public function execute(): void
public function run(): void
{
// Cards due in 2 days
$twoDays = date('Y-m-d', strtotime('+2 days'));
$cards = $this->db->fetchAll(
"SELECT c.id, c.card_key, c.title, c.deadline FROM cards c
......@@ -42,7 +41,6 @@ final class SendDeadlineRemindersJob implements JobInterface
}
}
// Cards due today
$today = date('Y-m-d');
$todayCards = $this->db->fetchAll(
"SELECT c.id, c.card_key, c.title FROM cards c
......@@ -62,9 +60,4 @@ final class SendDeadlineRemindersJob implements JobInterface
}
}
}
public function nextRunAt(): string
{
return date('Y-m-d 08:00:00', strtotime('+1 day'));
}
}
\ No newline at end of file
......@@ -16,15 +16,10 @@ final class InviteExpiryJob implements JobInterface
$this->db = Container::getInstance()->resolve(Connection::class);
}
public function execute(): void
public function run(): void
{
$this->db->query(
"UPDATE invites SET status = 'expired' WHERE status = 'active' AND expires_at < NOW()"
);
}
public function nextRunAt(): string
{
return date('Y-m-d 00:05:00', strtotime('+1 day'));
}
}
\ No newline at end of file
......@@ -20,12 +20,11 @@ final class DetectUnreportedDaysJob implements JobInterface
$this->notif = $c->resolve(NotificationManager::class);
}
public function execute(): void
public function run(): void
{
$yesterday = date('Y-m-d', strtotime('-1 day'));
$dayOfWeek = (int)date('w', strtotime($yesterday));
// Get all active contractors
$contractors = $this->db->fetchAll(
"SELECT u.id, u.full_name_en FROM users u WHERE u.role = 'contractor' AND u.status = 'active'"
);
......@@ -33,42 +32,36 @@ final class DetectUnreportedDaysJob implements JobInterface
foreach ($contractors as $contractor) {
$userId = $contractor['id'];
// Check if yesterday was a working day
$isWorkDay = $this->db->fetchOne(
"SELECT 1 FROM user_schedule_days WHERE user_id = ? AND day_of_week = ? AND work_mode != 'off' AND effective_to IS NULL",
[$userId, $dayOfWeek]
);
if (!$isWorkDay) continue;
// Check holiday
$isHoliday = $this->db->fetchOne(
"SELECT 1 FROM holidays WHERE start_date <= ? AND end_date >= ?",
[$yesterday, $yesterday]
);
if ($isHoliday) continue;
// Check unavailability
$isUnavailable = $this->db->fetchOne(
"SELECT 1 FROM unavailability_records WHERE user_id = ? AND start_date <= ? AND end_date >= ?",
[$userId, $yesterday, $yesterday]
);
if ($isUnavailable) continue;
// Check if report exists
$hasReport = $this->db->fetchOne(
"SELECT 1 FROM daily_reports WHERE user_id = ? AND report_date = ?",
[$userId, $yesterday]
);
if ($hasReport) continue;
// Create unreported record
$this->db->insert('daily_reports', [
'user_id' => $userId,
'report_date' => $yesterday,
'status' => 'unreported',
]);
// Notify contractor
$this->notif->createImportant(
$userId,
'Unreported Day',
......@@ -76,9 +69,4 @@ final class DetectUnreportedDaysJob implements JobInterface
);
}
}
public function nextRunAt(): string
{
return date('Y-m-d 01:00:00', strtotime('+1 day'));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment