@@ -5,69 +5,86 @@ const { getSecret, refreshToken } = require('../util');
5
5
const { SALESFORCE_API_VERSION } = require ( '../common.js' ) . globalConsts ;
6
6
7
7
let fayeClient ;
8
+ let context ;
9
+ let status ;
8
10
/**
9
11
* This method will be called from elastic.io platform providing following data
10
12
*
11
13
* @param msg incoming message object that contains ``body`` with payload
12
14
* @param configuration configuration that is account information and configuration field values
13
15
*/
14
16
async function processTrigger ( msg , configuration ) {
15
- this . logger . info ( 'Starting Subscribe to platform events Trigger' ) ;
17
+ context = this ;
18
+ if ( status === 'online' ) {
19
+ this . logger . info ( 'Subscription is still running, waiting for new messages' ) ;
20
+ return ;
21
+ }
22
+ context . logger . info ( 'Starting Subscribe to platform events Trigger' ) ;
23
+
16
24
const { secretId } = configuration ;
17
25
if ( ! secretId ) {
18
- this . logger . error ( 'secretId is missing in configuration, credentials cannot be fetched' ) ;
26
+ context . logger . error ( 'secretId is missing in configuration, credentials cannot be fetched' ) ;
19
27
throw new Error ( 'secretId is missing in configuration, credentials cannot be fetched' ) ;
20
28
}
21
- this . logger . debug ( 'Fetching credentials by secretId' ) ;
29
+ context . logger . debug ( 'Fetching credentials by secretId' ) ;
22
30
const { credentials } = await getSecret ( this , secretId , msg . id ) ;
23
31
const accessToken = credentials . access_token ;
24
32
const instanceUrl = credentials . undefined_params . instance_url ;
25
- this . logger . debug ( 'Preparing SalesForce connection...' ) ;
33
+ context . logger . debug ( 'Preparing SalesForce connection...' ) ;
26
34
const connection = new jsforce . Connection ( {
27
35
instanceUrl,
28
36
accessToken,
29
37
version : SALESFORCE_API_VERSION ,
30
38
} ) ;
31
39
const topic = `/event/${ configuration . object } ` ;
32
40
const replayId = - 1 ;
33
- this . logger . debug ( 'Creating streaming client' ) ;
34
- if ( ! fayeClient ) {
41
+ context . logger . debug ( 'Creating streaming client' ) ;
42
+ if ( ! fayeClient || status === 'down' ) {
35
43
fayeClient = connection . streaming . createClient ( [
36
44
new jsforce . StreamingExtension . Replay ( topic , replayId ) ,
37
45
new jsforce . StreamingExtension . AuthFailure ( async ( err ) => {
38
- this . logger . trace ( 'AuthFailure error occurred' ) ;
46
+ context . logger . trace ( 'AuthFailure error occurred' ) ;
39
47
if ( err . ext && err . ext . sfdc && err . ext . sfdc . failureReason && ( err . ext . sfdc . failureReason === '401::Authentication invalid' ) ) {
40
48
try {
41
- this . logger . debug ( 'Session is expired, trying to refresh token' ) ;
42
- await refreshToken ( this , secretId , msg . id ) ;
43
- this . logger . debug ( 'Token is successfully refreshed' ) ;
49
+ context . logger . debug ( 'Session is expired, trying to refresh token' ) ;
50
+ await refreshToken ( context , secretId , msg . id ) ;
51
+ context . logger . debug ( 'Token is successfully refreshed' ) ;
44
52
} catch ( error ) {
45
- this . logger . error ( 'Failed to fetch refresh token' ) ;
53
+ context . logger . error ( 'Failed to fetch refresh token' ) ;
46
54
throw new Error ( 'Failed to fetch refresh token' ) ;
47
55
}
48
56
fayeClient = undefined ;
49
- this . logger . info ( 'Lets call processTrigger one more time' ) ;
50
- await processTrigger . call ( this , msg , configuration ) ;
57
+ context . logger . info ( 'Lets call processTrigger one more time' ) ;
58
+ await processTrigger . call ( context , msg , configuration ) ;
51
59
} else {
52
- this . logger . error ( 'AuthFailure extension error occurred' ) ;
60
+ context . logger . error ( 'AuthFailure extension error occurred' ) ;
53
61
throw err ;
54
62
}
55
63
} ) ,
56
64
] ) ;
57
65
58
- fayeClient . subscribe ( topic , async ( message ) => {
59
- this . logger . info ( 'Incoming message found, going to emit...' ) ;
60
- await this . emit ( 'data' , messages . newMessageWithBody ( message ) ) ;
61
- } )
62
- . then ( ( ) => {
63
- this . logger . info ( 'Subscribed to PushTopic successfully' ) ;
64
- this . logger . trace ( `Subscribed to PushTopic: ${ topic } ` ) ;
65
- } ,
66
- ( err ) => {
67
- this . logger . error ( 'Subscriber error occurred' ) ;
68
- throw err ;
69
- } ) ;
70
- this . logger . info ( 'Streaming client created and ready' ) ;
66
+ fayeClient . on ( 'transport:down' , ( ) => {
67
+ context . logger . error ( 'Client is offline' ) ;
68
+ context . emit ( 'error' , 'Client is offline' ) ;
69
+ status = 'down' ;
70
+ context . logger . info ( 'Lets call processTrigger one more time' ) ;
71
+ processTrigger . call ( context , msg , configuration ) ;
72
+ } ) ;
73
+
74
+ fayeClient . on ( 'transport:up' , ( ) => {
75
+ context . logger . info ( 'Client is online' ) ;
76
+ } ) ;
77
+
78
+ await fayeClient . subscribe ( topic , async ( message ) => {
79
+ context . logger . info ( 'Incoming message found, going to emit...' ) ;
80
+ await context . emit ( 'data' , messages . newMessageWithBody ( message ) ) ;
81
+ } ) ;
82
+ status = 'online' ;
83
+
84
+ context . logger . info ( 'Subscribed to PushTopic successfully' ) ;
85
+ context . logger . trace ( `Subscribed to PushTopic: ${ topic } ` ) ;
86
+
87
+ context . logger . info ( 'Streaming client created and ready' ) ;
71
88
}
72
89
}
73
90
0 commit comments