Skip to content

Commit

Permalink
feat(whatislove-dev): add retry functionality for 3d party api calls …
Browse files Browse the repository at this point in the history
…wd-676 (#706)

* fix(root): edit cd cron expression wd-676

* feat(shared): update setAsyncTimeout signature wd-676

* feat(whatislove-dev): add retry functionality for api calls wd-676

* build(root): add cd action dispatch wd-676

* build(root): add log deploy output step wd-676
  • Loading branch information
what1s1ove authored Oct 6, 2024
1 parent a33c101 commit 270c554
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 6 deletions.
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'
45 changes: 45 additions & 0 deletions packages/shared/src/libs/helpers/retry-call/retry-call.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

0 comments on commit 270c554

Please sign in to comment.