-
Notifications
You must be signed in to change notification settings - Fork 8
/
CronRunner.js
63 lines (54 loc) · 1.83 KB
/
CronRunner.js
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
const path = require("path")
const { ScrollFile, ScrollFileSystem } = require("scroll-cli/scroll.js")
class CronRunner {
constructor(scrollHub) {
this.scrollHub = scrollHub
this.interval = 1 * 60 * 1000 // 1 minute default
this.isRunning = false
this.runningJobs = new Set()
this.minute = 0
}
start() {
if (this.isRunning) return
this.isRunning = true
this.timer = setInterval(() => this.checkCronFiles(), this.interval)
console.log("CronRunner started")
return this
}
stop() {
if (!this.isRunning) return
clearInterval(this.timer)
this.isRunning = false
console.log("CronRunner stopped")
}
async checkCronFiles() {
const { rootFolder, folderCache } = this.scrollHub
const folderNames = Object.keys(folderCache)
console.log(`Running Cron Loop. Minute ${this.minute}`)
this.minute++
for (const folderName of folderNames) {
try {
// Skip if already running
if (this.runningJobs.has(folderName)) continue
const cronJsPath = path.join(rootFolder, folderName, "cron.js")
const jsExists = await this.scrollHub.exists(cronJsPath)
if (jsExists) {
delete require.cache[require.resolve(cronJsPath)]
require(cronJsPath)
}
const cronPath = path.join(rootFolder, folderName, "cron.scroll")
const exists = await this.scrollHub.exists(cronPath)
if (!exists) continue
this.runningJobs.add(folderName)
const file = new ScrollFile(undefined, cronPath, new ScrollFileSystem())
await file.fuse()
await file.scrollProgram.buildAll()
this.runningJobs.delete(folderName)
} catch (err) {
console.error(`Error running cron for ${folderName}:`, err)
this.runningJobs.delete(folderName)
}
}
}
}
module.exports = { CronRunner }