-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.js
163 lines (130 loc) · 4.83 KB
/
runner.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var assert = require('assert')
var events = require('events')
var posters = require('./posters')
posters.differenceNewsAndPriceUpdate = parseInt(process.env.CTC_DIFF_NEWS_PRICE)
const unitTime = parseInt(process.env.CTC_UNIT_TIME) // unit time in ms
class ScriptsControl {
constructor(scriptsList) {
assert.equal(scriptsList instanceof Object, true, 'invalid scripts list')
this.scriptsList = scriptsList
this.scriptNames = Object.keys(scriptsList)
this.running = false
this.finished = false
this.runningScriptName = ''
this.lastRuntScriptName = ''
this.currentScriptRun = null
}
startScript(scriptName) {
assert.notStrictEqual(this.scriptsList[scriptName], undefined, 'invalid script name')
assert.strictEqual(this.currentScriptRun, null, 'there is already a script running, cannot start another unless it stops')
this.currentScriptRun = new ScriptRun(scriptName, this.scriptsList[scriptName])
this.currentScriptRun.assignScriptsControl(this)
this.currentScriptRun.start()
this.running = true
this.finished = false
this.runningScriptName = scriptName
}
stopCurrentlyRunningScript() {
if(this.currentScriptRun != null) {
this.currentScriptRun.stop()
}
this.running = false
this.currentScriptRun = null
this.lastRuntScriptName = this.runningScriptName
this.runningScriptName = ''
}
runningScriptFinished() {
this.running = false
this.finished = true
this.currentScriptRun = null
this.lastRuntScriptName = this.runningScriptName
this.runningScriptName = ''
}
status() {
return this.scriptNames.map((scriptName) => {
return {
name: scriptName,
running: (this.running) ? ((this.runningScriptName == scriptName) ? true : false) : false,
finished: (this.finished) ? ((this.lastRuntScriptName == scriptName) ? true : false) : false
}
}).reduce((previousItem, currentItem) => {
previousItem[currentItem.name] = currentItem.running ? "running" : (currentItem.finished ? "finished" : "not running")
return previousItem
}, {})
}
}
class ScriptRun {
constructor(scriptName, script) {
assert.equal(typeof scriptName, 'string', 'invalid script name')
assert.equal(script instanceof Array, true, 'invalid script')
this.scriptName = scriptName
this.script = script
this._runner = null
this.jobs = new events.EventEmitter()
this.eventsInit()
this.progressPercentage = 0
this.masterSwitch = false
}
assignScriptsControl(runner) {
this._runner = runner
}
updateProgress(runningIndex) {
this.progressPercentage = (runningIndex / this.script.length) * 100
}
eventsInit() {
this.jobs.on('start', () => {
console.log(`[${new Date().toString()}] started ${this.scriptName}.`)
})
this.jobs.on('finish', () => {
console.log(`[${new Date().toString()}] finished with ${this.scriptName}.`)
if(this._runner != null) {
this._runner.runningScriptFinished()
}
})
this.jobs.on('error', (err) => {
console.log(`[${new Date().toString()}] error occured at ${this.scriptName}.`)
console.error(err)
if(this._runner != null) {
this._runner.runningScriptFinished()
}
})
this.jobs.on('stop', () => {
console.log(`[${new Date().toString()}] stopped ${this.scriptName}, ${this.progressPercentage}% rolled.`)
})
}
start() {
this.masterSwitch = true
var runningIndex = 0
var previousTimeId = 0
var self = this
var repeatRecurse = function() {
self.updateProgress(runningIndex)
var currentScriptItem = self.script[runningIndex]
if(self.masterSwitch) {
if(runningIndex < self.script.length) {
setTimeout(() => {
posters.chainUpdate(currentScriptItem, () => {})
repeatRecurse()
}, (currentScriptItem.timeId - previousTimeId) * unitTime)
previousTimeId = currentScriptItem.timeId
runningIndex++
}
else {
self.jobs.emit('finish')
}
}
else {
self.jobs.emit('stop')
}
}
this.jobs.emit('start')
repeatRecurse()
}
stop() {
this.masterSwitch = false
}
}
module.exports = {
ScriptsControl: ScriptsControl,
ScriptRun: ScriptRun
}