Skip to content
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,7 @@ const FormData = require('form-data')

const config = require('../../config.js')
const Utils = require('../../utils')
const asyncRetry = require('../../utils/asyncRetry')
const { logger: genericLogger } = require('../../logging')
const {
generateTimestampAndSignatureForSPVerification
Expand DownExpand Up@@ -202,7 +203,7 @@ class TrackTranscodeHandoffManager {
`Polling for transcode and segments with uuid=${uuid}...`
)

return Utils.asyncRetry({
return asyncRetry({
logger: TrackTranscodeHandoffManager.logger,
asyncFn: async (bail, num) => {
if (num === 50) {
Expand DownExpand Up@@ -593,7 +594,7 @@ class TrackTranscodeHandoffManager {
return resp
}

return Utils.asyncRetry({ logger, asyncFn, logLabel, options })
return asyncRetry({ logger, asyncFn, logLabel, options })
}
}

Expand Down
2 changes: 1 addition & 1 deletion creator-node/src/redis.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,7 +6,7 @@ const Redis = require('ioredis')

const config = require('./config.js')
const { logger: genericLogger } = require('./logging')
const { asyncRetry } = require('./utils')
const asyncRetry = require('./utils/asyncRetry')

const redisClient = new Redis(config.get('redisPort'), config.get('redisHost'))

Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
const _ = require('lodash')
const axios = require('axios')
const { CancelToken } = axios
const retry = require('async-retry')

const config = require('../../../config')
const Utils = require('../../../utils')
const asyncRetry = require('../../../utils/asyncRetry')
const { logger } = require('../../../logging')

const { isPrimaryHealthy } = require('../CNodeHealthManager')
Expand DownExpand Up@@ -35,7 +34,7 @@ const getLatestUserIdFromDiscovery = async (discoveryNodeEndpoint) => {
let latestUserId = 0
try {
// Request all users that have this node as a replica (either primary or secondary)
const resp = await Utils.asyncRetry({
const resp = await asyncRetry({
logLabel: 'fetch the ID of the newest user on Audius',
asyncFn: async () => {
return axios({
Expand DownExpand Up@@ -89,7 +88,7 @@ const getNodeUsers = async (
)

// Request all users that have this node as a replica (either primary or secondary)
const resp = await Utils.asyncRetry({
const resp = await asyncRetry({
logLabel: 'fetch all users with this node in replica',
asyncFn: async () => {
return axios({
Expand DownExpand Up@@ -459,7 +458,7 @@ const computeSyncModeForUserAndReplica = async ({
let primaryFilesHashForRange
try {
// Throws error if fails after all retries
primaryFilesHashForRange = await Utils.asyncRetry({
primaryFilesHashForRange = await asyncRetry({
asyncFn: async () =>
DBManager.fetchFilesHashFromDB({
lookupKey: { lookupWallet: wallet },
Expand Down
4 changes: 2 additions & 2 deletions creator-node/src/snapbackSM/peerSetManager.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,7 @@ const axios = require('axios')
const { CancelToken } = axios

const config = require('../config')
const Utils = require('../utils')
const asyncRetry = require('../utils/asyncRetry')
const { logger } = require('../logging')
const {
GET_NODE_USERS_TIMEOUT_MS,
Expand DownExpand Up@@ -145,7 +145,7 @@ class PeerSetManager {
)

// Request all users that have this node as a replica (either primary or secondary)
const resp = await Utils.asyncRetry({
const resp = await asyncRetry({
logLabel: 'fetch all users with this node in replica',
asyncFn: async () => {
return axios({
Expand Down
3 changes: 2 additions & 1 deletion creator-node/src/snapbackSM/snapbackSM.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,6 +4,7 @@ const _ = require('lodash')
const retry = require('async-retry')

const Utils = require('../utils')
const asyncRetry = require('../utils/asyncRetry')
const models = require('../models')
const { logger } = require('../logging')
const redis = require('../redis.js')
Expand DownExpand Up@@ -1998,7 +1999,7 @@ class SnapbackSM {
let latestUserId = 0
try {
// Request all users that have this node as a replica (either primary or secondary)
const resp = await Utils.asyncRetry({
const resp = await asyncRetry({
logLabel: 'fetch all users with this node in replica',
asyncFn: async () => {
return axios({
Expand Down
44 changes: 0 additions & 44 deletions creator-node/src/utils.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,7 +4,6 @@ const path = require('path')
const axios = require('axios')
const spawn = require('child_process').spawn
const stream = require('stream')
const retry = require('async-retry')
const { promisify } = require('util')
const pipeline = promisify(stream.pipeline)
const { logger: genericLogger } = require('./logging.js')
Expand DownExpand Up@@ -351,48 +350,6 @@ function currentNodeShouldHandleTranscode({
return currentNodeShouldHandleTranscode
}

/**
* Wrapper around async-retry API.
*
* options described here https://github.com/tim-kos/node-retry#retrytimeoutsoptions
* @param {Object} param
* @param {func} param.asyncFn the fn to asynchronously retry
* @param {Object} param.options optional options. defaults to the params listed below if not explicitly passed in
* @param {number} [param.options.factor=2] the exponential factor
* @param {number} [param.options.retries=5] the max number of retries. defaulted to 5
* @param {number} [param.options.minTimeout=1000] minimum number of ms to wait after first retry. defaulted to 1000ms
* @param {number} [param.options.maxTimeout=5000] maximum number of ms between two retries. defaulted to 5000ms
* @param {func} [param.options.onRetry] fn that gets called per retry
* @param {Object} param.logger
* @param {Boolean} param.log enables/disables logging
* @param {string?} param.logLabel
* @returns the fn response if success, or throws an error
*/
function asyncRetry({
asyncFn,
options = {},
logger = genericLogger,
log = true,
logLabel = null
}) {
options = {
retries: 5,
factor: 2,
minTimeout: 1000,
maxTimeout: 5000,
onRetry: (err, i) => {
if (err && log) {
const logPrefix =
(logLabel ? `[${logLabel}] ` : '') + `[asyncRetry] [attempt #${i}]`
logger.warn(`${logPrefix}: `, err.message || err)
}
},
...options
}

return retry(asyncFn, options)
}

module.exports = Utils
module.exports.validateStateForImageDirCIDAndReturnFileUUID =
validateStateForImageDirCIDAndReturnFileUUID
Expand All@@ -402,4 +359,3 @@ module.exports.findCIDInNetwork = findCIDInNetwork
module.exports.runShellCommand = runShellCommand
module.exports.currentNodeShouldHandleTranscode =
currentNodeShouldHandleTranscode
module.exports.asyncRetry = asyncRetry
45 changes: 45 additions & 0 deletions creator-node/src/utils/asyncRetry.js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
const retry = require('async-retry')

const { logger: genericLogger } = require('../logging')

/**
* Wrapper around async-retry API.
*
* options described here https://github.com/tim-kos/node-retry#retrytimeoutsoptions
* @param {Object} param
* @param {func} param.asyncFn the fn to asynchronously retry
* @param {Object} param.options optional options. defaults to the params listed below if not explicitly passed in
* @param {number} [param.options.factor=2] the exponential factor
* @param {number} [param.options.retries=5] the max number of retries. defaulted to 5
* @param {number} [param.options.minTimeout=1000] minimum number of ms to wait after first retry. defaulted to 1000ms
* @param {number} [param.options.maxTimeout=5000] maximum number of ms between two retries. defaulted to 5000ms
* @param {func} [param.options.onRetry] fn that gets called per retry
* @param {Object} param.logger
* @param {Boolean} param.log enables/disables logging
* @param {string?} param.logLabel
* @returns the fn response if success, or throws an error
*/
module.exports = function asyncRetry({
asyncFn,
options = {},
logger = genericLogger,
log = true,
logLabel = null
}) {
options = {
retries: 5,
factor: 2,
minTimeout: 1000,
maxTimeout: 5000,
onRetry: (err, i) => {
if (err && log) {
const logPrefix =
(logLabel ? `[${logLabel}] ` : '') + `[asyncRetry] [attempt #${i}]`
logger.warn(`${logPrefix}: `, err.message || err)
}
},
...options
}

return retry(asyncFn, options)
}
7 changes: 4 additions & 3 deletions creator-node/test/utils.test.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ const { logger: genericLogger } = require('../src/logging')

// Module under test
const Utils = require('../src/utils')
const asyncRetry = require('../src/utils/asyncRetry')

// Partially tested test file!!

Expand DownExpand Up@@ -47,7 +48,7 @@ describe('test src/utils.js', () => {

let didRetry = false
try {
await Utils.asyncRetry({
await asyncRetry({
logger: genericLogger,
asyncFn: async () => {
return axios({ url: 'https://content_node.com/404', method: 'get' })
Expand DownExpand Up@@ -76,7 +77,7 @@ describe('test src/utils.js', () => {

let didRetry = false
try {
await Utils.asyncRetry({
await asyncRetry({
logger: genericLogger,
asyncFn: async () => {
return axios({
Expand DownExpand Up@@ -107,7 +108,7 @@ describe('test src/utils.js', () => {

let didRetry = false
try {
const resp = await Utils.asyncRetry({
const resp = await asyncRetry({
logger: genericLogger,
asyncFn: async () => {
return axios({
Expand Down