From d2d7b6805e7fedfdf35c15e06b41ae375cb563a1 Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Fri, 6 Oct 2017 17:00:31 +0300 Subject: [PATCH] Checking if Scheduled Jobs were run --- application/Espo/Core/Utils/ScheduledJob.php | 17 ++++++--- application/Espo/Jobs/Dummy.php | 38 +++++++++++++++++++ .../metadata/entityDefs/ScheduledJob.json | 6 +++ install/core/Installer.php | 8 ++-- install/core/afterInstall/queries.php | 2 +- 5 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 application/Espo/Jobs/Dummy.php diff --git a/application/Espo/Core/Utils/ScheduledJob.php b/application/Espo/Core/Utils/ScheduledJob.php index aa55e27a46..9907cefa5e 100644 --- a/application/Espo/Core/Utils/ScheduledJob.php +++ b/application/Espo/Core/Utils/ScheduledJob.php @@ -208,14 +208,18 @@ class ScheduledJob */ public function isCronConfigured() { - $date = new \DateTime('-' . $this->lastCronRunTime, new \DateTimeZone("UTC")); + $nowDate = new \DateTime('now', new \DateTimeZone("UTC")); + $startDate = new \DateTime('-' . $this->lastCronRunTime, new \DateTimeZone("UTC")); $query = " - SELECT COUNT(id) as count FROM job + SELECT job.id FROM scheduled_job + LEFT JOIN job ON job.scheduled_job_id = scheduled_job.id AND job.deleted = 0 WHERE - deleted = 0 - AND `status` IN ('Success', 'Failed') - AND execute_time > '" . $date->format('Y-m-d H:i:s') . "' + scheduled_job.job = 'Dummy' + AND scheduled_job.deleted = 0 + AND job.execute_time BETWEEN '". $startDate->format('Y-m-d H:i:s') ."' AND '". $nowDate->format('Y-m-d H:i:s') ."' + AND (job.status IN ('Success', 'Failed') OR (job.status = 'Pending' AND job.execute_time >= '". $nowDate->modify('-1 hour')->format('Y-m-d H:i:s') ."') ) + ORDER BY job.execute_time DESC "; $pdo = $this->getEntityManager()->getPDO(); @@ -223,7 +227,8 @@ class ScheduledJob $sth->execute(); $row = $sth->fetch(\PDO::FETCH_ASSOC); - if (isset($row['count']) && $row['count'] >= 1) { + + if (!empty($row['id'])) { return true; } diff --git a/application/Espo/Jobs/Dummy.php b/application/Espo/Jobs/Dummy.php new file mode 100644 index 0000000000..f4d9580b8f --- /dev/null +++ b/application/Espo/Jobs/Dummy.php @@ -0,0 +1,38 @@ +app->getContainer()->get('schema')->rebuild(); + $result = $this->app->getContainer()->get('dataManager')->rebuild(); } catch (\Exception $e) { $this->auth(); - $result = $this->app->getContainer()->get('schema')->rebuild(); + $result = $this->app->getContainer()->get('dataManager')->rebuild(); } return $result; @@ -451,6 +451,4 @@ class Installer return $result; } - - -} +} \ No newline at end of file diff --git a/install/core/afterInstall/queries.php b/install/core/afterInstall/queries.php index 6a730aa9d9..8c584b37ed 100644 --- a/install/core/afterInstall/queries.php +++ b/install/core/afterInstall/queries.php @@ -28,5 +28,5 @@ ************************************************************************/ return array( - + "INSERT INTO `job` (`id`, `name`, `deleted`, `status`, `execute_time`, `service_name`, `method`, `data`, `attempts`, `failed_attempts`, `created_at`, `modified_at`, `scheduled_job_id`, `target_id`, `target_type`) VALUES ('". uniqid() ."', 'Dummy', '0', 'Pending', '" . gmdate('Y-m-d H:i:s') . "', NULL, 'Dummy', NULL, '3', NULL, '" . gmdate('Y-m-d H:i:s') . "', '" . gmdate('Y-m-d H:i:s') . "', (SELECT id FROM scheduled_job WHERE deleted = 0 AND job = 'Dummy'), NULL, NULL);" ); \ No newline at end of file