Commit 3a0d6c1c9e1680cfc134bb80e4e14bae1206b9ab
1 parent
685ba2f25b
Exists in
master
Add SLEEP_BEFORE_TERMINATE_ON_ERROR_MS
Showing 1 changed file with 8 additions and 2 deletions Inline Diff
lib/transport.js
1 | const MODULE_NAME = 'TRANSPORT'; | 1 | const MODULE_NAME = 'TRANSPORT'; |
2 | const SLEEP_BEFORE_TERMINATE_ON_ERROR_MS = 5 * 1000; | ||
2 | 3 | ||
3 | const bot = require("simple-xmpp"); | 4 | const bot = require("simple-xmpp"); |
4 | const config = require('komodo-sdk/config'); | 5 | const config = require('komodo-sdk/config'); |
5 | const logger = require('komodo-sdk/logger'); | 6 | const logger = require('komodo-sdk/logger'); |
6 | const messagingService = require('komodo-center-messaging-client-lib'); | 7 | const messagingService = require('komodo-center-messaging-client-lib'); |
7 | 8 | ||
8 | let isReady; | 9 | let isReady; |
9 | 10 | ||
10 | bot.on('online', function(data) { | 11 | bot.on('online', function(data) { |
11 | logger.info('XMPP transport connected, JID: ' + data.jid.user); | 12 | logger.info('XMPP transport connected, JID: ' + data.jid.user); |
12 | bot.getRoster(); | 13 | bot.getRoster(); |
13 | 14 | ||
14 | setTimeout( | 15 | setTimeout( |
15 | function() { | 16 | function() { |
16 | isReady = true; | 17 | isReady = true; |
17 | 18 | ||
18 | logger.verbose('Transport is ready'); | 19 | logger.verbose('Transport is ready'); |
19 | 20 | ||
20 | if (messagingService.onOnline) { | 21 | if (messagingService.onOnline) { |
21 | messagingService.onOnline({ | 22 | messagingService.onOnline({ |
22 | me: config.username, | 23 | me: config.username, |
23 | }); | 24 | }); |
24 | } | 25 | } |
25 | }, | 26 | }, |
26 | 27 | ||
27 | config.warming_up_ms || (30 * 1000) | 28 | config.warming_up_ms || (30 * 1000) |
28 | ) | 29 | ) |
29 | }) | 30 | }) |
30 | 31 | ||
31 | bot.on('chat', function(partner, msg) { | 32 | bot.on('chat', function(partner, msg) { |
32 | if (!msg || !msg.trim()) { | 33 | if (!msg || !msg.trim()) { |
33 | return; | 34 | return; |
34 | } | 35 | } |
35 | 36 | ||
36 | if (partner == config.username.replace(/\/.*$/, '')) { | 37 | if (partner == config.username.replace(/\/.*$/, '')) { |
37 | return; | 38 | return; |
38 | } | 39 | } |
39 | 40 | ||
40 | if (!isReady) { | 41 | if (!isReady) { |
41 | logger.warn('Warming up is not finished yet, ignoring message', {me: config.username, partner: partner, msg: msg}); | 42 | logger.warn('Warming up is not finished yet, ignoring message', {me: config.username, partner: partner, msg: msg}); |
42 | return; | 43 | return; |
43 | } | 44 | } |
44 | 45 | ||
45 | logger.info('Incoming message via XMPP transport', {me: config.username, partner: partner, msg: msg}); | 46 | logger.info('Incoming message via XMPP transport', {me: config.username, partner: partner, msg: msg}); |
46 | 47 | ||
47 | if (messagingService && messagingService.onIncomingMessage) { | 48 | if (messagingService && messagingService.onIncomingMessage) { |
48 | messagingService.onIncomingMessage( | 49 | messagingService.onIncomingMessage( |
49 | { | 50 | { |
50 | me: config.username, | 51 | me: config.username, |
51 | partner: partner, | 52 | partner: partner, |
52 | msg: msg.trim() | 53 | msg: msg.trim() |
53 | } | 54 | } |
54 | ) | 55 | ) |
55 | } | 56 | } |
56 | }); | 57 | }); |
57 | 58 | ||
58 | bot.on('error', (err) => { | 59 | bot.on('error', (err) => { |
59 | logger.warn(`${MODULE_NAME} F2E53C12: Error detected.`, { | 60 | logger.warn(`${MODULE_NAME} F2E53C12: Error detected.`, { |
60 | eCode: err.code, | 61 | eCode: err.code, |
61 | eMessage: err.message, | 62 | eMessage: err.message, |
62 | }); | 63 | }); |
63 | 64 | ||
64 | if (!config.do_not_terminate_on_error) { | 65 | if (!config.do_not_terminate_on_error) { |
65 | logger.warn(`${MODULE_NAME} BA6C0C55: Terminating on error`); | 66 | logger.warn(`${MODULE_NAME} BA6C0C55: Terminating on error`, { |
66 | process.exit(1); | 67 | millisecondSleepBeforeTerminate: SLEEP_BEFORE_TERMINATE_ON_ERROR_MS, |
68 | }); | ||
69 | |||
70 | setTimeout(() => { | ||
71 | process.exit(1); | ||
72 | }, SLEEP_BEFORE_TERMINATE_ON_ERROR_MS) | ||
67 | } | 73 | } |
68 | }); | 74 | }); |
69 | 75 | ||
70 | function send(partner, msg) { | 76 | function send(partner, msg) { |
71 | logger.verbose('Sending message via XMPP transport', {transport: 'xmpp', me: config.username, partner: partner, msg: msg}); | 77 | logger.verbose('Sending message via XMPP transport', {transport: 'xmpp', me: config.username, partner: partner, msg: msg}); |
72 | bot.send(partner, msg); | 78 | bot.send(partner, msg); |
73 | } | 79 | } |
74 | 80 | ||
75 | bot.on('subscribe', function(from) { | 81 | bot.on('subscribe', function(from) { |
76 | logger.verbose('Incoming subscribe request from ' + from); | 82 | logger.verbose('Incoming subscribe request from ' + from); |
77 | bot.acceptSubscription(from); | 83 | bot.acceptSubscription(from); |
78 | bot.subscribe(from); | 84 | bot.subscribe(from); |
79 | }) | 85 | }) |
80 | 86 | ||
81 | function ping() { | 87 | function ping() { |
82 | isReady && bot.send(config.username, 'PING!'); | 88 | isReady && bot.send(config.username, 'PING!'); |
83 | } | 89 | } |
84 | 90 | ||
85 | function init() { | 91 | function init() { |
86 | messagingService.setTransport(exports); | 92 | messagingService.setTransport(exports); |
87 | 93 | ||
88 | bot.connect({ | 94 | bot.connect({ |
89 | jid: config.username, | 95 | jid: config.username, |
90 | password: config.password, | 96 | password: config.password, |
91 | host: config.xmpp_host | 97 | host: config.xmpp_host |
92 | }); | 98 | }); |
93 | 99 | ||
94 | setInterval( | 100 | setInterval( |
95 | ping, | 101 | ping, |
96 | config.ping_interval_ms || 60000 | 102 | config.ping_interval_ms || 60000 |
97 | ) | 103 | ) |
98 | } | 104 | } |
99 | 105 | ||
100 | init(); | 106 | init(); |
101 | 107 | ||
102 | exports.init = init; | 108 | exports.init = init; |
103 | exports.send = send; | 109 | exports.send = send; |