@platformatic/runtime 2.0.0-alpha.2 → 2.0.0-alpha.20

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.
Files changed (177) hide show
  1. package/config.d.ts +218 -0
  2. package/eslint.config.js +8 -0
  3. package/fixtures/botched-start/platformatic.runtime.json +1 -1
  4. package/fixtures/botched-start/services/a/platformatic.service.json +1 -1
  5. package/fixtures/composerApp/platformatic.composer.json +1 -1
  6. package/fixtures/configs/invalid-entrypoint.json +1 -1
  7. package/fixtures/configs/invalid-schema-type.config.json +1 -1
  8. package/fixtures/configs/{invalid-autoload-with-services.json → invalid-web-with-services.json} +9 -5
  9. package/fixtures/configs/missing-property.config.json +1 -1
  10. package/fixtures/configs/missing-service-config.json +1 -1
  11. package/fixtures/configs/monorepo-composer-no-autoload.json +2 -2
  12. package/fixtures/configs/monorepo-composer.json +2 -2
  13. package/fixtures/configs/monorepo-create-cycle.json +2 -2
  14. package/fixtures/configs/monorepo-missing-dependencies.json +2 -2
  15. package/fixtures/configs/monorepo-no-cycles.json +2 -2
  16. package/fixtures/configs/monorepo-openapi.json +2 -2
  17. package/fixtures/configs/{monorepo-hotreload-env.json → monorepo-watch-env.json} +2 -2
  18. package/fixtures/configs/monorepo-watch-single.json +12 -0
  19. package/fixtures/configs/monorepo-watch.json +24 -9
  20. package/fixtures/configs/monorepo-with-dependencies.json +2 -2
  21. package/fixtures/configs/monorepo-with-management-api-without-metrics.json +21 -0
  22. package/fixtures/configs/monorepo-with-management-api.json +2 -2
  23. package/fixtures/configs/{monorepo-hotreload.json → monorepo-with-metrics.json} +5 -4
  24. package/fixtures/configs/monorepo.json +2 -2
  25. package/fixtures/configs/no-services.config.json +1 -1
  26. package/fixtures/configs/no-sources.config.json +1 -1
  27. package/fixtures/configs/service-throws-on-start.json +1 -1
  28. package/fixtures/configs/service-with-env-port.json +2 -2
  29. package/fixtures/configs/service-with-stdio.json +12 -0
  30. package/fixtures/configs/{hotreload.json → watch.json} +2 -2
  31. package/fixtures/crash-on-bootstrap/platformatic.runtime.json +15 -0
  32. package/fixtures/crash-on-bootstrap/services/service-1/platformatic.service.json +14 -0
  33. package/fixtures/crash-on-bootstrap/services/service-1/plugin.js +5 -0
  34. package/fixtures/crash-on-bootstrap/services/service-2/platformatic.service.json +14 -0
  35. package/fixtures/crash-on-bootstrap/services/service-2/plugin.js +5 -0
  36. package/fixtures/dbApp/platformatic.db.json +1 -1
  37. package/fixtures/dbAppNoName/platformatic.db.json +1 -1
  38. package/fixtures/dbAppNoPackageJson/platformatic.db.json +1 -1
  39. package/fixtures/dbAppWithMigrationError/platformatic.db.json +1 -1
  40. package/fixtures/do-not-reload-dependencies/platformatic.service.json +1 -1
  41. package/fixtures/do-not-restart-on-crash/platformatic.runtime.json +3 -2
  42. package/fixtures/do-not-restart-on-crash/services/a/platformatic.service.json +8 -6
  43. package/fixtures/express/platformatic.runtime.json +1 -1
  44. package/fixtures/express/services/a/platformatic.service.json +1 -1
  45. package/fixtures/express/services/b/platformatic.service.json +1 -1
  46. package/fixtures/external-client/platformatic.service.json +1 -1
  47. package/fixtures/interceptors/idp.js +2 -2
  48. package/fixtures/interceptors/platformatic.runtime.json +1 -1
  49. package/fixtures/interceptors/services/a/platformatic.service.json +1 -1
  50. package/fixtures/interceptors-2/platformatic.runtime.json +1 -1
  51. package/fixtures/interceptors-2/services/a/platformatic.service.json +1 -1
  52. package/fixtures/leven/platformatic.runtime.json +2 -2
  53. package/fixtures/leven/services/deeply-spittle/platformatic.service.json +1 -1
  54. package/fixtures/leven/services/rainy-empire/platformatic.composer.json +1 -1
  55. package/fixtures/management-api/platformatic.json +8 -3
  56. package/fixtures/management-api/services/service-1/platformatic.json +8 -5
  57. package/fixtures/management-api/services/service-1/plugin.js +4 -3
  58. package/fixtures/management-api/services/service-2/platformatic.json +1 -1
  59. package/fixtures/management-api/services/service-db/platformatic.db.json +1 -1
  60. package/fixtures/management-api-custom-labels/platformatic.json +2 -2
  61. package/fixtures/management-api-custom-labels/services/service-1/platformatic.json +1 -1
  62. package/fixtures/management-api-custom-labels/services/service-1/plugin.js +4 -3
  63. package/fixtures/management-api-custom-labels/services/service-2/platformatic.json +1 -1
  64. package/fixtures/management-api-custom-labels/services/service-db/platformatic.db.json +1 -1
  65. package/fixtures/management-api-without-metrics/platformatic.json +3 -2
  66. package/fixtures/management-api-without-metrics/services/service-1/platformatic.json +1 -1
  67. package/fixtures/monorepo/composerApp/platformatic.composer.json +1 -1
  68. package/fixtures/monorepo/dbApp/platformatic.db.json +1 -1
  69. package/fixtures/monorepo/serviceApp/platformatic.service.json +3 -2
  70. package/fixtures/monorepo/serviceApp/with-logger/with-logger.cjs +2 -2
  71. package/fixtures/monorepo/serviceApp/with-logger/with-logger.d.ts +7 -7
  72. package/fixtures/monorepo/serviceAppWithLogger/platformatic.service.json +1 -1
  73. package/fixtures/monorepo/serviceAppWithLogger/plugin.js +12 -0
  74. package/fixtures/monorepo/serviceAppWithMultiplePlugins/platformatic.service.json +3 -2
  75. package/fixtures/monorepo-missing-dependencies/composer/platformatic.json +1 -1
  76. package/fixtures/monorepo-openapi/serviceAppWithoutOpenapi/platformatic.service.json +1 -1
  77. package/fixtures/monorepo-watch/service1/platformatic.service.json +1 -1
  78. package/fixtures/monorepo-with-dependencies/main/platformatic.json +1 -1
  79. package/fixtures/monorepo-with-dependencies/service-1/platformatic.json +1 -1
  80. package/fixtures/monorepo-with-dependencies/service-2/platformatic.json +1 -1
  81. package/fixtures/no-env.service.json +1 -1
  82. package/fixtures/preload/platformatic.runtime.json +1 -1
  83. package/fixtures/preload/services/a/platformatic.service.json +1 -1
  84. package/fixtures/prom-server/platformatic.json +2 -2
  85. package/fixtures/prom-server/services/service-1/platformatic.json +1 -1
  86. package/fixtures/prom-server/services/service-2/platformatic.json +1 -1
  87. package/fixtures/restart-on-crash/platformatic.runtime.json +1 -1
  88. package/fixtures/restart-on-crash/services/a/platformatic.service.json +8 -6
  89. package/fixtures/sample-runtime/package.json +3 -3
  90. package/fixtures/sample-runtime/platformatic.json +2 -2
  91. package/fixtures/sample-runtime/services/rival/package.json +2 -2
  92. package/fixtures/sample-runtime/services/rival/platformatic.json +1 -1
  93. package/fixtures/sample-runtime-with-2-services/package.json +3 -3
  94. package/fixtures/sample-runtime-with-2-services/platformatic.json +2 -2
  95. package/fixtures/sample-runtime-with-2-services/services/foobar/package.json +2 -2
  96. package/fixtures/sample-runtime-with-2-services/services/foobar/platformatic.json +1 -1
  97. package/fixtures/sample-runtime-with-2-services/services/rival/package.json +2 -2
  98. package/fixtures/sample-runtime-with-2-services/services/rival/platformatic.json +1 -1
  99. package/fixtures/server/logger-transport/platformatic.runtime.json +2 -2
  100. package/fixtures/server/logger-transport/services/echo/platformatic.service.json +1 -1
  101. package/fixtures/server/overrides-service/platformatic.runtime.json +2 -2
  102. package/fixtures/server/overrides-service/services/echo/platformatic.service.json +1 -1
  103. package/fixtures/server/runtime-server/platformatic.runtime.json +2 -2
  104. package/fixtures/server/runtime-server/services/echo/platformatic.service.json +1 -1
  105. package/fixtures/serviceAppThrowsOnStart/platformatic.service.json +1 -1
  106. package/fixtures/stackables/node_modules/foo/foo.js +2 -1
  107. package/fixtures/start-command-in-runtime.js +1 -1
  108. package/fixtures/stdio/platformatic.service.json +6 -0
  109. package/fixtures/stdio/plugin.js +24 -0
  110. package/fixtures/telemetry/platformatic.runtime.json +2 -2
  111. package/fixtures/telemetry/services/echo/platformatic.service.json +1 -1
  112. package/fixtures/telemetry/services/echo/routes/span.js +16 -2
  113. package/fixtures/telemetry/services/service-1/platformatic.service.json +19 -0
  114. package/fixtures/telemetry/services/service-1/routes/echo.js +7 -0
  115. package/fixtures/typescript/platformatic.runtime.json +2 -2
  116. package/fixtures/typescript/services/composer/platformatic.composer.json +1 -1
  117. package/fixtures/typescript/services/movies/global.d.ts +2 -3
  118. package/fixtures/typescript/services/movies/platformatic.db.json +1 -1
  119. package/fixtures/typescript/services/movies/types/Movie.d.ts +3 -3
  120. package/fixtures/typescript/services/movies/types/index.d.ts +6 -6
  121. package/fixtures/typescript/services/titles/client/client.d.ts +35 -35
  122. package/fixtures/typescript/services/titles/platformatic.service.json +1 -1
  123. package/fixtures/typescript-custom-flags/platformatic.runtime.json +2 -2
  124. package/fixtures/typescript-custom-flags/services/composer/platformatic.composer.json +1 -1
  125. package/fixtures/typescript-custom-flags/services/movies/global.d.ts +2 -3
  126. package/fixtures/typescript-custom-flags/services/movies/platformatic.db.json +1 -1
  127. package/fixtures/typescript-custom-flags/services/movies/types/Movie.d.ts +3 -3
  128. package/fixtures/typescript-custom-flags/services/movies/types/index.d.ts +6 -6
  129. package/fixtures/typescript-custom-flags/services/titles/client/client.d.ts +35 -35
  130. package/fixtures/typescript-custom-flags/services/titles/platformatic.service.json +1 -1
  131. package/fixtures/typescript-no-env/platformatic.runtime.json +2 -2
  132. package/fixtures/typescript-no-env/services/composer/platformatic.composer.json +1 -1
  133. package/fixtures/typescript-no-env/services/movies/global.d.ts +2 -3
  134. package/fixtures/typescript-no-env/services/movies/platformatic.db.json +1 -1
  135. package/fixtures/typescript-no-env/services/movies/types/Movie.d.ts +3 -3
  136. package/fixtures/typescript-no-env/services/movies/types/index.d.ts +6 -6
  137. package/fixtures/typescript-no-env/services/titles/client/client.d.ts +35 -35
  138. package/fixtures/typescript-no-env/services/titles/platformatic.service.json +1 -1
  139. package/index.d.ts +7 -8
  140. package/index.js +18 -10
  141. package/index.test-d.ts +10 -12
  142. package/lib/build-server.js +11 -13
  143. package/lib/compile.js +11 -10
  144. package/lib/config.js +32 -19
  145. package/lib/dependencies.js +2 -1
  146. package/lib/errors.js +6 -3
  147. package/lib/generator/errors.js +1 -1
  148. package/lib/generator/runtime-generator.d.ts +15 -15
  149. package/lib/generator/runtime-generator.js +95 -66
  150. package/lib/logger.js +55 -0
  151. package/lib/management-api.js +40 -46
  152. package/lib/prom-server.js +5 -9
  153. package/lib/runtime.js +1012 -0
  154. package/lib/schema.js +109 -91
  155. package/lib/start.js +51 -108
  156. package/lib/upgrade.js +2 -5
  157. package/lib/utils.js +65 -1
  158. package/lib/versions/v1.36.0.js +1 -1
  159. package/lib/versions/v1.5.0.js +1 -1
  160. package/lib/versions/v2.0.0.js +26 -0
  161. package/lib/worker/app.js +266 -0
  162. package/lib/worker/default-stackable.js +33 -0
  163. package/lib/worker/itc.js +162 -0
  164. package/lib/worker/main.js +135 -0
  165. package/lib/worker/metrics.js +106 -0
  166. package/lib/worker/symbols.js +7 -0
  167. package/package.json +36 -34
  168. package/runtime.mjs +5 -5
  169. package/schema.json +639 -0
  170. package/lib/api-client.js +0 -500
  171. package/lib/api.js +0 -420
  172. package/lib/app.js +0 -397
  173. package/lib/load-config.js +0 -12
  174. package/lib/loader.mjs +0 -103
  175. package/lib/message-port-writable.js +0 -50
  176. package/lib/worker.js +0 -182
  177. /package/lib/{interceptors.js → worker/interceptors.js} +0 -0
