Skip to content

Commit

Permalink
hotfix(condo): add retrying push for AppleMessaging (#4327)
Browse files Browse the repository at this point in the history
(cherry picked from commit 9ba8e2d)
  • Loading branch information
VKislov authored and sitozzz committed Feb 5, 2024
1 parent 6f1063e commit 8242130
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
17 changes: 13 additions & 4 deletions apps/condo/domains/notification/adapters/apple/AppleMessaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
APS_PUSH_TYPE_VOIP,
APS_PUSH_TYPE_BACKGROUND,
APS_RESPONSE_STATUS_SUCCESS,
RETRY_RESTRICTION,
} = require('./constants')

const logger = getLogger('AppleMessaging')
Expand Down Expand Up @@ -130,9 +131,9 @@ class AppleMessaging {
const stream = this.#session.request(headers)

stream.on('response', this.getResponseHandler(stream, resolve, reject))
stream.on('error', (error) => {
logger.error({ msg: 'sendPush errored', headers, options, payload, streamError: error })
return resolve(error)
stream.on('error', (err) => {
logger.error({ msg: 'sendPush errored', headers, options, payload, err })
return resolve(err)
})
stream.write(buffer)
stream.end()
Expand Down Expand Up @@ -161,7 +162,15 @@ class AppleMessaging {
payload.aps.alert = { ...notification }
}

const response = await this.sendPush(token, payload, options)
let response
for (let retryCounter = 0; retryCounter < RETRY_RESTRICTION; retryCounter++) {
response = await this.sendPush(token, payload, options)
if (response instanceof Error) {
logger.warn({ msg: `sendPush not successful on ${retryCounter + 1} try`, err: response })
continue
}
break
}

responses.push(response)

Expand Down
2 changes: 2 additions & 0 deletions apps/condo/domains/notification/adapters/apple/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const ERRORS = {
500: 'Internal server error.',
503: 'The server is shutting down and unavailable.',
}
const RETRY_RESTRICTION = 5

// https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns
const APS_PUSH_TYPE_ALERT = 'alert'
Expand All @@ -25,4 +26,5 @@ module.exports = {
APS_PUSH_TYPE_VOIP,
APS_PUSH_TYPE_BACKGROUND,
APS_RESPONSE_STATUS_SUCCESS,
RETRY_RESTRICTION,
}
6 changes: 3 additions & 3 deletions apps/condo/domains/notification/adapters/appleAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,10 @@ class AppleAdapter {
}

result = AppleAdapter.injectFakeResults(appleResult, fakeNotifications)
} catch (error) {
logger.error({ msg: 'sendNotification error', error })
} catch (err) {
logger.error({ msg: 'sendNotification error', err })

result = { state: 'error', error }
result = { state: 'error', error: err }
}
}

Expand Down

0 comments on commit 8242130

Please sign in to comment.