Skip to content

Commit

Permalink
add delay between synology requests
Browse files Browse the repository at this point in the history
  • Loading branch information
rush42 committed Nov 19, 2024
1 parent 8db9e6d commit a8933ef
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
8 changes: 4 additions & 4 deletions processing/steps/processTopics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export async function processTopics(
const skipCode = skipUnchanged && !helpersChanged && !fileChanged
const diffChanges = computeDiffs && !fileChanged

await logStart('Processing')
logStart('Processing')
for (const topic of topics) {
// get all tables related to `topic`
const topicTables = await getTopicTables(topic)
Expand All @@ -114,7 +114,7 @@ export async function processTopics(
`⏩ Skipping topic "${topic}". The code hasn't changed and SKIP_UNCHANGED is active.`,
)
} else {
await logStart(`Topic "${topic}"`)
logStart(`Topic "${topic}"`)

const processedTopicTables = topicTables.intersection(tableListPublic)

Expand All @@ -139,10 +139,10 @@ export async function processTopics(
await diffTables(Array.from(processedTopicTables))
}

await logEnd(`Topic "${topic}"`)
logEnd(`Topic "${topic}"`)
}
}

const timeElapsed = await logEnd('Processing')
const timeElapsed = logEnd('Processing')
return { timeElapsed, processedTables: Array.from(processedTables) }
}
6 changes: 3 additions & 3 deletions processing/utils/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ const lineLength = process.stdout.columns || 120
export function logPadded(left: string, right: string = '') {
console.log(chalk.inverse(left.padEnd(lineLength - right.length) + right))
}
export async function logStart(id: string) {
export function logStart(id: string) {
const message = `${id} started`
await synologyLogInfo(message)
synologyLogInfo(message)
logPadded(message)
startTimer(id)
}

export async function logEnd(id: string) {
export function logEnd(id: string) {
const timeElapsed = endTimer(id)
const timeFormatted = formatTimestamp(timeElapsed)

Expand Down
13 changes: 13 additions & 0 deletions processing/utils/synology.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { params } from './parameters'

// this is the delay we wait between two consecutive requests to synology
const requestDelayMs = 10000
let lastRequest = 0

async function logToSynology(message: string, token: string) {
// if the URL is not set, we don't log
if (!params.synologyURL) {
return
}

// due to the rate limit we need to wait between two consecuitive requests
const timeElapsed = Date.now() - lastRequest
lastRequest = Date.now()
if (timeElapsed < requestDelayMs) {
await new Promise((resolve) => setTimeout(resolve, requestDelayMs - timeElapsed))
}

// prepare the URL
const synologyParams = {
token: decodeURIComponent(token), // the token is already encoded in the env
Expand All @@ -31,6 +42,8 @@ async function logToSynology(message: string, token: string) {
},
body,
})

// error handling
if (!response.ok) {
throw new Error(`${response.statusText}`)
}
Expand Down

0 comments on commit a8933ef

Please sign in to comment.