package/lib/schema.js CHANGED
@@ -2,11 +2,40 @@
2
2
  'use strict'
3
3
 
4
4
  const telemetry = require('@platformatic/telemetry').schema
5
- const { schemas: { server } } = require('@platformatic/utils')
5
+ const {
6
+ schemaComponents: { server, logger }
7
+ } = require('@platformatic/utils')
8
+
9
+ const services = {
10
+ type: 'array',
11
+ items: {
12
+ type: 'object',
13
+ anyOf: [{ required: ['id', 'path'] }, { required: ['id', 'url'] }],
14
+ properties: {
15
+ id: {
16
+ type: 'string'
17
+ },
18
+ path: {
19
+ type: 'string',
20
+ resolvePath: true
21
+ },
22
+ config: {
23
+ type: 'string'
24
+ },
25
+ url: {
26
+ type: 'string'
27
+ },
28
+ useHttp: {
29
+ type: 'boolean'
30
+ }
31
+ }
32
+ }
33
+ }
34
+
6
35
  const pkg = require('../package.json')
7
- const version = 'v' + pkg.version
36
+
8
37
  const platformaticRuntimeSchema = {
9
- $id: `https://platformatic.dev/schemas/${version}/runtime`,
38
+ $id: `https://schemas.platformatic.dev/@platformatic/runtime/${pkg.version}.json`,
10
39
  $schema: 'http://json-schema.org/draft-07/schema#',
11
40
  type: 'object',
12
41
  properties: {
@@ -17,6 +46,9 @@ const platformaticRuntimeSchema = {
17
46
  type: 'string',
18
47
  resolvePath: true
19
48
  },
49
+ entrypoint: {
50
+ type: 'string'
51
+ },
20
52
  autoload: {
21
53
  type: 'object',
22
54
  additionalProperties: false,
@@ -38,7 +70,7 @@ const platformaticRuntimeSchema = {
38
70
  additionalProperties: {
39
71
  type: 'object',
40
72
  additionalProperties: false,
41
- required: ['id', 'config'],
73
+ required: ['id'],
42
74
  properties: {
43
75
  id: {
44
76
  type: 'string'
@@ -54,38 +86,20 @@ const platformaticRuntimeSchema = {
54
86
  }
55
87
  }
56
88
  },
57
- telemetry,
89
+ services,
90
+ web: services,
91
+ logger,
58
92
  server,
59
- entrypoint: {
60
- type: 'string'
61
- },
62
- hotReload: {
93
+ restartOnError: {
94
+ default: true,
63
95
  anyOf: [
96
+ { type: 'boolean' },
64
97
  {
65
- type: 'boolean'
66
- },
67
- {
68
- type: 'string'
98
+ type: 'number',
99
+ minimum: 100
69
100
  }
70
101
  ]
71
102
  },
72
- inspectorOptions: {
73
- type: 'object',
74
- properties: {
75
- host: {
76
- type: 'string'
77
- },
78
- port: {
79
- type: 'number'
80
- },
81
- breakFirstLine: {
82
- type: 'boolean'
83
- },
84
- hotReloadDisabled: {
85
- type: 'boolean'
86
- }
87
- }
88
- },
89
103
  undici: {
90
104
  type: 'object',
91
105
  properties: {
@@ -94,37 +108,50 @@ const platformaticRuntimeSchema = {
94
108
  additionalProperties: true
95
109
  },
96
110
  interceptors: {
97
- anyOf: [{
98
- type: 'array',
99
- items: {
100
- $ref: '#/$defs/undiciInterceptor'
101
- }
102
- }, {
103
- type: 'object',
104
- properties: {
105
- Client: {
106
- type: 'array',
107
- items: {
108
- $ref: '#/$defs/undiciInterceptor'
109
- }
110
- },
111
- Pool: {
112
- type: 'array',
113
- items: {
114
- $ref: '#/$defs/undiciInterceptor'
115
- }
116
- },
117
- Agent: {
118
- type: 'array',
119
- items: {
120
- $ref: '#/$defs/undiciInterceptor'
111
+ anyOf: [
112
+ {
113
+ type: 'array',
114
+ items: {
115
+ $ref: '#/$defs/undiciInterceptor'
116
+ }
117
+ },
118
+ {
119
+ type: 'object',
120
+ properties: {
121
+ Client: {
122
+ type: 'array',
123
+ items: {
124
+ $ref: '#/$defs/undiciInterceptor'
125
+ }
126
+ },
127
+ Pool: {
128
+ type: 'array',
129
+ items: {
130
+ $ref: '#/$defs/undiciInterceptor'
131
+ }
132
+ },
133
+ Agent: {
134
+ type: 'array',
135
+ items: {
136
+ $ref: '#/$defs/undiciInterceptor'
137
+ }
121
138
  }
122
139
  }
123
140
  }
124
- }]
141
+ ]
125
142
  }
126
143
  }
127
144
  },
145
+ watch: {
146
+ anyOf: [
147
+ {
148
+ type: 'boolean'
149
+ },
150
+ {
151
+ type: 'string'
152
+ }
153
+ ]
154
+ },
128
155
  managementApi: {
129
156
  anyOf: [
130
157
  { type: 'boolean' },
@@ -133,11 +160,15 @@ const platformaticRuntimeSchema = {
133
160
  type: 'object',
134
161
  properties: {
135
162
  logs: {
136
- maxSize: {
137
- type: 'number',
138
- minimum: 5,
139
- default: 200
140
- }
163
+ type: 'object',
164
+ properties: {
165
+ maxSize: {
166
+ type: 'number',
167
+ minimum: 5,
168
+ default: 200
169
+ }
170
+ },
171
+ additionalProperties: false
141
172
  }
142
173
  },
143
174
  additionalProperties: false
@@ -152,10 +183,7 @@ const platformaticRuntimeSchema = {
152
183
  type: 'object',
153
184
  properties: {
154
185
  port: {
155
- anyOf: [
156
- { type: 'integer' },
157
- { type: 'string' }
158
- ]
186
+ anyOf: [{ type: 'integer' }, { type: 'string' }]
159
187
  },
160
188
  hostname: { type: 'string' },
161
189
  endpoint: { type: 'string' },
@@ -177,39 +205,29 @@ const platformaticRuntimeSchema = {
177
205
  }
178
206
  ]
179
207
  },
180
- restartOnError: {
181
- default: true,
182
- anyOf: [
183
- { type: 'boolean' },
184
- { type: 'string' }
185
- ]
186
- },
187
- services: {
188
- type: 'array',
189
- items: {
190
- type: 'object',
191
- required: ['id', 'path', 'config'],
192
- properties: {
193
- id: {
194
- type: 'string'
195
- },
196
- path: {
197
- type: 'string',
198
- resolvePath: true
199
- },
200
- config: {
201
- type: 'string'
202
- },
203
- useHttp: {
204
- type: 'boolean'
205
- }
208
+ telemetry,
209
+ inspectorOptions: {
210
+ type: 'object',
211
+ properties: {
212
+ host: {
213
+ type: 'string'
214
+ },
215
+ port: {
216
+ type: 'number'
217
+ },
218
+ breakFirstLine: {
219
+ type: 'boolean'
220
+ },
221
+ watchDisabled: {
222
+ type: 'boolean'
206
223
  }
207
224
  }
208
225
  }
209
226
  },
210
227
  anyOf: [
211
228
  { required: ['autoload', 'entrypoint'] },
212
- { required: ['services', 'entrypoint'] }
229
+ { required: ['services', 'entrypoint'] },
230
+ { required: ['web', 'entrypoint'] }
213
231
  ],
214
232
  additionalProperties: false,
215
233
  $defs: {
package/lib/start.js CHANGED
@@ -1,46 +1,25 @@
1
1
  'use strict'
2
2
 
3
- const { once } = require('node:events')
4
3
  const inspector = require('node:inspector')
5
- const { join, resolve, dirname } = require('node:path')
6
4
  const { writeFile } = require('node:fs/promises')
7
- const { pathToFileURL } = require('node:url')
8
- const { Worker } = require('node:worker_threads')
5
+ const { join, resolve, dirname } = require('node:path')
6
+
9
7
  const { printConfigValidationErrors } = require('@platformatic/config')
8
+ const {
9
+ errors: { ensureLoggableError }
10
+ } = require('@platformatic/utils')
10
11
  const closeWithGrace = require('close-with-grace')
11
- const { loadConfig } = require('./load-config')
12
- const { startManagementApi } = require('./management-api')
13
- const { startPrometheusServer } = require('./prom-server.js')
14
- const { parseInspectorOptions, wrapConfigInRuntimeConfig } = require('./config')
15
- const { RuntimeApiClient, getRuntimeLogsDir } = require('./api-client.js')
16
- const errors = require('./errors')
17
- const pkg = require('../package.json')
18
12
  const pino = require('pino')
19
13
  const pretty = require('pino-pretty')
20
14
 
21
- const kLoaderFile = pathToFileURL(join(__dirname, 'loader.mjs')).href
22
- const kWorkerFile = join(__dirname, 'worker.js')
23
- const kWorkerExecArgv = [
24
- '--no-warnings',
25
- '--experimental-loader',
26
- kLoaderFile
27
- ]
28
-
29
- function startWorker ({ config, dirname, runtimeLogsDir }, env) {
30
- const worker = new Worker(kWorkerFile, {
31
- /* c8 ignore next */
32
- execArgv: config.hotReload ? kWorkerExecArgv : [],
33
- transferList: config.loggingPort ? [config.loggingPort] : [],
34
- workerData: { config, dirname, runtimeLogsDir },
35
- env
36
- })
37
-
38
- return worker
39
- }
15
+ const pkg = require('../package.json')
16
+ const { parseInspectorOptions, wrapConfigInRuntimeConfig } = require('./config')
17
+ const { Runtime } = require('./runtime')
18
+ const errors = require('./errors')
19
+ const { getRuntimeLogsDir, loadConfig } = require('./utils')
40
20
 
41
21
  async function buildRuntime (configManager, env) {
42
22
  env = env || process.env
43
- const config = configManager.current
44
23
 
45
24
  if (inspector.url()) {
46
25
  throw new errors.NodeInspectorFlagsNotSupportedError()
@@ -50,80 +29,30 @@ async function buildRuntime (configManager, env) {
50
29
  parseInspectorOptions(configManager)
51
30
  }
52
31
 
53
- if (config.hotReload) {
54
- config.loaderFile = kLoaderFile
55
- }
56
-
57
32
  const dirname = configManager.dirname
58
33
  const runtimeLogsDir = getRuntimeLogsDir(dirname, process.pid)
59
34
 
60
- // The configManager cannot be transferred to the worker, so remove it.
61
- delete config.configManager
62
-
63
- let worker = startWorker({ config, dirname, runtimeLogsDir }, env)
64
-
65
- let managementApi = null
66
-
67
- if (config.hotReload) {
68
- /* c8 ignore next 3 */
69
- process.on('SIGUSR2', () => {
70
- worker.postMessage({ signal: 'SIGUSR2' })
71
- })
72
-
73
- /* c8 ignore next 3 */
74
- configManager.on('update', () => {
75
- // TODO(cjihrig): Need to clean up and restart the worker.
76
- })
77
- }
78
-
79
- function setupExit () {
80
- worker.on('exit', (code) => {
81
- // runtimeApiClient.started can be false if a stop command was issued
82
- // via the management API.
83
- if (config.restartOnError === false || !runtimeApiClient.started) {
84
- // We must stop those here in case the `closeWithGrace` callback
85
- // was not called.
86
- configManager.fileWatcher?.stopWatching()
87
- managementApi?.close()
88
- return
89
- }
90
-
91
- worker = startWorker({ config, dirname, runtimeLogsDir }, env)
92
- setupExit()
35
+ const runtime = new Runtime(configManager, runtimeLogsDir, env)
93
36
 
94
- once(worker, 'message').then((msg) => {
95
- runtimeApiClient.setWorker(worker).catch(() => {
96
- // TODO: currently we restart if the worker fails to start intermitently
97
- // should we limit this to a number of retries?
98
- })
99
- })
100
- })
101
- }
37
+ /* c8 ignore next 3 */
38
+ process.on('SIGUSR2', async () => {
39
+ runtime.logger.info('Received SIGUSR2, restarting all services ...')
102
40
 
103
- setupExit()
104
-
105
- await once(worker, 'message') // plt:init
106
-
107
- const runtimeApiClient = new RuntimeApiClient(
108
- worker,
109
- configManager,
110
- runtimeLogsDir
111
- )
41
+ try {
42
+ await runtime.restart()
43
+ } catch (err) {
44
+ runtime.logger.error({ err: ensureLoggableError(err) }, 'Failed to restart services.')
45
+ }
46
+ })
112
47
 
113
- if (config.managementApi) {
114
- managementApi = await startManagementApi(runtimeApiClient, configManager)
115
- runtimeApiClient.managementApi = managementApi
116
- runtimeApiClient.on('start', () => {
117
- runtimeApiClient.startCollectingMetrics()
118
- })
119
- }
120
- if (config.metrics) {
121
- runtimeApiClient.on('start', async () => {
122
- await startPrometheusServer(runtimeApiClient, config.metrics)
123
- })
48
+ try {
49
+ await runtime.init()
50
+ } catch (e) {
51
+ await runtime.close()
52
+ throw e
124
53
  }
125
54
 
126
- return runtimeApiClient
55
+ return runtime
127
56
  }
128
57
 
129
58
  async function start (args) {
@@ -155,15 +84,15 @@ async function setupAndStartRuntime (config) {
155
84
  let runtime = await buildRuntime(runtimeConfig)
156
85
 
157
86
  let address = null
158
- let startErr = null
87
+ const startErr = null
159
88
  const originalPort = runtimeConfig.current.server?.port || 0
160
89
  while (address === null) {
161
90
  try {
162
91
  address = await runtime.start()
163
92
  } catch (err) {
164
- startErr = err
165
93
  if (err.code === 'EADDRINUSE') {
166
94
  await runtime.close()
95
+
167
96
  if (runtimeConfig.current?.server?.port > MAX_PORT) throw err
168
97
  runtimeConfig.current.server.port++
169
98
  runtime = await buildRuntime(runtimeConfig)
@@ -173,26 +102,36 @@ async function setupAndStartRuntime (config) {
173
102
  }
174
103
  }
175
104
  if (startErr?.code === 'PLT_RUNTIME_EADDR_IN_USE') {
176
- const logger = pino(pretty({
177
- translateTime: 'SYS:HH:MM:ss',
178
- ignore: 'hostname,pid'
179
- }))
105
+ const logger = pino(
106
+ pretty({
107
+ translateTime: 'SYS:HH:MM:ss',
108
+ ignore: 'hostname,pid'
109
+ })
110
+ )
180
111
  logger.warn(`Port: ${originalPort} is already in use!`)
181
112
  logger.warn(`Starting service on port: ${runtimeConfig.current.server.port}`)
182
113
  }
183
114
  return { address, runtime }
184
115
  }
185
116
 
186
- async function startCommand (args) {
117
+ async function startCommand (args, throwAllErrors = false) {
187
118
  try {
188
- const config = await loadConfig({}, args)
119
+ const config = await loadConfig(
120
+ {
121
+ alias: {
122
+ p: 'production'
123
+ },
124
+ boolean: ['p', 'production']
125
+ },
126
+ args
127
+ )
189
128
 
190
129
  const startResult = await setupAndStartRuntime(config)
191
130
 
192
131
  const runtime = startResult.runtime
193
132
  const res = startResult.address
194
133
 
195
- closeWithGrace(async (event) => {
134
+ closeWithGrace(async event => {
196
135
  if (event.err instanceof Error) {
197
136
  console.error(event.err)
198
137
  }
@@ -201,9 +140,13 @@ async function startCommand (args) {
201
140
 
202
141
  return res
203
142
  } catch (err) {
143
+ if (throwAllErrors) {
144
+ throw err
145
+ }
146
+
204
147
  if (err.code === 'PLT_CONFIG_NO_CONFIG_FILE_FOUND' && args.length === 1) {
205
148
  const config = {
206
- $schema: `https://platformatic.dev/schemas/v${pkg.version}/service`,
149
+ $schema: `https://schemas.platformatic.dev/@platformatic/service/${pkg.version}.json`,
207
150
  server: {
208
151
  hostname: '127.0.0.1',
209
152
  port: 3042,
@@ -229,7 +172,7 @@ async function startCommand (args) {
229
172
  console.error(`Missing config file!
230
173
  Be sure to have a config file with one of the following names:
231
174
 
232
- ${err.filenames.map((s) => ' * ' + s).join('\n')}
175
+ ${err.filenames.map(s => ' * ' + s).join('\n')}
233
176
 
234
177
  In alternative run "npm create platformatic@latest" to generate a basic plt service config.`)
235
178
  process.exit(1)
package/lib/upgrade.js CHANGED
@@ -1,7 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const { join } = require('path')
4
- const pkg = require('../package.json')
3
+ const { join } = require('node:path')
5
4
 
6
5
  module.exports = async function upgrade (config, version) {
7
6
  const { semgrator } = await import('semgrator')
@@ -10,7 +9,7 @@ module.exports = async function upgrade (config, version) {
10
9
  version,
11
10
  path: join(__dirname, 'versions'),
12
11
  input: config,
13
- logger: this.logger.child({ name: '@platformatic/runtime' })
12
+ logger: this.logger.child({ name: '@platformatic/runtime' }),
14
13
  })
15
14
 
16
15
  let result
@@ -19,7 +18,5 @@ module.exports = async function upgrade (config, version) {
19
18
  result = updated.result
20
19
  }
21
20
 
22
- result.$schema = `https://platformatic.dev/schemas/v${pkg.version}/runtime`
23
-
24
21
  return result
25
22
  }
package/lib/utils.js CHANGED
@@ -1,10 +1,74 @@
1
1
  'use strict'
2
+
3
+ const { createHash } = require('node:crypto')
4
+ const { tmpdir } = require('node:os')
5
+ const { join } = require('node:path')
6
+
7
+ const {
8
+ Store,
9
+ loadConfig: pltConfigLoadConfig,
10
+ loadEmptyConfig: pltConfigLoadEmptyConfig,
11
+ } = require('@platformatic/config')
12
+
13
+ const { platformaticRuntime } = require('./config')
14
+
2
15
  function getArrayDifference (a, b) {
3
16
  return a.filter(element => {
4
17
  return !b.includes(element)
5
18
  })
6
19
  }
7
20
 
21
+ function getServiceUrl (id) {
22
+ return `http://${id}.plt.local`
23
+ }
24
+
25
+ function getRuntimeTmpDir (runtimeDir) {
26
+ const platformaticTmpDir = join(tmpdir(), 'platformatic', 'applications')
27
+ const runtimeDirHash = createHash('md5').update(runtimeDir).digest('hex')
28
+ return join(platformaticTmpDir, runtimeDirHash)
29
+ }
30
+
31
+ function getRuntimeLogsDir (runtimeDir, runtimePID) {
32
+ const runtimeTmpDir = getRuntimeTmpDir(runtimeDir)
33
+ return join(runtimeTmpDir, runtimePID.toString(), 'logs')
34
+ }
35
+
36
+ async function loadConfig (minimistConfig, args, overrides, replaceEnv = true) {
37
+ const { default: platformaticBasic } = await import('@platformatic/basic')
38
+ const store = new Store()
39
+ store.add(platformaticRuntime)
40
+
41
+ const id = platformaticRuntime.schema.$id.replace('@platformatic/runtime', 'wattpm')
42
+ const schema = {
43
+ ...platformaticRuntime.schema,
44
+ $id: id,
45
+ }
46
+ const configManagerConfig = {
47
+ ...platformaticRuntime.configManagerConfig,
48
+ schema,
49
+ }
50
+ const wattpm = {
51
+ ...platformaticRuntime,
52
+ schema,
53
+ configManagerConfig,
54
+ }
55
+ store.add(wattpm)
56
+ store.add(platformaticBasic)
57
+
58
+ return pltConfigLoadConfig(minimistConfig, args, store, overrides, replaceEnv)
59
+ }
60
+
61
+ async function loadEmptyConfig (path, overrides, replaceEnv = true) {
62
+ const { default: platformaticBasic } = await import('@platformatic/basic')
63
+
64
+ return pltConfigLoadEmptyConfig(path, platformaticBasic, overrides, replaceEnv)
65
+ }
66
+
8
67
  module.exports = {
9
- getArrayDifference
68
+ getArrayDifference,
69
+ getRuntimeLogsDir,
70
+ getRuntimeTmpDir,
71
+ getServiceUrl,
72
+ loadConfig,
73
+ loadEmptyConfig,
10
74
  }
@@ -7,5 +7,5 @@ module.exports = {
7
7
  config.restartOnError = false
8
8
  }
9
9
  return config
10
- }
10
+ },
11
11
  }
@@ -7,5 +7,5 @@ module.exports = {
7
7
  delete config.watch
8
8
  }
9
9
  return config
10
- }
10
+ },
11
11
  }
@@ -0,0 +1,26 @@
1
+ 'use strict'
2
+
3
+ const pkg = require('../../package.json')
4
+
5
+ module.exports = {
6
+ version: '1.99.0', // This is to account alpha versions as well
7
+ up: function (config) {
8
+ if (config.restartOnError === undefined) {
9
+ config.restartOnError = true
10
+ }
11
+
12
+ if (config.hotReload) {
13
+ config.watch = config.hotReload
14
+ delete config.hotReload
15
+ }
16
+
17
+ config.$schema = `https://schemas.platformatic.dev/@platformatic/runtime/${pkg.version}.json`
18
+
19
+ if (config.server?.logger) {
20
+ config.logger = config.server.logger
21
+ delete config.server.logger
22
+ }
23
+
24
+ return config
25
+ }
26
+ }