2022-11-08 08:06:00 -08:00
|
|
|
import { existsSync } from 'fs';
|
|
|
|
import { mkdir, utimes, open, rm } from 'fs/promises';
|
|
|
|
import { join, dirname } from 'path';
|
2023-10-23 04:39:35 -07:00
|
|
|
import { Container } from 'typedi';
|
|
|
|
import { InstanceSettings } from 'n8n-core';
|
2023-10-25 07:35:22 -07:00
|
|
|
import { sleep } from 'n8n-workflow';
|
2022-12-16 06:27:49 -08:00
|
|
|
import { inProduction } from '@/constants';
|
2023-10-25 07:35:22 -07:00
|
|
|
import { Logger } from '@/Logger';
|
2022-11-08 08:06:00 -08:00
|
|
|
|
|
|
|
export const touchFile = async (filePath: string): Promise<void> => {
|
|
|
|
await mkdir(dirname(filePath), { recursive: true });
|
|
|
|
const time = new Date();
|
|
|
|
try {
|
|
|
|
await utimes(filePath, time, time);
|
|
|
|
} catch {
|
|
|
|
const fd = await open(filePath, 'w');
|
|
|
|
await fd.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-23 04:39:35 -07:00
|
|
|
const { n8nFolder } = Container.get(InstanceSettings);
|
|
|
|
const journalFile = join(n8nFolder, 'crash.journal');
|
2022-11-08 08:06:00 -08:00
|
|
|
|
|
|
|
export const init = async () => {
|
2022-12-16 06:27:49 -08:00
|
|
|
if (!inProduction) return;
|
|
|
|
|
2022-11-08 08:06:00 -08:00
|
|
|
if (existsSync(journalFile)) {
|
|
|
|
// Crash detected
|
2023-10-25 07:35:22 -07:00
|
|
|
Container.get(Logger).error('Last session crashed');
|
2022-11-08 08:06:00 -08:00
|
|
|
// add a 10 seconds pause to slow down crash-looping
|
|
|
|
await sleep(10_000);
|
|
|
|
}
|
|
|
|
await touchFile(journalFile);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const cleanup = async () => {
|
|
|
|
await rm(journalFile, { force: true });
|
|
|
|
};
|