-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.js
203 lines (198 loc) · 6.55 KB
/
services.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { TextLineStream } from "./deps.js"
export class ServiceManager {
constructor (services) {
this.services = services
// Define routes from services
this.routes = []
for (const [id, service] of Object.entries(services)) {
this.routes.push([`/${id}/start`, () => { service.start() }])
this.routes.push([`/${id}/stop`, () => { service.stop() }])
this.routes.push([`/${id}/mute`, () => { service.mute() }])
this.routes.push([`/${id}/unmute`, () => { service.unmute() }])
}
}
listen (config, getInfo) {
// Run service
Deno.serve(config, async (req) => {
try {
// Service status
const info = getInfo()
// Trim trailing slashes
let { pathname } = new URL(req.url)
while (pathname.endsWith('/')) pathname = pathname.slice(0, pathname.length - 1)
// Route request to service
for (const [route, handler] of this.routes) {
if (route === pathname) {
return await Promise.resolve(handler(req)) || redirect('/')
}
}
// Default routes
switch (pathname) {
case '': return respond(200, info)
default: return respond(404, { error: 'not found' })
}
} catch (e) {
// Error handler
console.error(e)
return respond(500, { error: e.message||e })
}
// Return a redirect to another path on the same host
function redirect (pathname) {
const url = new URL(req.url)
url.pathname = pathname
return Response.redirect(url)
}
// Respond with JSON data
})
}
}
export function respond (status, data) {
const headers = { "content-type": "application/json" }
return new Response(JSON.stringify(data, null, 2), { status, headers })
}
export class LogPipe {
muted = false
mute () { this.muted = true }
unmute () { this.muted = false }
pipe (stream, _kind) {
stream
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream())
.pipeTo(new WritableStream({ write: (chunk, _) => {
//this.muted || console.log(`:: ${this.name} :: ${kind} :: ${chunk}`)
this.muted || console.log(chunk)
}}))
}
}
export class Service extends LogPipe {
constructor (name, command, ...args) {
super()
this.name = name
this.command = command
this.args = args
this.process = null
this.signal = 'SIGTERM'
}
async state () {
const cmd = 'pgrep'
const args = [ '-Acx', this.command ]
const opts = { args, stdin: 'null', stdout: 'null', stderr: 'null' }
const status = await new Deno.Command(cmd, opts).spawn().status
return status.success
}
async start () {
console.log('🚀 Starting:', this.name)
if (this.process) {
console.log('🚀 Already started:', this.name)
return false
}
const options = { args: this.args, stdout: 'piped', stderr: 'piped' }
// Spawn child process
this.process = new Deno.Command(this.command, options).spawn()
// Listen for process exit
this.process.status.then(status=>{
console.log(
'🟠 Died:', this.name,
'with PID:', this.process.pid,
'and status:', JSON.stringify(status)
)
this.process = null
})
console.log('🚀 Started: ', this.name, 'at PID:', this.process.pid)
// Write service stdout and stderr to host stdout
this.pipe(this.process.stdout, "stdout")
this.pipe(this.process.stderr, "stderr")
return await this.state()
}
async pause () {
console.log('🟠 Stopping:', this.name)
if (!this.process) {
console.log('🟠 Already stopped:', this.name)
return false
}
const { pid } = this.process
await new Deno.Command('pkill', { args: ['-9', this.command] }).spawn().status
console.log('🟠 Stopped:', this.name, 'at PID:', pid)
return await this.state()
}
routes () {
return {
'/': async (_) => respond(200, await this.state()),
'/start': async (_) => respond(200, await this.start()),
'/pause': async (_) => respond(200, await this.pause()),
}
}
}
export class MultiService extends LogPipe {
constructor (name, commands) {
super()
this.name = name
this.commands = commands
console.log('🚀 Init', this.name, 'with', this.commands)
this.processes = commands.map(_=>null)
}
async state () {
const commands = [...new Set(this.commands.map(command=>command[0]))]
return await Promise.all(commands.map(async command=>{
const cmd = 'pgrep'
const args = [ '-Acx', command[0] ]
const opts = { args, stdin: 'null', stdout: 'null', stderr: 'null' }
const status = await new Deno.Command(cmd, opts).spawn().status
return status.success
}))
}
async start () {
console.log('🚀 Starting:', this.name)
if (this.processes.every(Boolean)) {
console.log('🚀 Already started:', this.name)
return false
}
if (this.processes.some(Boolean)) {
console.log('🟠 Partially started, killing all and restarted:', this.name)
await this.pause()
}
// Spawn each child process
for (const c in this.commands) {
const [command, ...args] = this.commands[c]
console.log('🚀 Spawning:', this.commands[c])
this.processes[c] = new Deno.Command(command, {
args,
stdout: 'piped',
stderr: 'piped',
}).spawn()
// Listen for process exit
this.processes[c].status.then(status=>{
console.log(
'🟠 Died:', this.name,
'with PID:', this.processes[c].pid,
'and status:', JSON.stringify(status)
)
this.processes[c] = null
})
console.log('🚀 Started: ', this.name, 'at PID:', this.processes[c].pid)
// Write service stdout and stderr to host stdout
this.pipe(this.processes[c].stdout, "stdout")
this.pipe(this.processes[c].stderr, "stderr")
}
return await this.state()
}
async pause () {
console.log('🟠 Stopping:', this.name)
if (this.processes.every(x=>Boolean(x)===false)) {
console.log('🟠 Already stopped:', this.name)
return false
}
await Promise.all(this.commands.map(async ([command])=>{
await new Deno.Command('pkill', { args: ['-e', '-9', command] }).spawn().status
console.log('🟠 Stopped:', this.name)
}))
return await this.state()
}
routes () {
return {
'/': async (_) => respond(200, await this.state()),
'/start': async (_) => respond(200, await this.start()),
'/pause': async (_) => respond(200, await this.pause()),
}
}
}