@platformatic/runtime 1.36.0 → 1.36.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.
@@ -0,0 +1,14 @@
1
+ {
2
+ "$schema": "https://platformatic.dev/schemas/v1.20.0/runtime",
3
+ "entrypoint": "a",
4
+ "autoload": {
5
+ "path": "./services"
6
+ },
7
+ "server": {
8
+ "hostname": "127.0.0.1",
9
+ "port": "{PORT}",
10
+ "logger": {
11
+ "level": "info"
12
+ }
13
+ }
14
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "$schema": "https://platformatic.dev/schemas/v1.3.0/service",
3
+ "server": {
4
+ "logger": {
5
+ "level": "warn"
6
+ }
7
+ },
8
+ "plugins": {
9
+ "paths": [{
10
+ "path": "plugin.js",
11
+ "encapsulate": false
12
+ }]
13
+ }
14
+ }
@@ -0,0 +1,9 @@
1
+ 'use strict'
2
+
3
+ module.exports = async function (fastify, options) {
4
+ fastify.post('/crash', async (_, reply) => {
5
+ setImmediate(() => {
6
+ throw new Error('Crash!')
7
+ })
8
+ })
9
+ }
@@ -1,5 +1,5 @@
1
1
  {
2
- "$schema": "https://platformatic.dev/schemas/v1.20.0/runtime",
2
+ "$schema": "https://platformatic.dev/schemas/v1.36.0/runtime",
3
3
  "entrypoint": "a",
4
4
  "autoload": {
5
5
  "path": "./services"
package/lib/api-client.js CHANGED
@@ -88,8 +88,8 @@ class RuntimeApiClient extends EventEmitter {
88
88
  }
89
89
 
90
90
  async start () {
91
- this.started = true
92
91
  const address = await this.#sendCommand('plt:start-services')
92
+ this.started = true
93
93
  this.emit('start', address)
94
94
  return address
95
95
  }
package/lib/errors.js CHANGED
@@ -25,7 +25,6 @@ module.exports = {
25
25
  FailedToUnlinkManagementApiSocket: createError(`${ERROR_PREFIX}_FAILED_TO_UNLINK_MANAGEMENT_API_SOCKET`, 'Failed to unlink management API socket "%s"'),
26
26
  LogFileNotFound: createError(`${ERROR_PREFIX}_LOG_FILE_NOT_FOUND`, 'Log file with index %s not found', 404),
27
27
  CannotFindGeneratorForTemplateError: createError(`${ERROR_PREFIX}_CANNOT_FIND_GENERATOR_FOR_TEMPLATE`, 'Cannot find a generator for template "%s"'),
28
- WorkerExitCodeError: createError(`${ERROR_PREFIX}_WORKER_EXIT_CODE`, 'The worker exited with non-zero exit code "%s"'),
29
28
  WorkerIsRequired: createError(`${ERROR_PREFIX}_REQUIRED_WORKER`, 'The worker parameter is required'),
30
29
 
31
30
  // TODO: should remove next one as it's not used anymore
@@ -90,7 +90,7 @@ class RuntimeGenerator extends BaseGenerator {
90
90
  this.addServicesDependencies()
91
91
 
92
92
  this.addEnvVars({
93
- PLT_SERVER_HOSTNAME: '0.0.0.0',
93
+ PLT_SERVER_HOSTNAME: '127.0.0.1',
94
94
  PORT: this.config.port || 3042,
95
95
  PLT_SERVER_LOGGER_LEVEL: this.config.logLevel || 'info',
96
96
  PLT_MANAGEMENT_API: true
package/lib/schema.js CHANGED
@@ -172,6 +172,13 @@ const platformaticRuntimeSchema = {
172
172
  }
173
173
  ]
174
174
  },
175
+ restartOnError: {
176
+ default: true,
177
+ anyOf: [
178
+ { type: 'boolean' },
179
+ { type: 'string' }
180
+ ]
181
+ },
175
182
  services: {
176
183
  type: 'array',
177
184
  items: {
package/lib/start.js CHANGED
@@ -4,7 +4,6 @@ const { once } = require('node:events')
4
4
  const inspector = require('node:inspector')
5
5
  const { join, resolve, dirname } = require('node:path')
6
6
  const { writeFile } = require('node:fs/promises')
7
- const { setTimeout: sleep } = require('node:timers/promises')
8
7
  const { pathToFileURL } = require('node:url')
9
8
  const { Worker } = require('node:worker_threads')
10
9
  const { start: serviceStart } = require('@platformatic/service')
@@ -13,7 +12,6 @@ const closeWithGrace = require('close-with-grace')
13
12
  const { loadConfig } = require('./load-config')
14
13
  const { startManagementApi } = require('./management-api')
15
14
  const { startPrometheusServer } = require('./prom-server.js')
16
- const { WorkerExitCodeError } = require('./errors')
17
15
  const { parseInspectorOptions, wrapConfigInRuntimeConfig } = require('./config')
18
16
  const { RuntimeApiClient, getRuntimeLogsDir } = require('./api-client.js')
19
17
  const errors = require('./errors')
@@ -64,19 +62,6 @@ async function buildRuntime (configManager, env = process.env) {
64
62
 
65
63
  let managementApi = null
66
64
 
67
- let exiting = false
68
- closeWithGrace((event, cb) => {
69
- exiting = true
70
- worker.postMessage(event)
71
- worker.once('exit', (code) => {
72
- if (code !== 0) {
73
- cb(new WorkerExitCodeError(code))
74
- return
75
- }
76
- cb()
77
- })
78
- })
79
-
80
65
  if (config.hotReload) {
81
66
  /* c8 ignore next 3 */
82
67
  process.on('SIGUSR2', () => {
@@ -93,7 +78,7 @@ async function buildRuntime (configManager, env = process.env) {
93
78
  worker.on('exit', (code) => {
94
79
  // runtimeApiClient.started can be false if a stop command was issued
95
80
  // via the management API.
96
- if (exiting || !runtimeApiClient.started) {
81
+ if (config.restartOnError === false || !runtimeApiClient.started) {
97
82
  // We must stop those here in case the `closeWithGrace` callback
98
83
  // was not called.
99
84
  configManager.fileWatcher?.stopWatching()
@@ -103,8 +88,12 @@ async function buildRuntime (configManager, env = process.env) {
103
88
 
104
89
  worker = startWorker({ config, dirname, runtimeLogsDir }, env)
105
90
  setupExit()
106
- once(worker, 'message').then(() => {
107
- runtimeApiClient.setWorker(worker)
91
+
92
+ once(worker, 'message').then((msg) => {
93
+ runtimeApiClient.setWorker(worker).catch(() => {
94
+ // TODO: currently we restart if the worker fails to start intermitently
95
+ // should we limit this to a number of retries?
96
+ })
108
97
  })
109
98
  })
110
99
  }
@@ -162,7 +151,16 @@ async function startCommand (args) {
162
151
  runtime = await buildRuntime(wrappedConfig)
163
152
  }
164
153
 
165
- return await runtime.start()
154
+ const res = await runtime.start()
155
+
156
+ closeWithGrace(async (event) => {
157
+ if (event.err instanceof Error) {
158
+ console.error(event.err)
159
+ }
160
+ await runtime.close()
161
+ })
162
+
163
+ return res
166
164
  } catch (err) {
167
165
  if (err.code === 'PLT_CONFIG_NO_CONFIG_FILE_FOUND' && args.length === 1) {
168
166
  const config = {
@@ -188,12 +186,6 @@ async function startCommand (args) {
188
186
  return startCommand(['--config', toWrite])
189
187
  }
190
188
 
191
- if (err.code === 'PLT_RUNTIME_RUNTIME_EXIT') {
192
- console.log('Runtime exited before startup was completed, restarting')
193
- await sleep(1000)
194
- return startCommand(args)
195
- }
196
-
197
189
  if (err.filenames) {
198
190
  console.error(`Missing config file!
199
191
  Be sure to have a config file with one of the following names:
@@ -207,12 +199,7 @@ async function startCommand (args) {
207
199
  process.exit(1)
208
200
  }
209
201
 
210
- delete err?.stack
211
- console.error(err?.message)
212
-
213
- if (err?.cause) {
214
- console.error(`${err.cause}`)
215
- }
202
+ console.error(err)
216
203
 
217
204
  process.exit(1)
218
205
  }
@@ -0,0 +1,11 @@
1
+ 'use strict'
2
+
3
+ module.exports = {
4
+ version: '1.36.0',
5
+ up: function (config) {
6
+ if (config.restartOnError === undefined) {
7
+ config.restartOnError = false
8
+ }
9
+ return config
10
+ }
11
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/runtime",
3
- "version": "1.36.0",
3
+ "version": "1.36.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -34,8 +34,8 @@
34
34
  "typescript": "^5.4.2",
35
35
  "undici-oidc-interceptor": "^0.5.0",
36
36
  "why-is-node-running": "^2.2.2",
37
- "@platformatic/sql-graphql": "1.36.0",
38
- "@platformatic/sql-mapper": "1.36.0"
37
+ "@platformatic/sql-graphql": "1.36.1",
38
+ "@platformatic/sql-mapper": "1.36.1"
39
39
  },
40
40
  "dependencies": {
41
41
  "@fastify/error": "^3.4.1",
@@ -63,13 +63,13 @@
63
63
  "undici": "^6.9.0",
64
64
  "why-is-node-running": "^2.2.2",
65
65
  "ws": "^8.16.0",
66
- "@platformatic/db": "1.36.0",
67
- "@platformatic/config": "1.36.0",
68
- "@platformatic/composer": "1.36.0",
69
- "@platformatic/service": "1.36.0",
70
- "@platformatic/generators": "1.36.0",
71
- "@platformatic/telemetry": "1.36.0",
72
- "@platformatic/utils": "1.36.0"
66
+ "@platformatic/composer": "1.36.1",
67
+ "@platformatic/config": "1.36.1",
68
+ "@platformatic/db": "1.36.1",
69
+ "@platformatic/generators": "1.36.1",
70
+ "@platformatic/service": "1.36.1",
71
+ "@platformatic/telemetry": "1.36.1",
72
+ "@platformatic/utils": "1.36.1"
73
73
  },
74
74
  "standard": {
75
75
  "ignore": [