@futo-org/backups-orchestrator-api 1.12.0 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/controllers/job.controller.d.ts +5 -4
- package/dist/controllers/job.controller.js +13 -12
- package/dist/controllers/job.controller.js.map +1 -1
- package/dist/controllers/onboarding.controller.d.ts +2 -1
- package/dist/controllers/onboarding.controller.js +16 -1
- package/dist/controllers/onboarding.controller.js.map +1 -1
- package/dist/controllers/schedule.controller.d.ts +3 -2
- package/dist/controllers/schedule.controller.js +8 -6
- package/dist/controllers/schedule.controller.js.map +1 -1
- package/dist/dto/onboarding.dto.d.ts +6 -0
- package/dist/dto/onboarding.dto.js +6 -0
- package/dist/dto/onboarding.dto.js.map +1 -1
- package/dist/dto/param.dto.d.ts +7 -0
- package/dist/dto/param.dto.js +6 -0
- package/dist/dto/param.dto.js.map +1 -0
- package/dist/enum.d.ts +3 -6
- package/dist/enum.js +2 -6
- package/dist/enum.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/jobs/repositoryBackup.job.js +25 -26
- package/dist/jobs/repositoryBackup.job.js.map +1 -1
- package/dist/jobs/repositoryRestoreFromPoint.job.d.ts +5 -3
- package/dist/jobs/repositoryRestoreFromPoint.job.js +23 -7
- package/dist/jobs/repositoryRestoreFromPoint.job.js.map +1 -1
- package/dist/legacy-sqlite/legacySqlite.d.ts +3 -0
- package/dist/legacy-sqlite/legacySqlite.js +38 -0
- package/dist/legacy-sqlite/legacySqlite.js.map +1 -0
- package/dist/legacy-sqlite/legacyState.repository.d.ts +9 -0
- package/dist/legacy-sqlite/legacyState.repository.js +221 -0
- package/dist/legacy-sqlite/legacyState.repository.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260218105210-InitialMigration.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260218122600-AddRepositories.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260224112323-RepositoryPathTable.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260224123344-RunHistoryTable.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260224125102-RepositoryMetricsTable.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260304162353-ScheduleTable.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260313222211-AddMetricsColumns.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260330120000-RepositoryIntegrationImmichTable.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260330130000-AddScheduleIdToImmichIntegration.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260507120000-AddRunHistoryType.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260512120000-AddRepositoryRetentionPolicy.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260512130000-AllowForgetRunHistoryType.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260603120000-AddRepositoryRemoteId.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260729130000-AddRepositoryPlacement.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260814120000-AllowWarnRunHistoryStatus.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260814120100-AddLastBackupStatus.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20260917120000-AllowCancelledStatus.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20261013120000-AddJobs.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20261015120000-MigrateRunHistoryToJobs.js.map +1 -0
- package/dist/legacy-sqlite/migrations/20261015120100-DropRunHistory.js.map +1 -0
- package/dist/legacy-sqlite/schema.d.ts +60 -0
- package/dist/legacy-sqlite/schema.js +2 -0
- package/dist/legacy-sqlite/schema.js.map +1 -0
- package/dist/moduleConfig.d.ts +10 -2
- package/dist/moduleConfig.js.map +1 -1
- package/dist/orchestrationApi.module.d.ts +6 -5
- package/dist/orchestrationApi.module.js +3 -18
- package/dist/orchestrationApi.module.js.map +1 -1
- package/dist/pglite.d.ts +5 -0
- package/dist/pglite.js +59 -0
- package/dist/pglite.js.map +1 -0
- package/dist/repositories/backend.repository.d.ts +2 -2
- package/dist/repositories/backend.repository.js +11 -22
- package/dist/repositories/backend.repository.js.map +1 -1
- package/dist/repositories/config.repository.d.ts +8 -2
- package/dist/repositories/config.repository.js +64 -54
- package/dist/repositories/config.repository.js.map +1 -1
- package/dist/repositories/database.repository.d.ts +4 -2
- package/dist/repositories/database.repository.js +24 -40
- package/dist/repositories/database.repository.js.map +1 -1
- package/dist/repositories/job.repository.d.ts +57 -11
- package/dist/repositories/job.repository.js +148 -66
- package/dist/repositories/job.repository.js.map +1 -1
- package/dist/repositories/repository.repository.d.ts +27 -15
- package/dist/repositories/repository.repository.js +11 -51
- package/dist/repositories/repository.repository.js.map +1 -1
- package/dist/repositories/repositoryIntegrationImmich.repository.d.ts +2 -6
- package/dist/repositories/repositoryIntegrationImmich.repository.js +16 -14
- package/dist/repositories/repositoryIntegrationImmich.repository.js.map +1 -1
- package/dist/repositories/repositoryLocalMetrics.repository.d.ts +8 -10
- package/dist/repositories/repositoryLocalMetrics.repository.js +23 -17
- package/dist/repositories/repositoryLocalMetrics.repository.js.map +1 -1
- package/dist/repositories/repositoryPath.repository.d.ts +4 -4
- package/dist/repositories/repositoryPath.repository.js +11 -7
- package/dist/repositories/repositoryPath.repository.js.map +1 -1
- package/dist/repositories/schedule.repository.d.ts +19 -18
- package/dist/repositories/schedule.repository.js +45 -44
- package/dist/repositories/schedule.repository.js.map +1 -1
- package/dist/schema/enums.d.ts +4 -0
- package/dist/schema/enums.js +19 -0
- package/dist/schema/enums.js.map +1 -0
- package/dist/schema/index.d.ts +14 -10
- package/dist/schema/index.js +35 -1
- package/dist/schema/index.js.map +1 -1
- package/dist/schema/migrations/1793295581871-InitialMigration.d.ts +3 -0
- package/dist/schema/migrations/1793295581871-InitialMigration.js +106 -0
- package/dist/schema/migrations/1793295581871-InitialMigration.js.map +1 -0
- package/dist/schema/tables/backend.table.d.ts +2 -2
- package/dist/schema/tables/backend.table.js +24 -2
- package/dist/schema/tables/backend.table.js.map +1 -1
- package/dist/schema/tables/config.table.d.ts +38 -3
- package/dist/schema/tables/config.table.js +26 -2
- package/dist/schema/tables/config.table.js.map +1 -1
- package/dist/schema/tables/job.table.d.ts +10 -6
- package/dist/schema/tables/job.table.js +75 -2
- package/dist/schema/tables/job.table.js.map +1 -1
- package/dist/schema/tables/repository.table.d.ts +4 -2
- package/dist/schema/tables/repository.table.js +41 -2
- package/dist/schema/tables/repository.table.js.map +1 -1
- package/dist/schema/tables/repositoryIntegrationImmich.table.d.ts +2 -2
- package/dist/schema/tables/repositoryIntegrationImmich.table.js +31 -3
- package/dist/schema/tables/repositoryIntegrationImmich.table.js.map +1 -1
- package/dist/schema/tables/repositoryLocalMetrics.table.d.ts +6 -5
- package/dist/schema/tables/repositoryLocalMetrics.table.js +39 -3
- package/dist/schema/tables/repositoryLocalMetrics.table.js.map +1 -1
- package/dist/schema/tables/repositoryPath.table.d.ts +1 -1
- package/dist/schema/tables/repositoryPath.table.js +26 -3
- package/dist/schema/tables/repositoryPath.table.js.map +1 -1
- package/dist/schema/tables/repositorySchedule.table.d.ts +2 -2
- package/dist/schema/tables/repositorySchedule.table.js +28 -4
- package/dist/schema/tables/repositorySchedule.table.js.map +1 -1
- package/dist/schema/tables/schedule.table.d.ts +6 -5
- package/dist/schema/tables/schedule.table.js +44 -2
- package/dist/schema/tables/schedule.table.js.map +1 -1
- package/dist/services/bootstrap.service.d.ts +5 -1
- package/dist/services/bootstrap.service.js +18 -2
- package/dist/services/bootstrap.service.js.map +1 -1
- package/dist/services/development.service.d.ts +3 -4
- package/dist/services/development.service.js +6 -18
- package/dist/services/development.service.js.map +1 -1
- package/dist/services/integrations.service.d.ts +5 -3
- package/dist/services/integrations.service.js +23 -12
- package/dist/services/integrations.service.js.map +1 -1
- package/dist/services/onboarding.service.d.ts +6 -2
- package/dist/services/onboarding.service.js +27 -2
- package/dist/services/onboarding.service.js.map +1 -1
- package/dist/services/repository.service.d.ts +1 -1
- package/dist/services/repository.service.js +17 -15
- package/dist/services/repository.service.js.map +1 -1
- package/dist/services/schedule.service.js +13 -14
- package/dist/services/schedule.service.js.map +1 -1
- package/dist/services/yucca.service.d.ts +26 -7
- package/dist/services/yucca.service.js +6 -7
- package/dist/services/yucca.service.js.map +1 -1
- package/dist/utils/restic.d.ts +2 -0
- package/dist/utils/restic.js +1 -0
- package/dist/utils/restic.js.map +1 -1
- package/package.json +28 -4
- package/dist/schema/migrations/20260218105210-InitialMigration.js.map +0 -1
- package/dist/schema/migrations/20260218122600-AddRepositories.js.map +0 -1
- package/dist/schema/migrations/20260224112323-RepositoryPathTable.js.map +0 -1
- package/dist/schema/migrations/20260224123344-RunHistoryTable.js.map +0 -1
- package/dist/schema/migrations/20260224125102-RepositoryMetricsTable.js.map +0 -1
- package/dist/schema/migrations/20260304162353-ScheduleTable.js.map +0 -1
- package/dist/schema/migrations/20260313222211-AddMetricsColumns.js.map +0 -1
- package/dist/schema/migrations/20260330120000-RepositoryIntegrationImmichTable.js.map +0 -1
- package/dist/schema/migrations/20260330130000-AddScheduleIdToImmichIntegration.js.map +0 -1
- package/dist/schema/migrations/20260507120000-AddRunHistoryType.js.map +0 -1
- package/dist/schema/migrations/20260512120000-AddRepositoryRetentionPolicy.js.map +0 -1
- package/dist/schema/migrations/20260512130000-AllowForgetRunHistoryType.js.map +0 -1
- package/dist/schema/migrations/20260603120000-AddRepositoryRemoteId.js.map +0 -1
- package/dist/schema/migrations/20260729130000-AddRepositoryPlacement.js.map +0 -1
- package/dist/schema/migrations/20260814120000-AllowWarnRunHistoryStatus.js.map +0 -1
- package/dist/schema/migrations/20260814120100-AddLastBackupStatus.js.map +0 -1
- package/dist/schema/migrations/20260917120000-AllowCancelledStatus.js.map +0 -1
- package/dist/schema/migrations/20261013120000-AddJobs.js.map +0 -1
- package/dist/schema/migrations/20261015120000-MigrateRunHistoryToJobs.js.map +0 -1
- package/dist/schema/migrations/20261015120100-DropRunHistory.js.map +0 -1
- /package/dist/{schema → legacy-sqlite}/migrations/20260218105210-InitialMigration.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260218105210-InitialMigration.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260218122600-AddRepositories.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260218122600-AddRepositories.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260224112323-RepositoryPathTable.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260224112323-RepositoryPathTable.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260224123344-RunHistoryTable.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260224123344-RunHistoryTable.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260224125102-RepositoryMetricsTable.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260224125102-RepositoryMetricsTable.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260304162353-ScheduleTable.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260304162353-ScheduleTable.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260313222211-AddMetricsColumns.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260313222211-AddMetricsColumns.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260330120000-RepositoryIntegrationImmichTable.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260330120000-RepositoryIntegrationImmichTable.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260330130000-AddScheduleIdToImmichIntegration.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260330130000-AddScheduleIdToImmichIntegration.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260507120000-AddRunHistoryType.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260507120000-AddRunHistoryType.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260512120000-AddRepositoryRetentionPolicy.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260512120000-AddRepositoryRetentionPolicy.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260512130000-AllowForgetRunHistoryType.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260512130000-AllowForgetRunHistoryType.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260603120000-AddRepositoryRemoteId.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260603120000-AddRepositoryRemoteId.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260729130000-AddRepositoryPlacement.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260729130000-AddRepositoryPlacement.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260814120000-AllowWarnRunHistoryStatus.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260814120000-AllowWarnRunHistoryStatus.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260814120100-AddLastBackupStatus.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260814120100-AddLastBackupStatus.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260917120000-AllowCancelledStatus.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20260917120000-AllowCancelledStatus.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20261013120000-AddJobs.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20261013120000-AddJobs.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20261015120000-MigrateRunHistoryToJobs.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20261015120000-MigrateRunHistoryToJobs.js +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20261015120100-DropRunHistory.d.ts +0 -0
- /package/dist/{schema → legacy-sqlite}/migrations/20261015120100-DropRunHistory.js +0 -0
|
@@ -12,7 +12,8 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
12
12
|
};
|
|
13
13
|
var DatabaseRepository_1;
|
|
14
14
|
import { Injectable } from '@nestjs/common';
|
|
15
|
-
import {
|
|
15
|
+
import { Kysely, sql } from 'kysely';
|
|
16
|
+
import { FileMigrationProvider, Migrator, NO_MIGRATIONS } from 'kysely/migration';
|
|
16
17
|
import { InjectKysely } from 'nestjs-kysely';
|
|
17
18
|
import { join } from 'node:path';
|
|
18
19
|
import { LoggingRepository } from './logging.repository.js';
|
|
@@ -27,9 +28,22 @@ let DatabaseRepository = DatabaseRepository_1 = class DatabaseRepository {
|
|
|
27
28
|
this.logger = logger;
|
|
28
29
|
this.logger.setContext(DatabaseRepository_1.name);
|
|
29
30
|
}
|
|
31
|
+
async tryLock(key) {
|
|
32
|
+
const { rows: [{ locked }], } = await sql `SELECT pg_try_advisory_lock(hashtext(${key})) AS locked`.execute(this.db);
|
|
33
|
+
return locked;
|
|
34
|
+
}
|
|
35
|
+
withLock(key, callback) {
|
|
36
|
+
return this.db.transaction().execute(async (trx) => {
|
|
37
|
+
await sql `SELECT pg_advisory_xact_lock(hashtext(${key}))`.execute(trx);
|
|
38
|
+
return callback(trx);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
30
41
|
async runMigrations() {
|
|
31
|
-
const
|
|
32
|
-
|
|
42
|
+
const { rows: [{ value }], } = await sql `SELECT 1::int8 AS value`.execute(this.db);
|
|
43
|
+
if (typeof value !== 'number') {
|
|
44
|
+
throw new TypeError('Misconfigured dialect: enable convertBigIntToNumber');
|
|
45
|
+
}
|
|
46
|
+
const { error, results } = await this.createMigrator().migrateToLatest();
|
|
33
47
|
for (const result of results ?? []) {
|
|
34
48
|
if (result.status === 'Success') {
|
|
35
49
|
this.logger.debug(`Migration "${result.migrationName}" succeeded`);
|
|
@@ -44,48 +58,18 @@ let DatabaseRepository = DatabaseRepository_1 = class DatabaseRepository {
|
|
|
44
58
|
}
|
|
45
59
|
this.logger.log('Finished running migrations');
|
|
46
60
|
}
|
|
47
|
-
async
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
SELECT name FROM restored.sqlite_master WHERE type = 'table' AND name = 'kysely_migrations'
|
|
53
|
-
`.execute(this.db);
|
|
54
|
-
if (kyselyProbe.length === 0) {
|
|
55
|
-
await sql `DETACH DATABASE restored`.execute(this.db);
|
|
56
|
-
throw new Error("Refusing to restore database, doesn't appear to be from the app");
|
|
57
|
-
}
|
|
58
|
-
await this.db.transaction().execute(async (trx) => {
|
|
59
|
-
const currentSchema = await sql `
|
|
60
|
-
SELECT name FROM main.sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'
|
|
61
|
-
`.execute(trx);
|
|
62
|
-
for (const { name } of currentSchema.rows) {
|
|
63
|
-
await sql `DROP TABLE ${sql.table(name)}`.execute(trx);
|
|
64
|
-
}
|
|
65
|
-
const restoredSchema = await sql `
|
|
66
|
-
SELECT type, name, sql FROM restored.sqlite_master
|
|
67
|
-
WHERE type IN ('table', 'index') AND name NOT LIKE 'sqlite_%' AND sql IS NOT NULL
|
|
68
|
-
ORDER BY type = 'index'
|
|
69
|
-
`.execute(trx);
|
|
70
|
-
for (const object of restoredSchema.rows) {
|
|
71
|
-
await sql.raw(object.sql).execute(trx);
|
|
72
|
-
}
|
|
73
|
-
for (const { name } of restoredSchema.rows.filter((object) => object.type === 'table')) {
|
|
74
|
-
await sql `INSERT INTO ${sql.table(name)} SELECT * FROM ${sql.table(`restored.${name}`)}`.execute(trx);
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
await sql `DETACH DATABASE restored`.execute(this.db);
|
|
78
|
-
}
|
|
79
|
-
finally {
|
|
80
|
-
await sql `PRAGMA foreign_keys = ON`.execute(this.db);
|
|
61
|
+
async resetMigrations() {
|
|
62
|
+
const { error } = await this.createMigrator().migrateTo(NO_MIGRATIONS);
|
|
63
|
+
if (error) {
|
|
64
|
+
this.logger.error(`Migration rollback failed: ${error}`);
|
|
65
|
+
throw new Error('Migration rollback failed.');
|
|
81
66
|
}
|
|
82
|
-
await this.runMigrations();
|
|
83
67
|
}
|
|
84
68
|
createMigrator() {
|
|
85
69
|
return new Migrator({
|
|
86
70
|
db: this.db,
|
|
87
|
-
migrationLockTableName: '
|
|
88
|
-
migrationTableName: '
|
|
71
|
+
migrationLockTableName: 'futo_backups_kysely_migrations_lock',
|
|
72
|
+
migrationTableName: 'futo_backups_kysely_migrations',
|
|
89
73
|
provider: new FileMigrationProvider({
|
|
90
74
|
fs: {
|
|
91
75
|
readdir: this.storage.readdir,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"database.repository.js","sourceRoot":"","sources":["../../src/repositories/database.repository.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"database.repository.js","sourceRoot":"","sources":["../../src/repositories/database.repository.ts"],"names":[],"mappings":"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"}
|
|
@@ -9,6 +9,7 @@ import { JobTable } from '../schema/tables/job.table.js';
|
|
|
9
9
|
import { SupportModeService } from '../services/supportMode.service.js';
|
|
10
10
|
import { TelemetryService } from '../services/telemetry.service.js';
|
|
11
11
|
import type { JobItem } from '../utils/types.js';
|
|
12
|
+
import { DatabaseRepository } from './database.repository.js';
|
|
12
13
|
import { LoggingRepository } from './logging.repository.js';
|
|
13
14
|
import { ModuleConfigRepository } from './moduleConfig.repository.js';
|
|
14
15
|
import { StorageRepository } from './storage.repository.js';
|
|
@@ -25,23 +26,43 @@ export declare class JobRepository implements OnModuleInit, BeforeApplicationShu
|
|
|
25
26
|
private readonly telemetry;
|
|
26
27
|
private readonly supportMode;
|
|
27
28
|
private readonly supportContext;
|
|
29
|
+
private readonly database;
|
|
28
30
|
private handlers;
|
|
29
31
|
private dispatching?;
|
|
30
32
|
private running?;
|
|
31
33
|
private stopped;
|
|
32
34
|
private activeJobs;
|
|
33
|
-
constructor(db: Kysely<DB>, discovery: DiscoveryService, reflector: Reflector, moduleConfig: ModuleConfigRepository, storage: StorageRepository, events: EventsGateway, logger: LoggingRepository, telemetry: TelemetryService, supportMode: SupportModeService, supportContext: SupportContextRepository);
|
|
35
|
+
constructor(db: Kysely<DB>, discovery: DiscoveryService, reflector: Reflector, moduleConfig: ModuleConfigRepository, storage: StorageRepository, events: EventsGateway, logger: LoggingRepository, telemetry: TelemetryService, supportMode: SupportModeService, supportContext: SupportContextRepository, database: DatabaseRepository);
|
|
34
36
|
onModuleInit(): void;
|
|
35
37
|
start(): Promise<{
|
|
36
38
|
id: string;
|
|
37
39
|
name: JobName;
|
|
38
40
|
repositoryId: string;
|
|
39
41
|
status: JobStatus;
|
|
40
|
-
queuedAt:
|
|
41
|
-
start:
|
|
42
|
-
end:
|
|
43
|
-
outcome:
|
|
44
|
-
|
|
42
|
+
queuedAt: Date;
|
|
43
|
+
start: Date | null;
|
|
44
|
+
end: Date | null;
|
|
45
|
+
outcome: import("../dto/job.dto.js").JobOutcomeDto | null;
|
|
46
|
+
sequence: number;
|
|
47
|
+
data: {
|
|
48
|
+
repositoryId: string;
|
|
49
|
+
} | {
|
|
50
|
+
repositoryId: string;
|
|
51
|
+
} | {
|
|
52
|
+
repositoryId: string;
|
|
53
|
+
snapshotId: string;
|
|
54
|
+
options: import("../dto/repository.dto.js").RepositorySnapshotRestoreRequestDto;
|
|
55
|
+
} | {
|
|
56
|
+
repositoryId: string;
|
|
57
|
+
snapshotId: string;
|
|
58
|
+
backendId: string;
|
|
59
|
+
options: import("../dto/repository.dto.js").RepositorySnapshotRestoreFromPointRequestDto;
|
|
60
|
+
} | {
|
|
61
|
+
repositoryId: string;
|
|
62
|
+
snapshotId: string;
|
|
63
|
+
} | {
|
|
64
|
+
repositoryId: string;
|
|
65
|
+
};
|
|
45
66
|
logFilePath: string;
|
|
46
67
|
}[]>;
|
|
47
68
|
getLogFilePath(id: string): Promise<string | undefined>;
|
|
@@ -50,6 +71,8 @@ export declare class JobRepository implements OnModuleInit, BeforeApplicationShu
|
|
|
50
71
|
private beginSupportSession;
|
|
51
72
|
queue(item: JobItem): Promise<string>;
|
|
52
73
|
queueAll(items: JobItem[]): Promise<string[]>;
|
|
74
|
+
queueUnlessActive(item: JobItem): Promise<string | undefined>;
|
|
75
|
+
private onQueued;
|
|
53
76
|
get(id: string): Promise<{
|
|
54
77
|
progress: JobProgressDto | undefined;
|
|
55
78
|
id: string;
|
|
@@ -85,7 +108,11 @@ export declare class JobRepository implements OnModuleInit, BeforeApplicationShu
|
|
|
85
108
|
} | undefined;
|
|
86
109
|
} | undefined;
|
|
87
110
|
} | undefined>;
|
|
111
|
+
persistRunning(): Promise<void>;
|
|
112
|
+
getLatestByName(name: JobName): Promise<JobDto | undefined>;
|
|
113
|
+
getActive(name: JobName): Promise<JobDto | undefined>;
|
|
88
114
|
private find;
|
|
115
|
+
private findIncludingRunning;
|
|
89
116
|
getAll(repositoryId: string): Promise<JobDto[]>;
|
|
90
117
|
getLatest(repositoryId: string, name: JobName): Promise<JobDto | undefined>;
|
|
91
118
|
getLastBackup(repositoryId: string): Promise<{
|
|
@@ -102,11 +129,30 @@ export declare class JobRepository implements OnModuleInit, BeforeApplicationShu
|
|
|
102
129
|
name: JobName;
|
|
103
130
|
repositoryId: string;
|
|
104
131
|
status: JobStatus;
|
|
105
|
-
queuedAt:
|
|
106
|
-
start:
|
|
107
|
-
end:
|
|
108
|
-
outcome:
|
|
109
|
-
|
|
132
|
+
queuedAt: Date;
|
|
133
|
+
start: Date | null;
|
|
134
|
+
end: Date | null;
|
|
135
|
+
outcome: import("../dto/job.dto.js").JobOutcomeDto | null;
|
|
136
|
+
sequence: number;
|
|
137
|
+
data: {
|
|
138
|
+
repositoryId: string;
|
|
139
|
+
} | {
|
|
140
|
+
repositoryId: string;
|
|
141
|
+
} | {
|
|
142
|
+
repositoryId: string;
|
|
143
|
+
snapshotId: string;
|
|
144
|
+
options: import("../dto/repository.dto.js").RepositorySnapshotRestoreRequestDto;
|
|
145
|
+
} | {
|
|
146
|
+
repositoryId: string;
|
|
147
|
+
snapshotId: string;
|
|
148
|
+
backendId: string;
|
|
149
|
+
options: import("../dto/repository.dto.js").RepositorySnapshotRestoreFromPointRequestDto;
|
|
150
|
+
} | {
|
|
151
|
+
repositoryId: string;
|
|
152
|
+
snapshotId: string;
|
|
153
|
+
} | {
|
|
154
|
+
repositoryId: string;
|
|
155
|
+
};
|
|
110
156
|
logFilePath: string;
|
|
111
157
|
}>;
|
|
112
158
|
private settle;
|
|
@@ -26,15 +26,31 @@ import { SupportModeService } from '../services/supportMode.service.js';
|
|
|
26
26
|
import { TelemetryService } from '../services/telemetry.service.js';
|
|
27
27
|
import { explainConnectivityFailure } from '../utils/connectivity.js';
|
|
28
28
|
import { getJobStatus, TaskCancelledError, writeError } from '../utils/errors.js';
|
|
29
|
+
import { DatabaseRepository } from './database.repository.js';
|
|
29
30
|
import { LoggingRepository } from './logging.repository.js';
|
|
30
31
|
import { ModuleConfigRepository } from './moduleConfig.repository.js';
|
|
31
32
|
import { StorageRepository } from './storage.repository.js';
|
|
32
33
|
import { SupportContextRepository } from './supportContext.repository.js';
|
|
33
|
-
const toJobDto = ({ data: _data, logFilePath: _logFilePath, outcome, ...job }) => ({
|
|
34
|
+
const toJobDto = ({ data: _data, logFilePath: _logFilePath, sequence: _sequence, queuedAt, start, end, outcome, ...job }) => ({
|
|
34
35
|
...job,
|
|
35
|
-
|
|
36
|
+
queuedAt: queuedAt.toISOString(),
|
|
37
|
+
start: start?.toISOString(),
|
|
38
|
+
end: end?.toISOString(),
|
|
39
|
+
outcome: outcome ?? undefined,
|
|
36
40
|
});
|
|
37
41
|
const finishedStatuses = new Set([JobStatus.Complete, JobStatus.Warn, JobStatus.Failed, JobStatus.Cancelled]);
|
|
42
|
+
const activeStatuses = [JobStatus.Queued, JobStatus.Running];
|
|
43
|
+
const toQueuedJob = ({ name, data }) => {
|
|
44
|
+
const id = randomUUID();
|
|
45
|
+
return {
|
|
46
|
+
id,
|
|
47
|
+
name,
|
|
48
|
+
repositoryId: data.repositoryId,
|
|
49
|
+
data,
|
|
50
|
+
status: JobStatus.Queued,
|
|
51
|
+
logFilePath: join('logs', 'jobs', `${id}.jsonl`),
|
|
52
|
+
};
|
|
53
|
+
};
|
|
38
54
|
let JobRepository = JobRepository_1 = class JobRepository {
|
|
39
55
|
db;
|
|
40
56
|
discovery;
|
|
@@ -46,12 +62,13 @@ let JobRepository = JobRepository_1 = class JobRepository {
|
|
|
46
62
|
telemetry;
|
|
47
63
|
supportMode;
|
|
48
64
|
supportContext;
|
|
65
|
+
database;
|
|
49
66
|
handlers = new Map();
|
|
50
67
|
dispatching;
|
|
51
68
|
running;
|
|
52
69
|
stopped = false;
|
|
53
70
|
activeJobs = new Map();
|
|
54
|
-
constructor(db, discovery, reflector, moduleConfig, storage, events, logger, telemetry, supportMode, supportContext) {
|
|
71
|
+
constructor(db, discovery, reflector, moduleConfig, storage, events, logger, telemetry, supportMode, supportContext, database) {
|
|
55
72
|
this.db = db;
|
|
56
73
|
this.discovery = discovery;
|
|
57
74
|
this.reflector = reflector;
|
|
@@ -62,6 +79,7 @@ let JobRepository = JobRepository_1 = class JobRepository {
|
|
|
62
79
|
this.telemetry = telemetry;
|
|
63
80
|
this.supportMode = supportMode;
|
|
64
81
|
this.supportContext = supportContext;
|
|
82
|
+
this.database = database;
|
|
65
83
|
this.logger.setContext(JobRepository_1.name);
|
|
66
84
|
}
|
|
67
85
|
onModuleInit() {
|
|
@@ -92,9 +110,10 @@ let JobRepository = JobRepository_1 = class JobRepository {
|
|
|
92
110
|
return [];
|
|
93
111
|
}
|
|
94
112
|
return this.db
|
|
95
|
-
.updateTable('
|
|
96
|
-
.set({ status: JobStatus.Incomplete, end: new Date()
|
|
97
|
-
.where('status', 'in',
|
|
113
|
+
.updateTable('futo_backups_jobs')
|
|
114
|
+
.set({ status: JobStatus.Incomplete, end: new Date() })
|
|
115
|
+
.where('status', 'in', activeStatuses)
|
|
116
|
+
.$if(this.running !== undefined, (qb) => qb.where('id', '!=', this.running.job.id))
|
|
98
117
|
.returningAll()
|
|
99
118
|
.execute();
|
|
100
119
|
}
|
|
@@ -133,72 +152,125 @@ let JobRepository = JobRepository_1 = class JobRepository {
|
|
|
133
152
|
if (items.length === 0) {
|
|
134
153
|
return [];
|
|
135
154
|
}
|
|
136
|
-
const queuedAt = new Date().toISOString();
|
|
137
155
|
const jobs = await this.db
|
|
138
|
-
.insertInto('
|
|
139
|
-
.values(items.map((
|
|
140
|
-
const id = randomUUID();
|
|
141
|
-
return {
|
|
142
|
-
id,
|
|
143
|
-
name,
|
|
144
|
-
repositoryId: data.repositoryId,
|
|
145
|
-
data: JSON.stringify(data),
|
|
146
|
-
status: JobStatus.Queued,
|
|
147
|
-
logFilePath: join('logs', 'jobs', `${id}.jsonl`),
|
|
148
|
-
queuedAt,
|
|
149
|
-
};
|
|
150
|
-
}))
|
|
156
|
+
.insertInto('futo_backups_jobs')
|
|
157
|
+
.values(items.map((item) => toQueuedJob(item)))
|
|
151
158
|
.returningAll()
|
|
152
159
|
.execute();
|
|
160
|
+
this.onQueued(jobs);
|
|
161
|
+
return jobs.map((job) => job.id);
|
|
162
|
+
}
|
|
163
|
+
async queueUnlessActive(item) {
|
|
164
|
+
if (this.running?.job.name === item.name) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const job = await this.database.withLock(`futo_backups_jobs:${item.name}`, async (trx) => {
|
|
168
|
+
const active = await trx
|
|
169
|
+
.selectFrom('futo_backups_jobs')
|
|
170
|
+
.select('id')
|
|
171
|
+
.where('name', '=', item.name)
|
|
172
|
+
.where('status', 'in', activeStatuses)
|
|
173
|
+
.executeTakeFirst();
|
|
174
|
+
if (active) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
return trx.insertInto('futo_backups_jobs').values(toQueuedJob(item)).returningAll().executeTakeFirst();
|
|
178
|
+
});
|
|
179
|
+
if (!job) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
this.onQueued([job]);
|
|
183
|
+
return job.id;
|
|
184
|
+
}
|
|
185
|
+
onQueued(jobs) {
|
|
153
186
|
for (const job of jobs) {
|
|
154
187
|
this.events.publish({ type: 'JobCreate', job: toJobDto(job) });
|
|
155
188
|
}
|
|
156
189
|
void this.dispatch();
|
|
157
|
-
return jobs.map((job) => job.id);
|
|
158
190
|
}
|
|
159
191
|
async get(id) {
|
|
160
|
-
const job = await this.
|
|
192
|
+
const job = await this.findIncludingRunning(id);
|
|
161
193
|
if (!job) {
|
|
162
194
|
return;
|
|
163
195
|
}
|
|
164
|
-
return { ...toJobDto(job), progress: this.running?.id === id ? this.running.ctx?.progress : undefined };
|
|
196
|
+
return { ...toJobDto(job), progress: this.running?.job.id === id ? this.running.ctx?.progress : undefined };
|
|
197
|
+
}
|
|
198
|
+
async persistRunning() {
|
|
199
|
+
if (!this.running) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const { sequence: _sequence, ...row } = this.running.job;
|
|
203
|
+
await this.db
|
|
204
|
+
.insertInto('futo_backups_jobs')
|
|
205
|
+
.values(row)
|
|
206
|
+
.onConflict((oc) => oc.column('id').doNothing())
|
|
207
|
+
.execute();
|
|
208
|
+
}
|
|
209
|
+
async getLatestByName(name) {
|
|
210
|
+
const job = await this.db
|
|
211
|
+
.selectFrom('futo_backups_jobs')
|
|
212
|
+
.selectAll()
|
|
213
|
+
.where('name', '=', name)
|
|
214
|
+
.orderBy('queuedAt', 'desc')
|
|
215
|
+
.orderBy('sequence', 'desc')
|
|
216
|
+
.limit(1)
|
|
217
|
+
.executeTakeFirst();
|
|
218
|
+
return job && toJobDto(job);
|
|
219
|
+
}
|
|
220
|
+
async getActive(name) {
|
|
221
|
+
if (this.running?.job.name === name) {
|
|
222
|
+
return toJobDto(this.running.job);
|
|
223
|
+
}
|
|
224
|
+
const job = await this.db
|
|
225
|
+
.selectFrom('futo_backups_jobs')
|
|
226
|
+
.selectAll()
|
|
227
|
+
.where('name', '=', name)
|
|
228
|
+
.where('status', 'in', activeStatuses)
|
|
229
|
+
.orderBy('sequence')
|
|
230
|
+
.limit(1)
|
|
231
|
+
.executeTakeFirst();
|
|
232
|
+
return job && toJobDto(job);
|
|
165
233
|
}
|
|
166
234
|
find(id) {
|
|
167
|
-
return this.db.selectFrom('
|
|
235
|
+
return this.db.selectFrom('futo_backups_jobs').selectAll().where('id', '=', id).executeTakeFirst();
|
|
236
|
+
}
|
|
237
|
+
async findIncludingRunning(id) {
|
|
238
|
+
const running = this.running?.job.id === id ? this.running.job : undefined;
|
|
239
|
+
return (await this.find(id)) ?? running;
|
|
168
240
|
}
|
|
169
241
|
async getAll(repositoryId) {
|
|
170
242
|
const jobs = await this.db
|
|
171
|
-
.selectFrom('
|
|
243
|
+
.selectFrom('futo_backups_jobs')
|
|
172
244
|
.selectAll()
|
|
173
245
|
.where('repositoryId', '=', repositoryId)
|
|
174
246
|
.orderBy('queuedAt', 'desc')
|
|
175
|
-
.orderBy(
|
|
247
|
+
.orderBy('sequence', 'desc')
|
|
176
248
|
.execute();
|
|
177
249
|
return jobs.map((job) => toJobDto(job));
|
|
178
250
|
}
|
|
179
251
|
async getLatest(repositoryId, name) {
|
|
180
252
|
const job = await this.db
|
|
181
|
-
.selectFrom('
|
|
253
|
+
.selectFrom('futo_backups_jobs')
|
|
182
254
|
.selectAll()
|
|
183
255
|
.where('repositoryId', '=', repositoryId)
|
|
184
256
|
.where('name', '=', name)
|
|
185
257
|
.orderBy('queuedAt', 'desc')
|
|
186
|
-
.orderBy(
|
|
258
|
+
.orderBy('sequence', 'desc')
|
|
187
259
|
.limit(1)
|
|
188
260
|
.executeTakeFirst();
|
|
189
261
|
return job && toJobDto(job);
|
|
190
262
|
}
|
|
191
263
|
async getLastBackup(repositoryId) {
|
|
192
|
-
const snapshotId = sql `
|
|
193
|
-
const totalBytes = sql `
|
|
264
|
+
const snapshotId = sql `outcome -> 'summary' ->> 'snapshotId'`;
|
|
265
|
+
const totalBytes = sql `(outcome -> 'summary' ->> 'totalBytesProcessed')::int8`;
|
|
194
266
|
const job = await this.db
|
|
195
|
-
.selectFrom('
|
|
267
|
+
.selectFrom('futo_backups_jobs')
|
|
196
268
|
.select([snapshotId.as('snapshotId'), totalBytes.as('totalBytes')])
|
|
197
269
|
.where('repositoryId', '=', repositoryId)
|
|
198
270
|
.where('name', '=', JobName.RepositoryBackup)
|
|
199
271
|
.where(snapshotId, 'is not', null)
|
|
200
272
|
.orderBy('queuedAt', 'desc')
|
|
201
|
-
.orderBy(
|
|
273
|
+
.orderBy('sequence', 'desc')
|
|
202
274
|
.limit(1)
|
|
203
275
|
.executeTakeFirst();
|
|
204
276
|
return job?.snapshotId ? { snapshotId: job.snapshotId, totalBytes: job.totalBytes ?? undefined } : undefined;
|
|
@@ -238,7 +310,7 @@ let JobRepository = JobRepository_1 = class JobRepository {
|
|
|
238
310
|
async cancelAll(repositoryId) {
|
|
239
311
|
await this.cancelQueued((eb) => eb('repositoryId', '=', repositoryId));
|
|
240
312
|
const running = await this.db
|
|
241
|
-
.selectFrom('
|
|
313
|
+
.selectFrom('futo_backups_jobs')
|
|
242
314
|
.select('id')
|
|
243
315
|
.where('repositoryId', '=', repositoryId)
|
|
244
316
|
.where('status', '=', JobStatus.Running)
|
|
@@ -248,14 +320,14 @@ let JobRepository = JobRepository_1 = class JobRepository {
|
|
|
248
320
|
this.events.publish({ type: 'JobCancel', jobId: id });
|
|
249
321
|
}
|
|
250
322
|
}
|
|
251
|
-
if (this.running?.repositoryId === repositoryId) {
|
|
252
|
-
await this.wait(this.running.id).catch(() => { });
|
|
323
|
+
if (this.running?.job.repositoryId === repositoryId) {
|
|
324
|
+
await this.wait(this.running.job.id).catch(() => { });
|
|
253
325
|
}
|
|
254
326
|
}
|
|
255
327
|
async cancelQueued(where) {
|
|
256
328
|
const cancelled = await this.db
|
|
257
|
-
.updateTable('
|
|
258
|
-
.set({ status: JobStatus.Cancelled, end: new Date()
|
|
329
|
+
.updateTable('futo_backups_jobs')
|
|
330
|
+
.set({ status: JobStatus.Cancelled, end: new Date() })
|
|
259
331
|
.where('status', '=', JobStatus.Queued)
|
|
260
332
|
.where(where)
|
|
261
333
|
.returningAll()
|
|
@@ -265,14 +337,14 @@ let JobRepository = JobRepository_1 = class JobRepository {
|
|
|
265
337
|
type: 'JobUpdate',
|
|
266
338
|
jobId: job.id,
|
|
267
339
|
repositoryId: job.repositoryId,
|
|
268
|
-
job: { status: job.status, end: job.end },
|
|
340
|
+
job: { status: job.status, end: job.end?.toISOString() },
|
|
269
341
|
});
|
|
270
342
|
this.settle(job, new TaskCancelledError());
|
|
271
343
|
}
|
|
272
344
|
return cancelled;
|
|
273
345
|
}
|
|
274
346
|
abort(id) {
|
|
275
|
-
if (this.running?.id !== id) {
|
|
347
|
+
if (this.running?.job.id !== id) {
|
|
276
348
|
return false;
|
|
277
349
|
}
|
|
278
350
|
this.running.controller.abort(new TaskCancelledError());
|
|
@@ -284,7 +356,7 @@ let JobRepository = JobRepository_1 = class JobRepository {
|
|
|
284
356
|
waiter = Promise.withResolvers();
|
|
285
357
|
this.activeJobs.set(id, waiter);
|
|
286
358
|
}
|
|
287
|
-
const job = await this.
|
|
359
|
+
const job = await this.findIncludingRunning(id);
|
|
288
360
|
if (!job) {
|
|
289
361
|
throw new NotFoundException('Job not found');
|
|
290
362
|
}
|
|
@@ -331,14 +403,17 @@ let JobRepository = JobRepository_1 = class JobRepository {
|
|
|
331
403
|
}
|
|
332
404
|
async claim() {
|
|
333
405
|
const job = await this.db
|
|
334
|
-
.updateTable('
|
|
335
|
-
.set({ status: JobStatus.Running, start: new Date()
|
|
406
|
+
.updateTable('futo_backups_jobs')
|
|
407
|
+
.set({ status: JobStatus.Running, start: new Date() })
|
|
408
|
+
.where('status', '=', JobStatus.Queued)
|
|
336
409
|
.where('id', '=', (eb) => eb
|
|
337
|
-
.selectFrom('
|
|
410
|
+
.selectFrom('futo_backups_jobs')
|
|
338
411
|
.select('id')
|
|
339
412
|
.where('status', '=', JobStatus.Queued)
|
|
340
|
-
.orderBy(
|
|
341
|
-
.limit(1)
|
|
413
|
+
.orderBy('sequence')
|
|
414
|
+
.limit(1)
|
|
415
|
+
.forUpdate()
|
|
416
|
+
.skipLocked())
|
|
342
417
|
.returningAll()
|
|
343
418
|
.executeTakeFirst();
|
|
344
419
|
if (job) {
|
|
@@ -346,7 +421,7 @@ let JobRepository = JobRepository_1 = class JobRepository {
|
|
|
346
421
|
type: 'JobUpdate',
|
|
347
422
|
jobId: job.id,
|
|
348
423
|
repositoryId: job.repositoryId,
|
|
349
|
-
job: { status: job.status, start: job.start },
|
|
424
|
+
job: { status: job.status, start: job.start?.toISOString() },
|
|
350
425
|
});
|
|
351
426
|
}
|
|
352
427
|
return job;
|
|
@@ -354,14 +429,14 @@ let JobRepository = JobRepository_1 = class JobRepository {
|
|
|
354
429
|
async run(job) {
|
|
355
430
|
this.logger.log(`Running job ${job.name} id=${job.id}`);
|
|
356
431
|
const { handler } = this.handlers.get(job.name);
|
|
357
|
-
this.running = {
|
|
432
|
+
this.running = { job, controller: new AbortController() };
|
|
358
433
|
const ctx = await this.createCtx(job, this.running.controller.signal);
|
|
359
434
|
this.running.ctx = ctx;
|
|
360
435
|
const support = await this.beginSupportSession(job);
|
|
361
436
|
let error;
|
|
362
437
|
try {
|
|
363
438
|
try {
|
|
364
|
-
await this.supportContext.run(support, () => handler.run(
|
|
439
|
+
await this.supportContext.run(support, () => handler.run(job.data, ctx));
|
|
365
440
|
}
|
|
366
441
|
catch (error_) {
|
|
367
442
|
error = error_;
|
|
@@ -371,30 +446,31 @@ let JobRepository = JobRepository_1 = class JobRepository {
|
|
|
371
446
|
if (support) {
|
|
372
447
|
ctx.outcome.support = await support.finish();
|
|
373
448
|
}
|
|
449
|
+
await new Promise((done) => ctx.logWriter.end(done));
|
|
450
|
+
const updateSet = {
|
|
451
|
+
status: getJobStatus(error),
|
|
452
|
+
outcome: ctx.outcome,
|
|
453
|
+
end: new Date(),
|
|
454
|
+
};
|
|
455
|
+
const { sequence: _sequence, ...row } = job;
|
|
456
|
+
job = await this.db
|
|
457
|
+
.insertInto('futo_backups_jobs')
|
|
458
|
+
.values({
|
|
459
|
+
...row,
|
|
460
|
+
...updateSet,
|
|
461
|
+
})
|
|
462
|
+
.onConflict((oc) => oc.column('id').doUpdateSet(updateSet))
|
|
463
|
+
.returningAll()
|
|
464
|
+
.executeTakeFirstOrThrow();
|
|
374
465
|
}
|
|
375
466
|
finally {
|
|
376
467
|
this.running = undefined;
|
|
377
468
|
}
|
|
378
|
-
await new Promise((done) => ctx.logWriter.end(done));
|
|
379
|
-
const updateSet = {
|
|
380
|
-
status: getJobStatus(error),
|
|
381
|
-
outcome: JSON.stringify(ctx.outcome),
|
|
382
|
-
end: new Date().toISOString(),
|
|
383
|
-
};
|
|
384
|
-
job = await this.db
|
|
385
|
-
.insertInto('jobs')
|
|
386
|
-
.values({
|
|
387
|
-
...job,
|
|
388
|
-
...updateSet,
|
|
389
|
-
})
|
|
390
|
-
.onConflict((oc) => oc.column('id').doUpdateSet(updateSet))
|
|
391
|
-
.returningAll()
|
|
392
|
-
.executeTakeFirstOrThrow();
|
|
393
469
|
this.events.publish({
|
|
394
470
|
type: 'JobUpdate',
|
|
395
471
|
jobId: job.id,
|
|
396
472
|
repositoryId: job.repositoryId,
|
|
397
|
-
job: { status: job.status, outcome: ctx.outcome, end: job.end },
|
|
473
|
+
job: { status: job.status, outcome: ctx.outcome, end: job.end?.toISOString() },
|
|
398
474
|
});
|
|
399
475
|
this.settle(job, error);
|
|
400
476
|
}
|
|
@@ -413,7 +489,12 @@ let JobRepository = JobRepository_1 = class JobRepository {
|
|
|
413
489
|
return;
|
|
414
490
|
}
|
|
415
491
|
this.running.lastProgressTick = progress;
|
|
416
|
-
this.events.publish({
|
|
492
|
+
this.events.publish({
|
|
493
|
+
type: 'JobTick',
|
|
494
|
+
jobId: this.running.job.id,
|
|
495
|
+
repositoryId: this.running.job.repositoryId,
|
|
496
|
+
progress,
|
|
497
|
+
});
|
|
417
498
|
}
|
|
418
499
|
};
|
|
419
500
|
__decorate([
|
|
@@ -434,7 +515,8 @@ JobRepository = JobRepository_1 = __decorate([
|
|
|
434
515
|
LoggingRepository,
|
|
435
516
|
TelemetryService,
|
|
436
517
|
SupportModeService,
|
|
437
|
-
SupportContextRepository
|
|
518
|
+
SupportContextRepository,
|
|
519
|
+
DatabaseRepository])
|
|
438
520
|
], JobRepository);
|
|
439
521
|
export { JobRepository };
|
|
440
522
|
//# sourceMappingURL=job.repository.js.map
|