✨ Move scheduled actions save path and create directory if it does not exist
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,6 +1,6 @@
|
|||||||
node_modules
|
node_modules
|
||||||
.env
|
.env
|
||||||
tokens.json
|
tokens.json
|
||||||
scheduled.json
|
storage/*.json
|
||||||
*.log
|
*.log
|
||||||
TODO.md
|
TODO.md
|
||||||
|
@@ -2,18 +2,11 @@ import { promises as fs } from "fs";
|
|||||||
import { handleClientAction } from "../chatClient";
|
import { handleClientAction } from "../chatClient";
|
||||||
import { resolve } from "path";
|
import { resolve } from "path";
|
||||||
|
|
||||||
export {
|
|
||||||
start,
|
|
||||||
scheduledActions,
|
|
||||||
checkScheduledActions,
|
|
||||||
saveScheduledActions
|
|
||||||
};
|
|
||||||
|
|
||||||
const LOG_PREFIX = "[Scheduled] ";
|
const LOG_PREFIX = "[Scheduled] ";
|
||||||
const SCHEDULED_FILE = resolve(process.cwd(), "scheduled.json");
|
const SCHEDULED_FILE = resolve(process.cwd(), "./storage/scheduled.json");
|
||||||
const FIRST_CHECK_TIMEOUT = 1000 * 5;
|
const FIRST_CHECK_TIMEOUT = 5e3;
|
||||||
const SAVE_TIMEOUT = 1000 * 30;
|
const SAVE_TIMEOUT = 5e3;
|
||||||
const CHECK_INTERVAL = 1000 * 60;
|
const CHECK_INTERVAL = 60e3;
|
||||||
|
|
||||||
const scheduledActions: Array<any> = [];
|
const scheduledActions: Array<any> = [];
|
||||||
|
|
||||||
@@ -32,6 +25,9 @@ async function start(): Promise<void> {
|
|||||||
savedActions = JSON.parse((await fs.readFile(SCHEDULED_FILE)).toString());
|
savedActions = JSON.parse((await fs.readFile(SCHEDULED_FILE)).toString());
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// probably file does not exist
|
// probably file does not exist
|
||||||
|
if (e instanceof Error) {
|
||||||
|
console.log(`${LOG_PREFIX}${e.message}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
scheduledActions.push.apply(scheduledActions, savedActions);
|
scheduledActions.push.apply(scheduledActions, savedActions);
|
||||||
@@ -78,8 +74,22 @@ function saveScheduledActions(): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
saveScheduledActionsTimeout = setTimeout(async () => {
|
saveScheduledActionsTimeout = setTimeout(async () => {
|
||||||
|
const normalizedPath = SCHEDULED_FILE.replace(/\\/g, "/");
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fs.stat(normalizedPath);
|
||||||
|
} catch (e) {
|
||||||
|
const dirs = normalizedPath.split("/");
|
||||||
|
dirs.pop();
|
||||||
|
await fs.mkdir(dirs.join("/"));
|
||||||
|
}
|
||||||
|
|
||||||
await fs.writeFile(SCHEDULED_FILE, JSON.stringify(scheduledActions));
|
await fs.writeFile(SCHEDULED_FILE, JSON.stringify(scheduledActions));
|
||||||
console.log(`${LOG_PREFIX}Saved actions.`);
|
console.log(`${LOG_PREFIX}Saved actions.`);
|
||||||
saveScheduledActionsTimeout = null;
|
saveScheduledActionsTimeout = null;
|
||||||
}, SAVE_TIMEOUT);
|
}, SAVE_TIMEOUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
saveScheduledActions();
|
||||||
|
|
||||||
|
export { start, scheduledActions, saveScheduledActions };
|
||||||
|
Reference in New Issue
Block a user