/
persistence.ts
66 lines (60 loc) · 1.5 KB
/
persistence.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import * as path from "path";
import * as fs from "fs-extra";
import { Task, Scheduler } from "./scheduler";
export interface TasksJsonFile {
running: Task[];
pending: Task[];
}
export class Persistence {
public CrawlerDirPath = path.join(process.cwd(), ".crawler");
public TaskFilePath = path.join(this.CrawlerDirPath, "task.json");
constructor(private scheduler: Scheduler) {
fs.ensureDirSync(this.CrawlerDirPath);
fs.ensureFileSync(this.TaskFilePath);
}
/**
* Load the last tasks from file
* @returns Whether load success or not
*/
public load(): boolean {
try {
const { running, pending } = fs.readJsonSync(
this.TaskFilePath
) as TasksJsonFile;
if (!running || !pending) {
return false;
}
if (!running.length && !pending.length) {
return false;
}
if (running && running.length) {
for (const task of running) {
this.scheduler.push(task);
}
}
if (pending && pending.length) {
for (const task of pending) {
this.scheduler.push(task);
}
}
return true;
} catch {
return false;
}
}
/**
* Sync current task to file
* @param runningTasks
* @param pendingTasks
*/
public sync(runningTasks: Task[], pendingTasks: Task[]) {
const json: TasksJsonFile = {
running: runningTasks,
pending: pendingTasks
};
fs.writeJsonSync(this.TaskFilePath, json, {
spaces: 2,
replacer: null
});
}
}