@ti-engine/core 1.7.1 → 1.8.0
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/CHANGELOG.md +393 -377
- package/LICENSE.md +321 -321
- package/README.md +599 -597
- package/bin/localization/labels.json +122 -122
- package/bin/settings.json +41 -41
- package/bin/start-instance.js +164 -164
- package/components/auditing.js +191 -191
- package/components/connection-observer.js +72 -72
- package/components/definitions.types.js +248 -248
- package/components/exchange/default/default-message-exchange.js +136 -136
- package/components/exchange/default/default-message-receiver.js +101 -101
- package/components/exchange/default/default-message-sender.js +100 -100
- package/components/exchange/message-dispatcher.js +168 -168
- package/components/exchange/message-exchange.js +449 -449
- package/components/exchange/message-handler.js +235 -235
- package/components/exchange/message-memory-cache.js +190 -190
- package/components/exchange/message-observer.js +126 -126
- package/components/exchange/message-receiver.js +181 -181
- package/components/exchange/message-sender.js +143 -143
- package/components/exchange/message-tracer.js +212 -212
- package/components/service-caller.js +370 -370
- package/components/service-consumer.js +131 -131
- package/components/service-executor.js +278 -278
- package/components/service-instance.js +316 -316
- package/components/service-provider.js +251 -251
- package/integrations/redis-integration.js +591 -591
- package/package.json +89 -89
- package/utils/cache.js +772 -772
- package/utils/config.js +103 -103
- package/utils/exceptions.js +368 -368
- package/utils/localization.js +298 -298
- package/utils/logger.js +82 -82
- package/utils/tools.js +632 -632
|
@@ -1,123 +1,123 @@
|
|
|
1
|
-
{
|
|
2
|
-
"system": {
|
|
3
|
-
"exceptions": {
|
|
4
|
-
"0": {
|
|
5
|
-
"en": "Unidentified error encountered or unrecognized exception code provided."
|
|
6
|
-
},
|
|
7
|
-
"1000": {
|
|
8
|
-
"en": "Error thrown by internal JS source."
|
|
9
|
-
},
|
|
10
|
-
"1001": {
|
|
11
|
-
"en": "Attempt to construct an abstract class detected."
|
|
12
|
-
},
|
|
13
|
-
"1002": {
|
|
14
|
-
"en": "Attempt to call an abstract method detected."
|
|
15
|
-
},
|
|
16
|
-
"1003": {
|
|
17
|
-
"en": "Invalid or no service domain name provided at microservice startup."
|
|
18
|
-
},
|
|
19
|
-
"1004": {
|
|
20
|
-
"en": "The system cache required for proper engine operation is unavailable."
|
|
21
|
-
},
|
|
22
|
-
"1005": {
|
|
23
|
-
"en": "The provided service handler is not a proper function."
|
|
24
|
-
},
|
|
25
|
-
"1006": {
|
|
26
|
-
"en": "The requested feature is not supported by current configuration or version."
|
|
27
|
-
},
|
|
28
|
-
"1007": {
|
|
29
|
-
"en": "The provided argument is not of the expected type."
|
|
30
|
-
},
|
|
31
|
-
"1008": {
|
|
32
|
-
"en": "The invoked framework component is not initialized."
|
|
33
|
-
},
|
|
34
|
-
"1009": {
|
|
35
|
-
"en": "Attempt to override a protected or private method or property detected."
|
|
36
|
-
},
|
|
37
|
-
"1010": {
|
|
38
|
-
"en": "The requested functionality is not yet implemented."
|
|
39
|
-
},
|
|
40
|
-
"2000": {
|
|
41
|
-
"en": "Invalid authorization token provided."
|
|
42
|
-
},
|
|
43
|
-
"2001": {
|
|
44
|
-
"en": "Invalid or expired session encountered."
|
|
45
|
-
},
|
|
46
|
-
"2002": {
|
|
47
|
-
"en": "Attempt for unauthorized access detected."
|
|
48
|
-
},
|
|
49
|
-
"2003": {
|
|
50
|
-
"en": "The system detected tampering with the message received via message exchange."
|
|
51
|
-
},
|
|
52
|
-
"2004": {
|
|
53
|
-
"en": "The requested authentication method is not recognized or supported."
|
|
54
|
-
},
|
|
55
|
-
"3000": {
|
|
56
|
-
"en": "General error during cross-application communication."
|
|
57
|
-
},
|
|
58
|
-
"3001": {
|
|
59
|
-
"en": "The message sender instance is currently unavailable."
|
|
60
|
-
},
|
|
61
|
-
"3002": {
|
|
62
|
-
"en": "The execution of a service could not complete within the allowed timeout."
|
|
63
|
-
},
|
|
64
|
-
"3003": {
|
|
65
|
-
"en": "The specified service is not found in the service registry."
|
|
66
|
-
},
|
|
67
|
-
"3004": {
|
|
68
|
-
"en": "The specified service is not found in the service definition interface."
|
|
69
|
-
},
|
|
70
|
-
"3005": {
|
|
71
|
-
"en": "No handler found in the interface for the specified service or service version."
|
|
72
|
-
},
|
|
73
|
-
"3006": {
|
|
74
|
-
"en": "The message receiver instance is currently unavailable."
|
|
75
|
-
},
|
|
76
|
-
"3007": {
|
|
77
|
-
"en": "The message exchange is irrevocably broken and cannot be used any longer."
|
|
78
|
-
},
|
|
79
|
-
"3008": {
|
|
80
|
-
"en": "The execution of a service reported a failure. This might be a business logic error or a system error."
|
|
81
|
-
},
|
|
82
|
-
"3010": {
|
|
83
|
-
"en": "Connection retry attempts exceeded the configured limit."
|
|
84
|
-
},
|
|
85
|
-
"4000": {
|
|
86
|
-
"en": "The request method is not recognized or not supported."
|
|
87
|
-
},
|
|
88
|
-
"4001": {
|
|
89
|
-
"en": "The request URI is not recognized or not supported."
|
|
90
|
-
},
|
|
91
|
-
"4002": {
|
|
92
|
-
"en": "The request body is not recognized or not supported."
|
|
93
|
-
},
|
|
94
|
-
"4003": {
|
|
95
|
-
"en": "The request query is not recognized or not supported."
|
|
96
|
-
},
|
|
97
|
-
"4004": {
|
|
98
|
-
"en": "The request headers are not recognized or not supported."
|
|
99
|
-
},
|
|
100
|
-
"4005": {
|
|
101
|
-
"en": "The request parameters are not recognized or not supported."
|
|
102
|
-
},
|
|
103
|
-
"4006": {
|
|
104
|
-
"en": "The request format is not recognized or not supported."
|
|
105
|
-
},
|
|
106
|
-
"4007": {
|
|
107
|
-
"en": "The request content type is not recognized or not supported."
|
|
108
|
-
},
|
|
109
|
-
"4008": {
|
|
110
|
-
"en": "The request content length is not recognized or not supported."
|
|
111
|
-
},
|
|
112
|
-
"4009": {
|
|
113
|
-
"en": "The request content encoding is not recognized or not supported."
|
|
114
|
-
},
|
|
115
|
-
"5004": {
|
|
116
|
-
"en": "The requested resource cannot be found. See details for more information."
|
|
117
|
-
},
|
|
118
|
-
"5005": {
|
|
119
|
-
"en": "The application service encountered an error. See details for more information."
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"system": {
|
|
3
|
+
"exceptions": {
|
|
4
|
+
"0": {
|
|
5
|
+
"en": "Unidentified error encountered or unrecognized exception code provided."
|
|
6
|
+
},
|
|
7
|
+
"1000": {
|
|
8
|
+
"en": "Error thrown by internal JS source."
|
|
9
|
+
},
|
|
10
|
+
"1001": {
|
|
11
|
+
"en": "Attempt to construct an abstract class detected."
|
|
12
|
+
},
|
|
13
|
+
"1002": {
|
|
14
|
+
"en": "Attempt to call an abstract method detected."
|
|
15
|
+
},
|
|
16
|
+
"1003": {
|
|
17
|
+
"en": "Invalid or no service domain name provided at microservice startup."
|
|
18
|
+
},
|
|
19
|
+
"1004": {
|
|
20
|
+
"en": "The system cache required for proper engine operation is unavailable."
|
|
21
|
+
},
|
|
22
|
+
"1005": {
|
|
23
|
+
"en": "The provided service handler is not a proper function."
|
|
24
|
+
},
|
|
25
|
+
"1006": {
|
|
26
|
+
"en": "The requested feature is not supported by current configuration or version."
|
|
27
|
+
},
|
|
28
|
+
"1007": {
|
|
29
|
+
"en": "The provided argument is not of the expected type."
|
|
30
|
+
},
|
|
31
|
+
"1008": {
|
|
32
|
+
"en": "The invoked framework component is not initialized."
|
|
33
|
+
},
|
|
34
|
+
"1009": {
|
|
35
|
+
"en": "Attempt to override a protected or private method or property detected."
|
|
36
|
+
},
|
|
37
|
+
"1010": {
|
|
38
|
+
"en": "The requested functionality is not yet implemented."
|
|
39
|
+
},
|
|
40
|
+
"2000": {
|
|
41
|
+
"en": "Invalid authorization token provided."
|
|
42
|
+
},
|
|
43
|
+
"2001": {
|
|
44
|
+
"en": "Invalid or expired session encountered."
|
|
45
|
+
},
|
|
46
|
+
"2002": {
|
|
47
|
+
"en": "Attempt for unauthorized access detected."
|
|
48
|
+
},
|
|
49
|
+
"2003": {
|
|
50
|
+
"en": "The system detected tampering with the message received via message exchange."
|
|
51
|
+
},
|
|
52
|
+
"2004": {
|
|
53
|
+
"en": "The requested authentication method is not recognized or supported."
|
|
54
|
+
},
|
|
55
|
+
"3000": {
|
|
56
|
+
"en": "General error during cross-application communication."
|
|
57
|
+
},
|
|
58
|
+
"3001": {
|
|
59
|
+
"en": "The message sender instance is currently unavailable."
|
|
60
|
+
},
|
|
61
|
+
"3002": {
|
|
62
|
+
"en": "The execution of a service could not complete within the allowed timeout."
|
|
63
|
+
},
|
|
64
|
+
"3003": {
|
|
65
|
+
"en": "The specified service is not found in the service registry."
|
|
66
|
+
},
|
|
67
|
+
"3004": {
|
|
68
|
+
"en": "The specified service is not found in the service definition interface."
|
|
69
|
+
},
|
|
70
|
+
"3005": {
|
|
71
|
+
"en": "No handler found in the interface for the specified service or service version."
|
|
72
|
+
},
|
|
73
|
+
"3006": {
|
|
74
|
+
"en": "The message receiver instance is currently unavailable."
|
|
75
|
+
},
|
|
76
|
+
"3007": {
|
|
77
|
+
"en": "The message exchange is irrevocably broken and cannot be used any longer."
|
|
78
|
+
},
|
|
79
|
+
"3008": {
|
|
80
|
+
"en": "The execution of a service reported a failure. This might be a business logic error or a system error."
|
|
81
|
+
},
|
|
82
|
+
"3010": {
|
|
83
|
+
"en": "Connection retry attempts exceeded the configured limit."
|
|
84
|
+
},
|
|
85
|
+
"4000": {
|
|
86
|
+
"en": "The request method is not recognized or not supported."
|
|
87
|
+
},
|
|
88
|
+
"4001": {
|
|
89
|
+
"en": "The request URI is not recognized or not supported."
|
|
90
|
+
},
|
|
91
|
+
"4002": {
|
|
92
|
+
"en": "The request body is not recognized or not supported."
|
|
93
|
+
},
|
|
94
|
+
"4003": {
|
|
95
|
+
"en": "The request query is not recognized or not supported."
|
|
96
|
+
},
|
|
97
|
+
"4004": {
|
|
98
|
+
"en": "The request headers are not recognized or not supported."
|
|
99
|
+
},
|
|
100
|
+
"4005": {
|
|
101
|
+
"en": "The request parameters are not recognized or not supported."
|
|
102
|
+
},
|
|
103
|
+
"4006": {
|
|
104
|
+
"en": "The request format is not recognized or not supported."
|
|
105
|
+
},
|
|
106
|
+
"4007": {
|
|
107
|
+
"en": "The request content type is not recognized or not supported."
|
|
108
|
+
},
|
|
109
|
+
"4008": {
|
|
110
|
+
"en": "The request content length is not recognized or not supported."
|
|
111
|
+
},
|
|
112
|
+
"4009": {
|
|
113
|
+
"en": "The request content encoding is not recognized or not supported."
|
|
114
|
+
},
|
|
115
|
+
"5004": {
|
|
116
|
+
"en": "The requested resource cannot be found. See details for more information."
|
|
117
|
+
},
|
|
118
|
+
"5005": {
|
|
119
|
+
"en": "The application service encountered an error. See details for more information."
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
123
|
}
|
package/bin/settings.json
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
{
|
|
2
|
-
"auditing": {
|
|
3
|
-
"logConsoleEnabled": true,
|
|
4
|
-
"logDetails": true,
|
|
5
|
-
"logMinLevel": 0,
|
|
6
|
-
"logUsesJSON": false
|
|
7
|
-
},
|
|
8
|
-
"gcloudIntegration": {
|
|
9
|
-
"apiKey": "",
|
|
10
|
-
"loggingEnabled": false,
|
|
11
|
-
"logName": "ti-engine",
|
|
12
|
-
"projectID": ""
|
|
13
|
-
},
|
|
14
|
-
"localization": {
|
|
15
|
-
"labelsPath": [],
|
|
16
|
-
"language": "en"
|
|
17
|
-
},
|
|
18
|
-
"memoryCache": {
|
|
19
|
-
"authKey": null,
|
|
20
|
-
"redisDB": 0,
|
|
21
|
-
"redisHost": "127.0.0.1",
|
|
22
|
-
"redisPort": 6379,
|
|
23
|
-
"user": "default"
|
|
24
|
-
},
|
|
25
|
-
"messageExchange": {
|
|
26
|
-
"messageQueuePrefix": "ti:messages:",
|
|
27
|
-
"messageStore": "ti:messages:store",
|
|
28
|
-
"securityHashEnabled": true,
|
|
29
|
-
"securityHashKey": "",
|
|
30
|
-
"traceExpirationTime": 3600,
|
|
31
|
-
"traceLogEnabled": false,
|
|
32
|
-
"traceRepository": "ti:messages:trace"
|
|
33
|
-
},
|
|
34
|
-
"serviceConfig": {
|
|
35
|
-
"executionTimeout": 180000,
|
|
36
|
-
"healthCheckAddress": "ti:services:registry:health:",
|
|
37
|
-
"healthCheckInterval": "*/1 * * * * *",
|
|
38
|
-
"healthCheckTimeout": 3,
|
|
39
|
-
"serviceRegistryAddress": "ti:services:registry:catalog:"
|
|
40
|
-
},
|
|
41
|
-
"operationMode": "production"
|
|
1
|
+
{
|
|
2
|
+
"auditing": {
|
|
3
|
+
"logConsoleEnabled": true,
|
|
4
|
+
"logDetails": true,
|
|
5
|
+
"logMinLevel": 0,
|
|
6
|
+
"logUsesJSON": false
|
|
7
|
+
},
|
|
8
|
+
"gcloudIntegration": {
|
|
9
|
+
"apiKey": "",
|
|
10
|
+
"loggingEnabled": false,
|
|
11
|
+
"logName": "ti-engine",
|
|
12
|
+
"projectID": ""
|
|
13
|
+
},
|
|
14
|
+
"localization": {
|
|
15
|
+
"labelsPath": [],
|
|
16
|
+
"language": "en"
|
|
17
|
+
},
|
|
18
|
+
"memoryCache": {
|
|
19
|
+
"authKey": null,
|
|
20
|
+
"redisDB": 0,
|
|
21
|
+
"redisHost": "127.0.0.1",
|
|
22
|
+
"redisPort": 6379,
|
|
23
|
+
"user": "default"
|
|
24
|
+
},
|
|
25
|
+
"messageExchange": {
|
|
26
|
+
"messageQueuePrefix": "ti:messages:",
|
|
27
|
+
"messageStore": "ti:messages:store",
|
|
28
|
+
"securityHashEnabled": true,
|
|
29
|
+
"securityHashKey": "",
|
|
30
|
+
"traceExpirationTime": 3600,
|
|
31
|
+
"traceLogEnabled": false,
|
|
32
|
+
"traceRepository": "ti:messages:trace"
|
|
33
|
+
},
|
|
34
|
+
"serviceConfig": {
|
|
35
|
+
"executionTimeout": 180000,
|
|
36
|
+
"healthCheckAddress": "ti:services:registry:health:",
|
|
37
|
+
"healthCheckInterval": "*/1 * * * * *",
|
|
38
|
+
"healthCheckTimeout": 3,
|
|
39
|
+
"serviceRegistryAddress": "ti:services:registry:catalog:"
|
|
40
|
+
},
|
|
41
|
+
"operationMode": "production"
|
|
42
42
|
}
|
package/bin/start-instance.js
CHANGED
|
@@ -1,165 +1,165 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* The ti-engine is an open source, free to use—both for personal and commercial projects—framework for the creation of microservice-based solutions using node.js.
|
|
3
|
-
* Copyright © 2021-2025 Boris Kostadinov <kostadinov.boris@gmail.com>
|
|
4
|
-
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
5
|
-
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
6
|
-
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
"use strict";
|
|
10
|
-
|
|
11
|
-
const path = require( "path" );
|
|
12
|
-
|
|
13
|
-
// Load any ENV variables defined in a .env file - before including any framework files:
|
|
14
|
-
// - If an env file path is provided via process arguments, use it; otherwise, default to CWD/.env
|
|
15
|
-
const envFilePath = ( () => {
|
|
16
|
-
let envPath = path.join( process.cwd(), ".env" );
|
|
17
|
-
const argv = Array.isArray( process.argv ) ? process.argv.slice( 2 ) : [];
|
|
18
|
-
const aliases = [ "--env", "--env-file", "--dotenv", "--dotenv-path", "-e" ];
|
|
19
|
-
for ( const key of aliases ) {
|
|
20
|
-
const idx = argv.indexOf( key );
|
|
21
|
-
if ( idx !== -1 && idx + 1 < argv.length ) {
|
|
22
|
-
const value = argv[ idx + 1 ];
|
|
23
|
-
if ( typeof value === "string" && !value.startsWith( "-" ) ) {
|
|
24
|
-
envPath = path.join( process.cwd(), value.trim() );
|
|
25
|
-
break;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return envPath;
|
|
30
|
-
} )();
|
|
31
|
-
|
|
32
|
-
// Load the resolved .env file using the native Node loader (Node >= 20.12). A missing file is not fatal —
|
|
33
|
-
// environment variables may be supplied entirely by the OS/container. Existing process.env values are NOT overridden.
|
|
34
|
-
try {
|
|
35
|
-
process.loadEnvFile( envFilePath );
|
|
36
|
-
} catch ( error ) {
|
|
37
|
-
if ( error.code !== "ENOENT" ) {
|
|
38
|
-
throw error;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const tools = require( "#tools" );
|
|
43
|
-
const logger = require( "#logger" );
|
|
44
|
-
|
|
45
|
-
// Configure the current instance variables before requiring any platform modules and store the necessary ones in memory cache:
|
|
46
|
-
process.env.TI_INSTANCE_ID = "ti-" + tools.getUUID();
|
|
47
|
-
process.env.TI_INSTANCE_CLASS = process.env.TI_INSTANCE_CLASS || "";
|
|
48
|
-
|
|
49
|
-
// Derive a safe default service domain name if not explicitly provided:
|
|
50
|
-
// - Normalize path separators
|
|
51
|
-
// - Strip directory and file extension
|
|
52
|
-
const defaultNameFromClass = ( () => {
|
|
53
|
-
try {
|
|
54
|
-
const normalized = ( process.env.TI_INSTANCE_CLASS || "" ).replace( /\\/g, "/" );
|
|
55
|
-
const base = path.posix.basename( normalized );
|
|
56
|
-
const name = path.parse( base ).name;
|
|
57
|
-
return name || "";
|
|
58
|
-
} catch {
|
|
59
|
-
return "";
|
|
60
|
-
}
|
|
61
|
-
} )();
|
|
62
|
-
|
|
63
|
-
process.env.TI_INSTANCE_NAME = process.env.TI_INSTANCE_NAME || defaultNameFromClass;
|
|
64
|
-
|
|
65
|
-
// Configure the process termination handlers to ensure a graceful shutdown:
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Will be used to gracefully shut down the instance.
|
|
69
|
-
* <br/>
|
|
70
|
-
* NOTE: Will be overridden below after the instance creation.
|
|
71
|
-
*
|
|
72
|
-
* @method
|
|
73
|
-
* @param {number} exitCode
|
|
74
|
-
* @abstract
|
|
75
|
-
* @private
|
|
76
|
-
*/
|
|
77
|
-
let shutDownInstance = ( exitCode ) => {
|
|
78
|
-
process.exit( exitCode );
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
// This event will handle the process termination (Ctrl + C):
|
|
82
|
-
process.on( "SIGINT", () => {
|
|
83
|
-
logger.log( `SIGINT event detected in main instance process.`, logger.logSeverity.NOTICE );
|
|
84
|
-
shutDownInstance( 0 );
|
|
85
|
-
} );
|
|
86
|
-
|
|
87
|
-
// This event will handle the process termination via terminal hangup (CMD close) - not delivered on all platforms:
|
|
88
|
-
process.on( "SIGHUP", () => {
|
|
89
|
-
logger.log( `SIGHUP event detected in main instance process.`, logger.logSeverity.NOTICE );
|
|
90
|
-
shutDownInstance( 0 );
|
|
91
|
-
} );
|
|
92
|
-
|
|
93
|
-
// This event will handle the process termination:
|
|
94
|
-
process.on( "SIGTERM", () => {
|
|
95
|
-
logger.log( `SIGTERM event detected in main instance process.`, logger.logSeverity.NOTICE );
|
|
96
|
-
shutDownInstance( 0 );
|
|
97
|
-
} );
|
|
98
|
-
|
|
99
|
-
// Handle Windows console break (Ctrl + Break):
|
|
100
|
-
process.on( "SIGBREAK", () => {
|
|
101
|
-
logger.log( `SIGBREAK event detected in main instance process.`, logger.logSeverity.NOTICE );
|
|
102
|
-
shutDownInstance( 0 );
|
|
103
|
-
} );
|
|
104
|
-
|
|
105
|
-
// Configure the process general error handlers:
|
|
106
|
-
|
|
107
|
-
process.on( "unhandledRejection", ( reason ) => {
|
|
108
|
-
// Check if the fail-fast behavior has been forcefully disabled:
|
|
109
|
-
const failFastDisabled = tools.toBool( process.env.TI_FAIL_FAST_ON_UNHANDLED_OFF || "" );
|
|
110
|
-
logger.log( `Unhandled promise rejection identified! Make sure this isn't a software bug.`, logger.logSeverity.WARNING, {
|
|
111
|
-
reason: tools.errorToJSON && reason instanceof Error ? tools.errorToJSON( reason ) : reason
|
|
112
|
-
} );
|
|
113
|
-
if ( failFastDisabled !== true ) {
|
|
114
|
-
setImmediate( () => process.exit( 1 ) );
|
|
115
|
-
}
|
|
116
|
-
} );
|
|
117
|
-
|
|
118
|
-
process.on( "multipleResolves", ( type, promise, reason ) => {
|
|
119
|
-
logger.log( `Multiple promise resolves detected! Make sure this isn't a software bug.`, logger.logSeverity.WARNING, {
|
|
120
|
-
type,
|
|
121
|
-
reason: tools.errorToJSON && reason instanceof Error ? tools.errorToJSON( reason ) : reason
|
|
122
|
-
} );
|
|
123
|
-
} );
|
|
124
|
-
|
|
125
|
-
process.on( "uncaughtException", ( error ) => {
|
|
126
|
-
logger.log( `Some nasty and uncaught error just occurred in the application!`, logger.logSeverity.ALERT, error );
|
|
127
|
-
setImmediate( () => process.exit( 1 ) );
|
|
128
|
-
} );
|
|
129
|
-
|
|
130
|
-
// Start the instance:
|
|
131
|
-
try {
|
|
132
|
-
const serviceConstructor = require( path.join( process.cwd(), process.env.TI_INSTANCE_CLASS ) );
|
|
133
|
-
const serviceConfigPath = process.env.TI_INSTANCE_CONFIG;
|
|
134
|
-
/** @type ServiceConfiguration */
|
|
135
|
-
let serviceConfig;
|
|
136
|
-
if ( serviceConfigPath ) {
|
|
137
|
-
serviceConfig = require( path.join( process.cwd(), process.env.TI_INSTANCE_CONFIG ) );
|
|
138
|
-
}
|
|
139
|
-
/** @type ServiceInstance */
|
|
140
|
-
const mainInstance = new serviceConstructor( process.env.TI_INSTANCE_NAME, serviceConfig );
|
|
141
|
-
|
|
142
|
-
/** @override */
|
|
143
|
-
shutDownInstance = ( exitCode ) => {
|
|
144
|
-
mainInstance.stop().then( () => {
|
|
145
|
-
setImmediate( () => process.exit( exitCode ) );
|
|
146
|
-
} ).catch( ( error ) => {
|
|
147
|
-
logger.log( `Error occurred during shut down of instance '${ process.env.TI_INSTANCE_ID }'! Exit code changed from '${ exitCode }' to '1'.`, logger.logSeverity.ERROR, error );
|
|
148
|
-
setImmediate( () => process.exit( 1 ) );
|
|
149
|
-
} );
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
if ( mainInstance.isServiceInstance ) {
|
|
153
|
-
logger.log( `Starting new instance of type '${ process.env.TI_INSTANCE_NAME }' with instance ID '${ process.env.TI_INSTANCE_ID }'.`, logger.logSeverity.NOTICE );
|
|
154
|
-
mainInstance.start().catch( ( error ) => {
|
|
155
|
-
logger.log( `Error detected during instance '${ process.env.TI_INSTANCE_ID }' startup!`, logger.logSeverity.ALERT, error );
|
|
156
|
-
setImmediate( () => process.exit( 1 ) );
|
|
157
|
-
} );
|
|
158
|
-
} else {
|
|
159
|
-
logger.log( `Attempting to start a module that does not implement the ServiceInstance abstract class!`, logger.logSeverity.CRITICAL );
|
|
160
|
-
setImmediate( () => process.exit( 1 ) );
|
|
161
|
-
}
|
|
162
|
-
} catch ( error ) {
|
|
163
|
-
logger.log( `Error detected in the instance startup script!`, logger.logSeverity.ALERT, error );
|
|
164
|
-
setImmediate( () => process.exit( 1 ) );
|
|
1
|
+
/*
|
|
2
|
+
* The ti-engine is an open source, free to use—both for personal and commercial projects—framework for the creation of microservice-based solutions using node.js.
|
|
3
|
+
* Copyright © 2021-2025 Boris Kostadinov <kostadinov.boris@gmail.com>
|
|
4
|
+
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
5
|
+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
6
|
+
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
"use strict";
|
|
10
|
+
|
|
11
|
+
const path = require( "path" );
|
|
12
|
+
|
|
13
|
+
// Load any ENV variables defined in a .env file - before including any framework files:
|
|
14
|
+
// - If an env file path is provided via process arguments, use it; otherwise, default to CWD/.env
|
|
15
|
+
const envFilePath = ( () => {
|
|
16
|
+
let envPath = path.join( process.cwd(), ".env" );
|
|
17
|
+
const argv = Array.isArray( process.argv ) ? process.argv.slice( 2 ) : [];
|
|
18
|
+
const aliases = [ "--env", "--env-file", "--dotenv", "--dotenv-path", "-e" ];
|
|
19
|
+
for ( const key of aliases ) {
|
|
20
|
+
const idx = argv.indexOf( key );
|
|
21
|
+
if ( idx !== -1 && idx + 1 < argv.length ) {
|
|
22
|
+
const value = argv[ idx + 1 ];
|
|
23
|
+
if ( typeof value === "string" && !value.startsWith( "-" ) ) {
|
|
24
|
+
envPath = path.join( process.cwd(), value.trim() );
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return envPath;
|
|
30
|
+
} )();
|
|
31
|
+
|
|
32
|
+
// Load the resolved .env file using the native Node loader (Node >= 20.12). A missing file is not fatal —
|
|
33
|
+
// environment variables may be supplied entirely by the OS/container. Existing process.env values are NOT overridden.
|
|
34
|
+
try {
|
|
35
|
+
process.loadEnvFile( envFilePath );
|
|
36
|
+
} catch ( error ) {
|
|
37
|
+
if ( error.code !== "ENOENT" ) {
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const tools = require( "#tools" );
|
|
43
|
+
const logger = require( "#logger" );
|
|
44
|
+
|
|
45
|
+
// Configure the current instance variables before requiring any platform modules and store the necessary ones in memory cache:
|
|
46
|
+
process.env.TI_INSTANCE_ID = "ti-" + tools.getUUID();
|
|
47
|
+
process.env.TI_INSTANCE_CLASS = process.env.TI_INSTANCE_CLASS || "";
|
|
48
|
+
|
|
49
|
+
// Derive a safe default service domain name if not explicitly provided:
|
|
50
|
+
// - Normalize path separators
|
|
51
|
+
// - Strip directory and file extension
|
|
52
|
+
const defaultNameFromClass = ( () => {
|
|
53
|
+
try {
|
|
54
|
+
const normalized = ( process.env.TI_INSTANCE_CLASS || "" ).replace( /\\/g, "/" );
|
|
55
|
+
const base = path.posix.basename( normalized );
|
|
56
|
+
const name = path.parse( base ).name;
|
|
57
|
+
return name || "";
|
|
58
|
+
} catch {
|
|
59
|
+
return "";
|
|
60
|
+
}
|
|
61
|
+
} )();
|
|
62
|
+
|
|
63
|
+
process.env.TI_INSTANCE_NAME = process.env.TI_INSTANCE_NAME || defaultNameFromClass;
|
|
64
|
+
|
|
65
|
+
// Configure the process termination handlers to ensure a graceful shutdown:
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Will be used to gracefully shut down the instance.
|
|
69
|
+
* <br/>
|
|
70
|
+
* NOTE: Will be overridden below after the instance creation.
|
|
71
|
+
*
|
|
72
|
+
* @method
|
|
73
|
+
* @param {number} exitCode
|
|
74
|
+
* @abstract
|
|
75
|
+
* @private
|
|
76
|
+
*/
|
|
77
|
+
let shutDownInstance = ( exitCode ) => {
|
|
78
|
+
process.exit( exitCode );
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// This event will handle the process termination (Ctrl + C):
|
|
82
|
+
process.on( "SIGINT", () => {
|
|
83
|
+
logger.log( `SIGINT event detected in main instance process.`, logger.logSeverity.NOTICE );
|
|
84
|
+
shutDownInstance( 0 );
|
|
85
|
+
} );
|
|
86
|
+
|
|
87
|
+
// This event will handle the process termination via terminal hangup (CMD close) - not delivered on all platforms:
|
|
88
|
+
process.on( "SIGHUP", () => {
|
|
89
|
+
logger.log( `SIGHUP event detected in main instance process.`, logger.logSeverity.NOTICE );
|
|
90
|
+
shutDownInstance( 0 );
|
|
91
|
+
} );
|
|
92
|
+
|
|
93
|
+
// This event will handle the process termination:
|
|
94
|
+
process.on( "SIGTERM", () => {
|
|
95
|
+
logger.log( `SIGTERM event detected in main instance process.`, logger.logSeverity.NOTICE );
|
|
96
|
+
shutDownInstance( 0 );
|
|
97
|
+
} );
|
|
98
|
+
|
|
99
|
+
// Handle Windows console break (Ctrl + Break):
|
|
100
|
+
process.on( "SIGBREAK", () => {
|
|
101
|
+
logger.log( `SIGBREAK event detected in main instance process.`, logger.logSeverity.NOTICE );
|
|
102
|
+
shutDownInstance( 0 );
|
|
103
|
+
} );
|
|
104
|
+
|
|
105
|
+
// Configure the process general error handlers:
|
|
106
|
+
|
|
107
|
+
process.on( "unhandledRejection", ( reason ) => {
|
|
108
|
+
// Check if the fail-fast behavior has been forcefully disabled:
|
|
109
|
+
const failFastDisabled = tools.toBool( process.env.TI_FAIL_FAST_ON_UNHANDLED_OFF || "" );
|
|
110
|
+
logger.log( `Unhandled promise rejection identified! Make sure this isn't a software bug.`, logger.logSeverity.WARNING, {
|
|
111
|
+
reason: tools.errorToJSON && reason instanceof Error ? tools.errorToJSON( reason ) : reason
|
|
112
|
+
} );
|
|
113
|
+
if ( failFastDisabled !== true ) {
|
|
114
|
+
setImmediate( () => process.exit( 1 ) );
|
|
115
|
+
}
|
|
116
|
+
} );
|
|
117
|
+
|
|
118
|
+
process.on( "multipleResolves", ( type, promise, reason ) => {
|
|
119
|
+
logger.log( `Multiple promise resolves detected! Make sure this isn't a software bug.`, logger.logSeverity.WARNING, {
|
|
120
|
+
type,
|
|
121
|
+
reason: tools.errorToJSON && reason instanceof Error ? tools.errorToJSON( reason ) : reason
|
|
122
|
+
} );
|
|
123
|
+
} );
|
|
124
|
+
|
|
125
|
+
process.on( "uncaughtException", ( error ) => {
|
|
126
|
+
logger.log( `Some nasty and uncaught error just occurred in the application!`, logger.logSeverity.ALERT, error );
|
|
127
|
+
setImmediate( () => process.exit( 1 ) );
|
|
128
|
+
} );
|
|
129
|
+
|
|
130
|
+
// Start the instance:
|
|
131
|
+
try {
|
|
132
|
+
const serviceConstructor = require( path.join( process.cwd(), process.env.TI_INSTANCE_CLASS ) );
|
|
133
|
+
const serviceConfigPath = process.env.TI_INSTANCE_CONFIG;
|
|
134
|
+
/** @type ServiceConfiguration */
|
|
135
|
+
let serviceConfig;
|
|
136
|
+
if ( serviceConfigPath ) {
|
|
137
|
+
serviceConfig = require( path.join( process.cwd(), process.env.TI_INSTANCE_CONFIG ) );
|
|
138
|
+
}
|
|
139
|
+
/** @type ServiceInstance */
|
|
140
|
+
const mainInstance = new serviceConstructor( process.env.TI_INSTANCE_NAME, serviceConfig );
|
|
141
|
+
|
|
142
|
+
/** @override */
|
|
143
|
+
shutDownInstance = ( exitCode ) => {
|
|
144
|
+
mainInstance.stop().then( () => {
|
|
145
|
+
setImmediate( () => process.exit( exitCode ) );
|
|
146
|
+
} ).catch( ( error ) => {
|
|
147
|
+
logger.log( `Error occurred during shut down of instance '${ process.env.TI_INSTANCE_ID }'! Exit code changed from '${ exitCode }' to '1'.`, logger.logSeverity.ERROR, error );
|
|
148
|
+
setImmediate( () => process.exit( 1 ) );
|
|
149
|
+
} );
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
if ( mainInstance.isServiceInstance ) {
|
|
153
|
+
logger.log( `Starting new instance of type '${ process.env.TI_INSTANCE_NAME }' with instance ID '${ process.env.TI_INSTANCE_ID }'.`, logger.logSeverity.NOTICE );
|
|
154
|
+
mainInstance.start().catch( ( error ) => {
|
|
155
|
+
logger.log( `Error detected during instance '${ process.env.TI_INSTANCE_ID }' startup!`, logger.logSeverity.ALERT, error );
|
|
156
|
+
setImmediate( () => process.exit( 1 ) );
|
|
157
|
+
} );
|
|
158
|
+
} else {
|
|
159
|
+
logger.log( `Attempting to start a module that does not implement the ServiceInstance abstract class!`, logger.logSeverity.CRITICAL );
|
|
160
|
+
setImmediate( () => process.exit( 1 ) );
|
|
161
|
+
}
|
|
162
|
+
} catch ( error ) {
|
|
163
|
+
logger.log( `Error detected in the instance startup script!`, logger.logSeverity.ALERT, error );
|
|
164
|
+
setImmediate( () => process.exit( 1 ) );
|
|
165
165
|
}
|