forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeployQueueServer.ts
99 lines (86 loc) · 2.99 KB
/
DeployQueueServer.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Deploy, DeployChange, DeployStatus } from "../clientUtils/owidTypes.js"
import * as fs from "fs-extra"
import {
DEPLOY_QUEUE_FILE_PATH,
DEPLOY_PENDING_FILE_PATH,
} from "../settings/serverSettings.js"
export class DeployQueueServer {
constructor(
queueFilePath = DEPLOY_QUEUE_FILE_PATH,
pendingFilePath = DEPLOY_PENDING_FILE_PATH
) {
this.queueFilePath = queueFilePath
this.pendingFilePath = pendingFilePath
}
private queueFilePath: string
private pendingFilePath: string
// File manipulation
private async readQueuedFile() {
return await fs.readFile(this.queueFilePath, "utf8")
}
private async readPendingFile() {
if (await fs.pathExists(this.pendingFilePath))
return await fs.readFile(this.pendingFilePath, "utf8")
return undefined
}
async readQueuedAndPendingFiles() {
const queueContent = await this.readQueuedFile()
const pendingContent = await this.readPendingFile()
// If any deploys didn't exit cleanly, this.pendingFilePath would exist.
// Prepend that message to the current deploy.
return pendingContent
? pendingContent + "\n" + queueContent
: queueContent
}
async enqueueChange(item: DeployChange) {
await fs.appendFile(this.queueFilePath, JSON.stringify(item) + "\n")
}
async clearQueueFile() {
await fs.truncate(this.queueFilePath, 0)
}
async writePendingFile(content: string) {
await fs.writeFile(this.pendingFilePath, content)
}
async deletePendingFile() {
await fs.unlink(this.pendingFilePath)
}
// Parsing queue content
async queueIsEmpty() {
const res = await this.readQueuedAndPendingFiles()
return !res
}
// Parse all lines in file as JSON
parseQueueContent(content: string): DeployChange[] {
return content
.split("\n")
.map((line) => {
try {
return JSON.parse(line)
} catch (err) {
return null
}
})
.filter((x) => x)
}
async getDeploys() {
const [queueContent, pendingContent] = await Promise.all([
this.readQueuedFile(),
this.readPendingFile(),
])
const deploys: Deploy[] = []
if (queueContent)
deploys.push({
status: DeployStatus.queued,
// Changes are always appended. Reversing them means the latest changes are first
// (which is what we want in the UI).
// We can't sort by time because the presence of "time" is not guaranteed.
changes: this.parseQueueContent(queueContent).reverse(),
})
if (pendingContent)
deploys.push({
status: DeployStatus.pending,
changes: this.parseQueueContent(pendingContent).reverse(),
})
return deploys
}
}