Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(whatislove-dev): add retry functionality for 3d party api calls wd-676 #706

Merged
merged 5 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ name: Continuous Delivery

on:
schedule:
- cron: '0 0 * * *'
- cron: '0 12 * * *'
push:
branches:
- main
workflow_dispatch:

jobs:
release:
Expand All @@ -30,7 +31,7 @@ jobs:
dependencies:
name: Install Dependencies
needs: release
if: ${{ needs.release.outputs.release_created || github.event_name == 'schedule' }}
if: ${{ needs.release.outputs.release_created || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' }}
runs-on: ubuntu-latest
steps:
- name: Code Checkout
Expand Down Expand Up @@ -131,3 +132,6 @@ jobs:
args: 'deploy --json --prod --site ${{ matrix.project }} --filter ${{ env.PACKAGE_DIR }} --dir ${{ env.PACKAGE_DIR }}/build'
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}

- name: Log Deploy Output
run: echo "${{ steps.deployment.outputs.NETLIFY_OUTPUT }}"
6 changes: 5 additions & 1 deletion apps/whatislove-dev/src/data/mentions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { retryCall } from '@whatislove.dev/shared'
import { parseHTML } from 'linkedom'

import { default as environment } from './environment.js'
Expand Down Expand Up @@ -270,7 +271,10 @@ let loader = async () => {
let allPagesMentions = /** @type {PagesMentions} */ ({})

for (let mentionLoader of mentionLoaders) {
let fetchedPageMentions = await mentionLoader()
let fetchedPageMentions = await retryCall({
callback: mentionLoader,
retriesCount: 3,
})

for (let [pageUrl, pageMentions] of Object.entries(
fetchedPageMentions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Toast {

toastNode.textContent = message

await setAsyncTimeout(() => {}, duration)
await setAsyncTimeout(undefined, duration)

await setAsyncTimeout(() => {
toastNode.textContent = ``
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
getRandomNumber,
getShuffledItems,
initDebounce,
retryCall,
setAsyncTimeout,
} from './libs/helpers/helpers.js'
export { ValuesOf } from './libs/types/types.js'
1 change: 1 addition & 0 deletions packages/shared/src/libs/helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export { getISODate } from './get-iso-date/get-iso-date.helper.js'
export { getRandomNumber } from './get-random-number/get-random-number.helper.js'
export { getShuffledItems } from './get-shuffled-items/get-shuffled-items.helper.js'
export { initDebounce } from './init-debounce/init-debounce.helper.js'
export { retryCall } from './retry-call/retry-call.helper.js'
export { setAsyncTimeout } from './set-async-timeout/set-async-timeout.helper.js'
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { setAsyncTimeout } from '../set-async-timeout/set-async-timeout.helper.js'

let MINIMAL_RETRY_COUNT_FOR_CALL = /** @type {const} */ (0)
let RETRY_COUNT_DECREMENT_PER_CALL = /** @type {const} */ (1)

/**
* @template {unknown} T
* @param {{
* callback: (...args: unknown[]) => T | Promise<T>
* retriesCount?: number
* delayMs?: number
* delayFactorCount?: number
* }} params
* @returns {Promise<T>}
*/
let retryCall = async ({
callback,
delayFactorCount = 3,
delayMs = 1000,
retriesCount = 3,
}) => {
try {
return await callback()
} catch (error) {
let hasRetries = retriesCount > MINIMAL_RETRY_COUNT_FOR_CALL

if (!hasRetries) {
throw error
}

await setAsyncTimeout(undefined, delayMs)

let updatedDelayMs = delayMs * delayFactorCount
let updatedTriesCount = retriesCount - RETRY_COUNT_DECREMENT_PER_CALL

return retryCall({
callback,
delayFactorCount,
delayMs: updatedDelayMs,
retriesCount: updatedTriesCount,
})
}
}

export { retryCall }
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* @param {(...args: unknown[]) => unknown} callback
* @param {((...args: unknown[]) => unknown) | undefined} callback
* @param {number} [timeout]
* @returns {Promise<void>}
*/
let setAsyncTimeout = (callback, timeout = 0) => {
return new Promise((resolve) => {
setTimeout(() => {
callback()
callback?.()
resolve()
}, timeout)
})
Expand Down
Loading