Skip to content

Commit d5ef807

Browse files
authored
Fix a bug unable to receive Salesforce Platform Events (#71)
1 parent 176defa commit d5ef807

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.7.2 (September 28, 2023)
2+
* Improvements in `Subscribe to platform events` trigger:
3+
* fixed duplicates retries on connections lost
4+
* fixed incorrect behavior with AuthFailure
5+
16
## 2.7.1 (September 21, 2023)
27
* Improvements in `Subscribe to platform events` trigger:
38
* Added retry on connections lost

component.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Customer relationship management (CRM) software & cloud computing from the leader in CRM solutions for businesses large & small.",
44
"docsUrl": "https://github.com/elasticio/salesforce-component-v2",
55
"url": "http://www.salesforce.com/",
6-
"version": "2.7.1",
6+
"version": "2.7.2",
77
"authClientTypes": [
88
"oauth2"
99
],

lib/triggers/streamPlatformEvents.js

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-empty */
12
const jsforce = require('jsforce');
23
const { messages } = require('elasticio-node');
34
const { callJSForceMethod } = require('../helpers/wrapper');
@@ -6,7 +7,8 @@ const { SALESFORCE_API_VERSION } = require('../common.js').globalConsts;
67

78
let fayeClient;
89
let context;
9-
let status;
10+
let refreshTokenNeeded = false;
11+
let creationInProgress = false;
1012
/**
1113
* This method will be called from elastic.io platform providing following data
1214
*
@@ -15,17 +17,28 @@ let status;
1517
*/
1618
async function processTrigger(msg, configuration) {
1719
context = this;
18-
if (status === 'online') {
20+
if (fayeClient) {
1921
this.logger.info('Subscription is still running, waiting for new messages');
2022
return;
2123
}
24+
if (creationInProgress) {
25+
this.logger.info('Subscription recreate in progress');
26+
return;
27+
}
28+
if (fayeClient) fayeClient = undefined;
29+
creationInProgress = true;
2230
context.logger.info('Starting Subscribe to platform events Trigger');
2331

2432
const { secretId } = configuration;
2533
if (!secretId) {
2634
context.logger.error('secretId is missing in configuration, credentials cannot be fetched');
35+
creationInProgress = false;
2736
throw new Error('secretId is missing in configuration, credentials cannot be fetched');
2837
}
38+
if (refreshTokenNeeded) {
39+
await refreshToken(context, secretId, msg.id);
40+
refreshTokenNeeded = false;
41+
}
2942
context.logger.debug('Fetching credentials by secretId');
3043
const { credentials } = await getSecret(this, secretId, msg.id);
3144
const accessToken = credentials.access_token;
@@ -39,53 +52,42 @@ async function processTrigger(msg, configuration) {
3952
const topic = `/event/${configuration.object}`;
4053
const replayId = -1;
4154
context.logger.debug('Creating streaming client');
42-
if (!fayeClient || status === 'down') {
43-
fayeClient = connection.streaming.createClient([
44-
new jsforce.StreamingExtension.Replay(topic, replayId),
45-
new jsforce.StreamingExtension.AuthFailure(async (err) => {
46-
context.logger.trace('AuthFailure error occurred');
47-
if (err.ext && err.ext.sfdc && err.ext.sfdc.failureReason && (err.ext.sfdc.failureReason === '401::Authentication invalid')) {
48-
try {
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');
52-
} catch (error) {
53-
context.logger.error('Failed to fetch refresh token');
54-
throw new Error('Failed to fetch refresh token');
55-
}
56-
fayeClient = undefined;
57-
context.logger.info('Lets call processTrigger one more time');
58-
await processTrigger.call(context, msg, configuration);
59-
} else {
60-
context.logger.error('AuthFailure extension error occurred');
61-
throw err;
62-
}
63-
}),
64-
]);
6555

66-
fayeClient.on('transport:down', () => {
67-
context.logger.error('Client is offline');
68-
context.emit('error', 'Client is offline');
69-
status = 'down';
56+
fayeClient = connection.streaming.createClient([
57+
new jsforce.StreamingExtension.Replay(topic, replayId),
58+
new jsforce.StreamingExtension.AuthFailure((err) => {
59+
let errMsg = '';
60+
try {
61+
errMsg = JSON.stringify(err);
62+
} catch (e) {
63+
errMsg = err;
64+
}
65+
context.logger.warn(`AuthFailure error occurred ${errMsg}`);
66+
refreshTokenNeeded = true;
67+
fayeClient = undefined;
68+
creationInProgress = false;
7069
context.logger.info('Lets call processTrigger one more time');
7170
processTrigger.call(context, msg, configuration);
72-
});
71+
}),
72+
]);
7373

74-
fayeClient.on('transport:up', () => {
75-
context.logger.info('Client is online');
76-
});
74+
fayeClient.on('transport:down', () => {
75+
context.logger.error('Client is offline');
76+
});
7777

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';
78+
fayeClient.on('transport:up', () => {
79+
context.logger.info('Client is online');
80+
});
8381

84-
context.logger.info('Subscribed to PushTopic successfully');
85-
context.logger.trace(`Subscribed to PushTopic: ${topic}`);
82+
await fayeClient.subscribe(topic, async (message) => {
83+
context.logger.info('Incoming message found, going to emit...');
84+
await context.emit('data', messages.newMessageWithBody(message));
85+
});
86+
creationInProgress = false;
87+
context.logger.info('Subscribed to PushTopic successfully');
88+
context.logger.trace(`Subscribed to PushTopic: ${topic}`);
8689

87-
context.logger.info('Streaming client created and ready');
88-
}
90+
context.logger.info('Streaming client created and ready');
8991
}
9092

9193
/**

0 commit comments

Comments
 (0)