From 63dac34b0f8dfb296249896b5b77da7233268bd0 Mon Sep 17 00:00:00 2001 From: raviks789 Date: Tue, 4 Feb 2025 14:07:03 +0100 Subject: [PATCH 1/2] `director_job`: Change timestamp data types to integer --- schema/mysql-migrations/upgrade_189.sql | 17 +++++++++++++++++ schema/mysql.sql | 6 +++--- schema/pgsql-migrations/upgrade_189.sql | 18 ++++++++++++++++++ schema/pgsql.sql | 6 +++--- 4 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 schema/mysql-migrations/upgrade_189.sql create mode 100644 schema/pgsql-migrations/upgrade_189.sql diff --git a/schema/mysql-migrations/upgrade_189.sql b/schema/mysql-migrations/upgrade_189.sql new file mode 100644 index 00000000..c8b91933 --- /dev/null +++ b/schema/mysql-migrations/upgrade_189.sql @@ -0,0 +1,17 @@ +ALTER TABLE director_job ADD COLUMN ts_last_attempt_tmp BIGINT(20) DEFAULT NULL; +ALTER TABLE director_job ADD COLUMN ts_last_error_tmp BIGINT(20) DEFAULT NULL; + + +UPDATE director_job +SET ts_last_attempt_tmp = UNIX_TIMESTAMP(ts_last_attempt) * 1000, + ts_last_error_tmp = UNIX_TIMESTAMP(ts_last_error) * 1000; + +ALTER TABLE director_job + DROP COLUMN ts_last_attempt, + DROP COLUMN ts_last_error, + CHANGE ts_last_attempt_tmp ts_last_attempt BIGINT(20) DEFAULT NULL, + CHANGE ts_last_error_tmp ts_last_error BIGINT(20) DEFAULT NULL; + +INSERT INTO director_schema_migration +(schema_version, migration_time) +VALUES (189, NOW()); \ No newline at end of file diff --git a/schema/mysql.sql b/schema/mysql.sql index dd8a0f84..8052ecde 100644 --- a/schema/mysql.sql +++ b/schema/mysql.sql @@ -347,8 +347,8 @@ CREATE TABLE director_job ( run_interval INT(10) UNSIGNED NOT NULL, -- seconds timeperiod_id INT(10) UNSIGNED DEFAULT NULL, last_attempt_succeeded ENUM('y', 'n') DEFAULT NULL, - ts_last_attempt TIMESTAMP NULL DEFAULT NULL, - ts_last_error TIMESTAMP NULL DEFAULT NULL, + ts_last_attempt BIGINT(20) NULL DEFAULT NULL, + ts_last_error BIGINT(20) NULL DEFAULT NULL, last_error_message TEXT DEFAULT NULL, PRIMARY KEY (id), UNIQUE KEY (job_name), @@ -2446,4 +2446,4 @@ CREATE TABLE branched_icinga_dependency ( INSERT INTO director_schema_migration (schema_version, migration_time) - VALUES (188, NOW()); + VALUES (189, NOW()); diff --git a/schema/pgsql-migrations/upgrade_189.sql b/schema/pgsql-migrations/upgrade_189.sql new file mode 100644 index 00000000..6d6fdadf --- /dev/null +++ b/schema/pgsql-migrations/upgrade_189.sql @@ -0,0 +1,18 @@ +ALTER TABLE director_job ADD COLUMN ts_last_attempt_tmp bigint DEFAULT NULL; +ALTER TABLE director_job ADD COLUMN ts_last_error_tmp bigint DEFAULT NULL; + + +UPDATE director_job +SET ts_last_attempt_tmp = UNIX_TIMESTAMP(ts_last_attempt) * 1000, + ts_last_error_tmp = UNIX_TIMESTAMP(ts_last_error) * 1000; + +ALTER TABLE director_job + DROP COLUMN ts_last_attempt, + DROP COLUMN ts_last_error; + +ALTER TABLE director_job RENAME COLUMN ts_last_attempt_tmp TO ts_last_attempt; +ALTER TABLE director_job RENAME COLUMN ts_last_error_tmp TO ts_last_error; + +INSERT INTO director_schema_migration + (schema_version, migration_time) + VALUES (189, NOW()); diff --git a/schema/pgsql.sql b/schema/pgsql.sql index 694d8c90..c9f8183d 100644 --- a/schema/pgsql.sql +++ b/schema/pgsql.sql @@ -448,8 +448,8 @@ CREATE TABLE director_job ( run_interval integer NOT NULL, -- seconds timeperiod_id integer DEFAULT NULL, last_attempt_succeeded enum_boolean DEFAULT NULL, - ts_last_attempt timestamp with time zone DEFAULT NULL, - ts_last_error timestamp with time zone DEFAULT NULL, + ts_last_attempt bigint DEFAULT NULL, + ts_last_error bigint DEFAULT NULL, last_error_message text NULL DEFAULT NULL, CONSTRAINT director_job_period FOREIGN KEY (timeperiod_id) @@ -2781,4 +2781,4 @@ CREATE INDEX branched_dependency_search_object_name ON branched_icinga_dependenc INSERT INTO director_schema_migration (schema_version, migration_time) - VALUES (187, NOW()); + VALUES (189, NOW()); From 4c283a24c8794975f0580be035a97a1555f9a2d1 Mon Sep 17 00:00:00 2001 From: raviks789 Date: Tue, 4 Feb 2025 15:43:18 +0100 Subject: [PATCH 2/2] Fix: Support data type change of columns `ts_last_attempt` and `ts_last_error` from timestamp to bigint --- library/Director/Objects/DirectorJob.php | 14 +++++++++----- library/Director/Web/Table/JobTable.php | 6 +++--- library/Director/Web/Widget/JobDetails.php | 2 +- schema/mysql-migrations/upgrade_189.sql | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/library/Director/Objects/DirectorJob.php b/library/Director/Objects/DirectorJob.php index aadcabc0..a4a9eb61 100644 --- a/library/Director/Objects/DirectorJob.php +++ b/library/Director/Objects/DirectorJob.php @@ -3,6 +3,7 @@ namespace Icinga\Module\Director\Objects; use Icinga\Exception\NotFoundError; +use Icinga\Module\Director\Daemon\DaemonUtil; use Icinga\Module\Director\Daemon\Logger; use Icinga\Module\Director\Data\Db\DbObjectWithSettings; use Icinga\Module\Director\Db; @@ -84,7 +85,8 @@ class DirectorJob extends DbObjectWithSettings implements ExportInterface, Insta public function run() { $job = $this->getInstance(); - $this->set('ts_last_attempt', date('Y-m-d H:i:s')); + $currentTimestamp = DaemonUtil::timestampWithMilliseconds(); + $this->set('ts_last_attempt', $currentTimestamp); try { $job->run(); @@ -92,7 +94,7 @@ class DirectorJob extends DbObjectWithSettings implements ExportInterface, Insta $success = true; } catch (Exception $e) { Logger::error($e->getMessage()); - $this->set('ts_last_error', date('Y-m-d H:i:s')); + $this->set('ts_last_error', $currentTimestamp); $this->set('last_error_message', $e->getMessage()); $this->set('last_attempt_succeeded', 'n'); $success = false; @@ -127,8 +129,8 @@ class DirectorJob extends DbObjectWithSettings implements ExportInterface, Insta } return ( - strtotime($this->get('ts_last_attempt')) + $this->get('run_interval') * 2 - ) < time(); + $this->get('ts_last_attempt') + $this->get('run_interval') * 2 * 1000 + ) < DaemonUtil::timestampWithMilliseconds(); } public function hasBeenDisabled() @@ -145,7 +147,9 @@ class DirectorJob extends DbObjectWithSettings implements ExportInterface, Insta return $this->isWithinTimeperiod(); } - if (strtotime($this->get('ts_last_attempt')) + $this->get('run_interval') < time()) { + if ( + $this->get('ts_last_attempt') + $this->get('run_interval') * 1000 < DaemonUtil::timestampWithMilliseconds() + ) { return $this->isWithinTimeperiod(); } diff --git a/library/Director/Web/Table/JobTable.php b/library/Director/Web/Table/JobTable.php index 81ba07b8..c9b3e85e 100644 --- a/library/Director/Web/Table/JobTable.php +++ b/library/Director/Web/Table/JobTable.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Web\Table; use gipfl\IcingaWeb2\Link; use gipfl\IcingaWeb2\Table\ZfQueryBasedTable; +use Icinga\Module\Director\Daemon\DaemonUtil; class JobTable extends ZfQueryBasedTable { @@ -37,11 +38,11 @@ class JobTable extends ZfQueryBasedTable protected function getJobClasses($row) { - if ($row->unixts_last_attempt === null) { + if ($row->ts_last_attempt === null) { return 'pending'; } - if ($row->unixts_last_attempt + $row->run_interval < time()) { + if ($row->ts_last_attempt + $row->run_interval * 1000 < DaemonUtil::timestampWithMilliseconds()) { return 'pending'; } @@ -73,7 +74,6 @@ class JobTable extends ZfQueryBasedTable 'run_interval' => 'j.run_interval', 'last_attempt_succeeded' => 'j.last_attempt_succeeded', 'ts_last_attempt' => 'j.ts_last_attempt', - 'unixts_last_attempt' => 'UNIX_TIMESTAMP(j.ts_last_attempt)', 'ts_last_error' => 'j.ts_last_error', 'last_error_message' => 'j.last_error_message', ] diff --git a/library/Director/Web/Widget/JobDetails.php b/library/Director/Web/Widget/JobDetails.php index 3a530a25..b794d378 100644 --- a/library/Director/Web/Widget/JobDetails.php +++ b/library/Director/Web/Widget/JobDetails.php @@ -45,7 +45,7 @@ class JobDetails extends HtmlDocument $tsLastAttempt = $job->get('ts_last_attempt'); if ($tsLastAttempt) { - $ts = \strtotime($tsLastAttempt); + $ts = $tsLastAttempt / 1000; $timeAgo = Html::tag('span', [ 'class' => 'time-ago', 'title' => DateFormatter::formatDateTime($ts) diff --git a/schema/mysql-migrations/upgrade_189.sql b/schema/mysql-migrations/upgrade_189.sql index c8b91933..4a52fc0d 100644 --- a/schema/mysql-migrations/upgrade_189.sql +++ b/schema/mysql-migrations/upgrade_189.sql @@ -14,4 +14,4 @@ ALTER TABLE director_job INSERT INTO director_schema_migration (schema_version, migration_time) -VALUES (189, NOW()); \ No newline at end of file +VALUES (189, NOW());