File tree Expand file tree Collapse file tree 5 files changed +53
-28
lines changed Expand file tree Collapse file tree 5 files changed +53
-28
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { createTerminus } from '@godaddy/terminus';
3
3
import app from './app' ;
4
4
import config from './config' ;
5
5
import logger from './logger' ;
6
+ import services from './services' ;
6
7
7
8
const server = http . createServer ( app ) ;
8
9
@@ -28,6 +29,14 @@ createTerminus(server, {
28
29
29
30
async function start ( ) : Promise < void > {
30
31
try {
32
+ const namespace = config . services . trace . daemonAddressNamespace ;
33
+ const name = config . services . trace . daemonAddressName ;
34
+ if ( typeof namespace === 'string' && typeof name === 'string' ) {
35
+ const address = await services . ServiceDiscovery . discoverInstance ( namespace , name ) ;
36
+
37
+ ( services . Trace as any ) . setDaemonAddress ( address ) ;
38
+ }
39
+
31
40
server . listen ( config . port , ( ) => {
32
41
logger . info ( `Server started at http://localhost:${ config . port } ` ) ;
33
42
} ) ;
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import metadata from './metadata';
8
8
import job from './job' ;
9
9
import cache from './cache' ;
10
10
import trace from './trace' ;
11
+ import ServiceDiscovery from './serviceDiscovery' ;
11
12
import config from '../config' ;
12
13
13
14
const container = {
@@ -62,6 +63,10 @@ const container = {
62
63
default :
63
64
return trace . CLSHooked ;
64
65
}
66
+ } ,
67
+
68
+ get ServiceDiscovery ( ) {
69
+ return new ServiceDiscovery ( ) ;
65
70
}
66
71
} ;
67
72
Original file line number Diff line number Diff line change
1
+ import { ServiceDiscoveryClient , DiscoverInstancesCommand , HealthStatusFilter } from '@aws-sdk/client-servicediscovery' ;
2
+
3
+ export class ServiceDiscovery {
4
+ public async discoverInstance ( namespace : string , name : string ) : Promise < string > {
5
+ const client = new ServiceDiscoveryClient ( { region : process . env . AWS_XRAY_REGION } ) ;
6
+
7
+ const input = {
8
+ NamespaceName : namespace ,
9
+ ServiceName : name ,
10
+ MaxResults : 1 ,
11
+ HealthStatus : HealthStatusFilter . HEALTHY_OR_ELSE_ALL ,
12
+ } ;
13
+
14
+ const command = new DiscoverInstancesCommand ( input ) ;
15
+
16
+ const result = await client . send ( command ) ;
17
+
18
+ const ip = result . Instances [ 0 ] . Attributes . AWS_INSTANCE_IPV4 ;
19
+ const port = result . Instances [ 0 ] . Attributes . AWS_INSTANCE_PORT ;
20
+
21
+ return `${ ip } :${ port } ` ;
22
+ }
23
+ }
24
+
25
+ export default ServiceDiscovery ;
Original file line number Diff line number Diff line change 1
- import { ServiceDiscoveryClient , DiscoverInstancesCommand , HealthStatusFilter } from '@aws-sdk/client-servicediscovery' ;
2
1
import AWSXRay from './AWSXRay' ;
3
2
import CLSHooked from './CLSHooked' ;
4
3
import config from '../../config' ;
5
- import { nextTick } from 'process' ;
6
4
7
5
const container = {
8
6
get AWSXRay ( ) {
9
7
if ( typeof this . _awsXRay === 'undefined' ) {
10
8
this . _awsXRay = new AWSXRay ( config . services . trace . plugins ) ;
11
-
12
- if ( typeof config . services . trace . daemonAddressNamespace === 'string' ) {
13
- const setDaemonAddress = async ( ) => {
14
- const client = new ServiceDiscoveryClient ( { region : process . env . AWS_XRAY_REGION } ) ;
15
-
16
- const input = {
17
- NamespaceName : config . services . trace . daemonAddressNamespace ,
18
- ServiceName : config . services . trace . daemonAddressName ,
19
- MaxResults : 1 ,
20
- HealthStatus : HealthStatusFilter . HEALTHY_OR_ELSE_ALL ,
21
- } ;
22
-
23
- const command = new DiscoverInstancesCommand ( input ) ;
24
-
25
- const result = await client . send ( command ) ;
26
-
27
- const ip = result . Instances [ 0 ] . Attributes . AWS_INSTANCE_IPV4 ;
28
- const port = result . Instances [ 0 ] . Attributes . AWS_INSTANCE_PORT ;
29
-
30
- this . _awsXRay . setDaemonAddress ( `${ ip } :${ port } ` ) ;
31
- } ;
32
-
33
- nextTick ( setDaemonAddress ) ;
34
- }
35
9
}
36
10
37
11
return this . _awsXRay ;
Original file line number Diff line number Diff line change @@ -69,6 +69,18 @@ app.on('timeout_error', (err) => {
69
69
logger . error ( 'Timeout error!' , { message : err . message , stack : err . stack } ) ;
70
70
} ) ;
71
71
72
- logger . info ( 'Upload worker is running' ) ;
72
+ async function start ( ) {
73
+ const namespace = config . services . trace . daemonAddressNamespace ;
74
+ const name = config . services . trace . daemonAddressName ;
75
+ if ( typeof namespace === 'string' && typeof name === 'string' ) {
76
+ const address = await services . ServiceDiscovery . discoverInstance ( namespace , name ) ;
73
77
74
- app . start ( ) ;
78
+ ( services . Trace as any ) . setDaemonAddress ( address ) ;
79
+ }
80
+
81
+ logger . info ( 'Upload worker is running' ) ;
82
+
83
+ app . start ( ) ;
84
+ }
85
+
86
+ start ( ) ;
You can’t perform that action at this time.
0 commit comments