@platformatic/service 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.taprc +1 -1
- package/index.js +29 -17
- package/lib/start.mjs +17 -17
- package/package.json +3 -3
- package/test/cli/helper.mjs +1 -1
- package/test/cli/watch.test.mjs +244 -0
package/.taprc
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
timeout:
|
|
1
|
+
timeout: 160
|
|
2
2
|
coverage: false
|
package/index.js
CHANGED
|
@@ -33,6 +33,19 @@ async function platformaticService (app, opts, toLoad = []) {
|
|
|
33
33
|
app.decorate('platformatic', {})
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
{
|
|
37
|
+
const fileWatcher = opts.fileWatcher
|
|
38
|
+
const configManager = opts.configManager
|
|
39
|
+
|
|
40
|
+
if (fileWatcher !== undefined) {
|
|
41
|
+
app.platformatic.fileWatcher = fileWatcher
|
|
42
|
+
}
|
|
43
|
+
if (configManager !== undefined) {
|
|
44
|
+
app.platformatic.configManager = configManager
|
|
45
|
+
app.platformatic.config = configManager.current
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
36
49
|
if (opts.plugin) {
|
|
37
50
|
let pluginOptions = opts.plugin
|
|
38
51
|
/* c8 ignore next 4 */
|
|
@@ -116,7 +129,12 @@ async function buildServer (options, app = platformaticService) {
|
|
|
116
129
|
|
|
117
130
|
const _restart = handler.restart
|
|
118
131
|
|
|
119
|
-
|
|
132
|
+
let debounce = null
|
|
133
|
+
handler.restart = (opts) => {
|
|
134
|
+
if (debounce) {
|
|
135
|
+
return debounce
|
|
136
|
+
}
|
|
137
|
+
|
|
120
138
|
addLoggerToTheConfig(opts)
|
|
121
139
|
|
|
122
140
|
// Ignore because not tested on Windows
|
|
@@ -124,26 +142,20 @@ async function buildServer (options, app = platformaticService) {
|
|
|
124
142
|
// this on Windows
|
|
125
143
|
/* c8 ignore start */
|
|
126
144
|
if (opts) {
|
|
127
|
-
opts = createServerConfig(opts)
|
|
128
|
-
opts.app = app
|
|
129
|
-
|
|
130
145
|
const fileWatcher = handler.app.platformatic.fileWatcher
|
|
131
146
|
const configManager = handler.app.platformatic.configManager
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
handler.app.platformatic.fileWatcher = fileWatcher
|
|
137
|
-
}
|
|
138
|
-
if (configManager !== undefined) {
|
|
139
|
-
handler.app.platformatic.configManager = configManager
|
|
140
|
-
handler.app.platformatic.config = configManager.current
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return handler
|
|
147
|
+
opts.fileWatcher = fileWatcher
|
|
148
|
+
opts.configManager = configManager
|
|
149
|
+
opts = createServerConfig(opts)
|
|
150
|
+
opts.app = app
|
|
144
151
|
}
|
|
152
|
+
debounce = _restart(opts).then(() => {
|
|
153
|
+
handler.app.log.info('restarted')
|
|
154
|
+
}).finally(() => {
|
|
155
|
+
debounce = null
|
|
156
|
+
})
|
|
145
157
|
/* c8 ignore stop */
|
|
146
|
-
return
|
|
158
|
+
return debounce
|
|
147
159
|
}
|
|
148
160
|
|
|
149
161
|
return handler
|
package/lib/start.mjs
CHANGED
|
@@ -34,9 +34,9 @@ async function start (_args) {
|
|
|
34
34
|
configManager
|
|
35
35
|
})
|
|
36
36
|
|
|
37
|
-
configManager.on('update', (newConfig) => onConfigUpdated(newConfig, server))
|
|
38
37
|
server.app.platformatic.configManager = configManager
|
|
39
38
|
server.app.platformatic.config = config
|
|
39
|
+
configManager.on('update', (newConfig) => onConfigUpdated(newConfig, server))
|
|
40
40
|
|
|
41
41
|
if (
|
|
42
42
|
config.plugin !== undefined &&
|
|
@@ -58,9 +58,6 @@ async function start (_args) {
|
|
|
58
58
|
process.on('SIGUSR2', function () {
|
|
59
59
|
server.app.log.info('reloading configuration')
|
|
60
60
|
server.restart()
|
|
61
|
-
.then(() => {
|
|
62
|
-
server.app.log.info('restarted')
|
|
63
|
-
})
|
|
64
61
|
.catch((err) => {
|
|
65
62
|
server.app.log.error({
|
|
66
63
|
err: {
|
|
@@ -103,7 +100,7 @@ async function startFileWatching (server) {
|
|
|
103
100
|
})
|
|
104
101
|
fileWatcher.startWatching()
|
|
105
102
|
|
|
106
|
-
server.app.log.
|
|
103
|
+
server.app.log.debug('start watching files')
|
|
107
104
|
server.app.platformatic.fileWatcher = fileWatcher
|
|
108
105
|
}
|
|
109
106
|
|
|
@@ -112,7 +109,7 @@ async function stopFileWatching (server) {
|
|
|
112
109
|
if (fileWatcher !== undefined) {
|
|
113
110
|
await fileWatcher.stopWatching()
|
|
114
111
|
|
|
115
|
-
server.app.log.
|
|
112
|
+
server.app.log.debug('stop watching files')
|
|
116
113
|
server.app.platformatic.fileWatcher = undefined
|
|
117
114
|
}
|
|
118
115
|
}
|
|
@@ -120,18 +117,11 @@ async function stopFileWatching (server) {
|
|
|
120
117
|
async function onConfigUpdated (newConfig, server) {
|
|
121
118
|
try {
|
|
122
119
|
server.app.platformatic.config = newConfig
|
|
123
|
-
server.app.log.
|
|
120
|
+
server.app.log.debug('config changed')
|
|
124
121
|
server.app.log.trace({ newConfig }, 'new config')
|
|
125
122
|
|
|
126
123
|
await stopFileWatching(server)
|
|
127
124
|
|
|
128
|
-
if (
|
|
129
|
-
newConfig.plugin !== undefined &&
|
|
130
|
-
newConfig.plugin.watch !== false
|
|
131
|
-
) {
|
|
132
|
-
await startFileWatching(server)
|
|
133
|
-
}
|
|
134
|
-
|
|
135
125
|
await server.restart(newConfig)
|
|
136
126
|
} catch (err) {
|
|
137
127
|
// TODO: test this
|
|
@@ -141,14 +131,24 @@ async function onConfigUpdated (newConfig, server) {
|
|
|
141
131
|
stack: err.stack
|
|
142
132
|
}
|
|
143
133
|
}, 'failed to reload config')
|
|
134
|
+
} finally {
|
|
135
|
+
if (
|
|
136
|
+
newConfig.plugin !== undefined &&
|
|
137
|
+
newConfig.plugin.watch !== false
|
|
138
|
+
) {
|
|
139
|
+
await startFileWatching(server)
|
|
140
|
+
}
|
|
144
141
|
}
|
|
145
142
|
}
|
|
146
143
|
|
|
147
144
|
async function onFilesUpdated (server) {
|
|
145
|
+
// Reload the config as well, otherwise we will have problems
|
|
146
|
+
// in case the files watcher triggers the config watcher too
|
|
147
|
+
const configManager = server.app.platformatic.configManager
|
|
148
148
|
try {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
await server.restart(
|
|
149
|
+
server.app.log.debug('files changed')
|
|
150
|
+
await configManager.parse()
|
|
151
|
+
await server.restart(configManager.current)
|
|
152
152
|
} catch (err) {
|
|
153
153
|
// TODO: test this
|
|
154
154
|
server.app.log.error({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/service",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "Matteo Collina <hello@matteocollina.com>",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"@fastify/static": "^6.5.0",
|
|
35
35
|
"@fastify/swagger": "^8.0.0",
|
|
36
36
|
"@fastify/under-pressure": "^8.0.0",
|
|
37
|
-
"@platformatic/config": "0.6.
|
|
38
|
-
"@platformatic/utils": "0.6.
|
|
37
|
+
"@platformatic/config": "0.6.1",
|
|
38
|
+
"@platformatic/utils": "0.6.1",
|
|
39
39
|
"close-with-grace": "^1.1.0",
|
|
40
40
|
"commist": "^3.1.2",
|
|
41
41
|
"desm": "^1.2.0",
|
package/test/cli/helper.mjs
CHANGED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import os from 'os'
|
|
2
|
+
import { join, basename } from 'path'
|
|
3
|
+
import { writeFile, mkdtemp } from 'fs/promises'
|
|
4
|
+
import { setTimeout as sleep } from 'timers/promises'
|
|
5
|
+
import t, { test } from 'tap'
|
|
6
|
+
import { request } from 'undici'
|
|
7
|
+
import { start } from './helper.mjs'
|
|
8
|
+
|
|
9
|
+
t.jobs = 5
|
|
10
|
+
|
|
11
|
+
function createLoggingPlugin (text) {
|
|
12
|
+
return `\
|
|
13
|
+
module.exports = async (app) => {
|
|
14
|
+
app.get('/version', () => '${text}')
|
|
15
|
+
}
|
|
16
|
+
`
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
test('should watch js files by default', async ({ equal, teardown }) => {
|
|
20
|
+
const tmpDir = await mkdtemp(join(os.tmpdir(), 'watch-'))
|
|
21
|
+
const pluginFilePath = join(tmpDir, 'plugin.js')
|
|
22
|
+
const configFilePath = join(tmpDir, 'platformatic.service.json')
|
|
23
|
+
|
|
24
|
+
const defaultConfig = {
|
|
25
|
+
server: {
|
|
26
|
+
logger: {
|
|
27
|
+
level: 'info'
|
|
28
|
+
},
|
|
29
|
+
hostname: '127.0.0.1',
|
|
30
|
+
port: 0
|
|
31
|
+
},
|
|
32
|
+
plugin: {
|
|
33
|
+
path: pluginFilePath,
|
|
34
|
+
watch: true
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
await Promise.all([
|
|
39
|
+
writeFile(configFilePath, JSON.stringify(defaultConfig)),
|
|
40
|
+
writeFile(pluginFilePath, createLoggingPlugin('v1'))
|
|
41
|
+
])
|
|
42
|
+
|
|
43
|
+
const { child, url } = await start('-c', configFilePath)
|
|
44
|
+
teardown(() => child.kill('SIGINT'))
|
|
45
|
+
|
|
46
|
+
await writeFile(pluginFilePath, createLoggingPlugin('v2'))
|
|
47
|
+
|
|
48
|
+
await sleep(5000)
|
|
49
|
+
|
|
50
|
+
const res = await request(`${url}/version`)
|
|
51
|
+
const version = await res.body.text()
|
|
52
|
+
equal(version, 'v2')
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('should watch allowed file', async ({ comment, teardown }) => {
|
|
56
|
+
const tmpDir = await mkdtemp(join(os.tmpdir(), 'watch-'))
|
|
57
|
+
const jsonFilePath = join(tmpDir, 'plugin-config.json')
|
|
58
|
+
const pluginFilePath = join(tmpDir, 'plugin.js')
|
|
59
|
+
const configFilePath = join(tmpDir, 'platformatic.service.json')
|
|
60
|
+
|
|
61
|
+
const config = {
|
|
62
|
+
server: {
|
|
63
|
+
logger: {
|
|
64
|
+
level: 'info'
|
|
65
|
+
},
|
|
66
|
+
hostname: '127.0.0.1',
|
|
67
|
+
port: 0
|
|
68
|
+
},
|
|
69
|
+
plugin: {
|
|
70
|
+
path: pluginFilePath,
|
|
71
|
+
watch: true,
|
|
72
|
+
watchOptions: {
|
|
73
|
+
allow: ['*.js', '*.json']
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const pluginCode = `\
|
|
79
|
+
const readFileSync = require('fs').readFileSync
|
|
80
|
+
const json = readFileSync(${JSON.stringify(jsonFilePath)}, 'utf8')
|
|
81
|
+
|
|
82
|
+
module.exports = async function (app) {
|
|
83
|
+
if (json === 'RESTARTED') {
|
|
84
|
+
app.log.info('RESTARTED')
|
|
85
|
+
}
|
|
86
|
+
}`
|
|
87
|
+
|
|
88
|
+
await Promise.all([
|
|
89
|
+
writeFile(configFilePath, JSON.stringify(config)),
|
|
90
|
+
writeFile(jsonFilePath, 'INITIAL'),
|
|
91
|
+
writeFile(pluginFilePath, pluginCode)
|
|
92
|
+
])
|
|
93
|
+
|
|
94
|
+
const { child } = await start('-c', configFilePath)
|
|
95
|
+
teardown(() => child.kill('SIGINT'))
|
|
96
|
+
|
|
97
|
+
writeFile(jsonFilePath, 'RESTARTED')
|
|
98
|
+
for await (const log of child.ndj) {
|
|
99
|
+
if (log.msg === 'RESTARTED') break
|
|
100
|
+
}
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
test('should not watch ignored file', async ({ teardown, equal }) => {
|
|
104
|
+
const tmpDir = await mkdtemp(join(os.tmpdir(), 'watch-'))
|
|
105
|
+
const pluginFilePath = join(tmpDir, 'plugin.js')
|
|
106
|
+
const configFilePath = join(tmpDir, 'platformatic.service.json')
|
|
107
|
+
|
|
108
|
+
const config = {
|
|
109
|
+
server: {
|
|
110
|
+
logger: {
|
|
111
|
+
level: 'info'
|
|
112
|
+
},
|
|
113
|
+
hostname: '127.0.0.1',
|
|
114
|
+
port: 0
|
|
115
|
+
},
|
|
116
|
+
plugin: {
|
|
117
|
+
path: pluginFilePath,
|
|
118
|
+
watch: true,
|
|
119
|
+
watchOptions: {
|
|
120
|
+
ignore: [basename(pluginFilePath)]
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
await Promise.all([
|
|
126
|
+
writeFile(configFilePath, JSON.stringify(config)),
|
|
127
|
+
writeFile(pluginFilePath, createLoggingPlugin('v1'))
|
|
128
|
+
])
|
|
129
|
+
|
|
130
|
+
const { child, url } = await start('-c', configFilePath)
|
|
131
|
+
teardown(() => child.kill('SIGINT'))
|
|
132
|
+
|
|
133
|
+
await writeFile(pluginFilePath, createLoggingPlugin('v2'))
|
|
134
|
+
await sleep(5000)
|
|
135
|
+
|
|
136
|
+
const res = await request(`${url}/version`)
|
|
137
|
+
const version = await res.body.text()
|
|
138
|
+
equal(version, 'v1')
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
test('should not loop forever when doing ESM', async ({ comment, fail }) => {
|
|
142
|
+
const tmpDir = await mkdtemp(join(os.tmpdir(), 'watch-esm-'))
|
|
143
|
+
const pluginFilePath = join(tmpDir, 'plugin.mjs')
|
|
144
|
+
const configFilePath = join(tmpDir, 'platformatic.service.json')
|
|
145
|
+
|
|
146
|
+
const config = {
|
|
147
|
+
server: {
|
|
148
|
+
logger: {
|
|
149
|
+
level: 'info'
|
|
150
|
+
},
|
|
151
|
+
hostname: '127.0.0.1',
|
|
152
|
+
port: 0
|
|
153
|
+
},
|
|
154
|
+
plugin: {
|
|
155
|
+
path: pluginFilePath,
|
|
156
|
+
watch: true,
|
|
157
|
+
watchOptions: {
|
|
158
|
+
ignore: [basename(pluginFilePath)]
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
await Promise.all([
|
|
164
|
+
writeFile(configFilePath, JSON.stringify(config)),
|
|
165
|
+
writeFile(pluginFilePath, 'export default async (app) => {}')
|
|
166
|
+
])
|
|
167
|
+
|
|
168
|
+
const { child } = await start('-c', configFilePath)
|
|
169
|
+
|
|
170
|
+
await sleep(1000)
|
|
171
|
+
|
|
172
|
+
child.kill('SIGINT')
|
|
173
|
+
|
|
174
|
+
let linesCounter = 0
|
|
175
|
+
for await (const line of child.ndj) {
|
|
176
|
+
// lines will have a series of "config changed"
|
|
177
|
+
// messages without an ignore
|
|
178
|
+
comment(line.msg)
|
|
179
|
+
if (++linesCounter > 2) {
|
|
180
|
+
fail()
|
|
181
|
+
break
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
test('should watch config file', async ({ comment, teardown }) => {
|
|
187
|
+
const tmpDir = await mkdtemp(join(os.tmpdir(), 'watch-config-'))
|
|
188
|
+
const pluginFilePath = join(tmpDir, 'plugin.js')
|
|
189
|
+
const configFilePath = join(tmpDir, 'platformatic.service.json')
|
|
190
|
+
|
|
191
|
+
const config = {
|
|
192
|
+
server: {
|
|
193
|
+
logger: {
|
|
194
|
+
level: 'info'
|
|
195
|
+
},
|
|
196
|
+
hostname: '127.0.0.1',
|
|
197
|
+
port: 0
|
|
198
|
+
},
|
|
199
|
+
plugin: {
|
|
200
|
+
path: pluginFilePath,
|
|
201
|
+
watch: true,
|
|
202
|
+
watchOptions: {
|
|
203
|
+
allow: ['*.js', '*.json']
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const config2 = {
|
|
209
|
+
server: {
|
|
210
|
+
logger: {
|
|
211
|
+
level: 'info'
|
|
212
|
+
},
|
|
213
|
+
hostname: '127.0.0.1',
|
|
214
|
+
port: 0
|
|
215
|
+
},
|
|
216
|
+
plugin: {
|
|
217
|
+
path: pluginFilePath,
|
|
218
|
+
options: {
|
|
219
|
+
log: true
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const pluginCode = `\
|
|
225
|
+
module.exports = async function (app, opts) {
|
|
226
|
+
if (opts.log) {
|
|
227
|
+
app.log.info('RESTARTED')
|
|
228
|
+
}
|
|
229
|
+
}`
|
|
230
|
+
|
|
231
|
+
await Promise.all([
|
|
232
|
+
writeFile(configFilePath, JSON.stringify(config)),
|
|
233
|
+
writeFile(pluginFilePath, pluginCode)
|
|
234
|
+
])
|
|
235
|
+
|
|
236
|
+
const { child } = await start('-c', configFilePath)
|
|
237
|
+
teardown(() => child.kill('SIGINT'))
|
|
238
|
+
|
|
239
|
+
// We do not await
|
|
240
|
+
writeFile(configFilePath, JSON.stringify(config2))
|
|
241
|
+
for await (const log of child.ndj) {
|
|
242
|
+
if (log.msg === 'RESTARTED') break
|
|
243
|
+
}
|
|
244
|
+
})
|