feat(maintenance): Perform the CRON migration in the migration file, and add an espilon for check safety

This commit is contained in:
Ionys 2025-06-14 20:44:46 +02:00
parent f0abbe0861
commit 3075bc9bd9
2 changed files with 28 additions and 6 deletions

View file

@ -1,9 +1,30 @@
// Add column last_start_date to maintenance table
exports.up = function (knex) {
return knex.schema
exports.up = async function (knex) {
await knex.schema
.alterTable("maintenance", function (table) {
table.datetime("last_start_date");
});
// Perform migration for recurring-interval strategy
const recurringMaintenances = await knex("maintenance").where({
strategy: "recurring-interval",
cron: "* * * * *"
}).select("id", "start_time");
// eslint-disable-next-line camelcase
const maintenanceUpdates = recurringMaintenances.map(async ({ start_time, id }) => {
// eslint-disable-next-line camelcase
const [ hourStr, minuteStr ] = start_time.split(":");
const hour = parseInt(hourStr, 10);
const minute = parseInt(minuteStr, 10);
const cron = `${minute} ${hour} * * *`;
await knex("maintenance")
.where({ id })
.update({ cron });
});
await Promise.all(maintenanceUpdates);
};
exports.down = function (knex) {

View file

@ -219,8 +219,8 @@ class Maintenance extends BeanModel {
log.debug("maintenance", "Run maintenance id: " + this.id);
// 1.21.2 migration and 2.0.0-beta.4 migration
if (!this.cron || this.strategy === "recurring-interval" && this.cron === "* * * * *") {
// 1.21.2 migration
if (!this.cron) {
await this.generateCron();
if (!this.timezone) {
this.timezone = "UTC";
@ -281,7 +281,8 @@ class Maintenance extends BeanModel {
}
// If last start date is set, it means the maintenance has been started before
let lastStartDate = dayjs(this.lastStartDate);
let lastStartDate = dayjs(this.lastStartDate)
.subtract(1.1, "hour"); // Subtract 1.1 hour to avoid issues with timezone differences
// Check if the interval is enough
if (current.diff(lastStartDate, "day") < this.interval_day) {
@ -447,7 +448,7 @@ class Maintenance extends BeanModel {
let array = this.start_time.split(":");
let hour = parseInt(array[0]);
let minute = parseInt(array[1]);
this.cron = minute + " " + hour + " * * *";
this.cron = `${minute} ${hour} * * *`;
this.duration = this.calcDuration();
log.debug("maintenance", "Cron: " + this.cron);
log.debug("maintenance", "Duration: " + this.duration);