96bed71022
* Improves error logging
- use pino error logger correctly by passing in error as first argument
* monitor deployment: Filter logging at INFO level and above
* Use structured logging to create parameters for monitoring results
* Add structured logging to obj fileimport service
* Fileimport service, fix and improve logging
- use child logger with additional context where possible
- select appropriate logging level
- fix duplicated context in log statement
* REST endpoints, add context to structured logging and remove same context from message
* Webhook service provides context to bound logger to properly use structured logging
- Pass bound logger containing context to `makeNetworkRequest`
- do not log url, as it may contain a secret (like Discord's webhook urls), instead log the webhook Id
- log error message when network call fails
* upload: make better use of structured logging when recording data
* pino-pretty when in dev or test mode
- pino-pretty configured to send to stderr
* LOG_PRETTY env var
* Silence structured logging during testing
- can not rely on determining the port number by reading from stdout/stderr
- instead we determine which port is free, then create our server on that port
- we then poll that port until the server is ready before commencing tests
* Allow puppeteer to install chromium
* Do not need to install chromium separately
72 lines
2.3 KiB
TypeScript
72 lines
2.3 KiB
TypeScript
import cron from 'node-cron'
|
|
import { InvalidArgumentError } from '@/modules/shared/errors'
|
|
import { ensureError } from '@/modules/shared/helpers/errorHelper'
|
|
import { acquireTaskLock } from '@/modules/core/repositories/scheduledTasks'
|
|
import { ScheduledTaskRecord } from '@/modules/core/helpers/types'
|
|
import { activitiesLogger } from '@/logging/logging'
|
|
|
|
export const scheduledCallbackWrapper = async (
|
|
scheduledTime: Date,
|
|
taskName: string,
|
|
lockTimeout: number,
|
|
callback: (scheduledTime: Date) => Promise<void>,
|
|
acquireLock: (
|
|
scheduledTask: ScheduledTaskRecord
|
|
) => Promise<ScheduledTaskRecord | null>
|
|
) => {
|
|
const boundLogger = activitiesLogger.child({ taskName })
|
|
// try to acquire the task lock with the function name and a new expiration date
|
|
const lockExpiresAt = new Date(scheduledTime.getTime() + lockTimeout)
|
|
try {
|
|
const lock = await acquireLock({ taskName, lockExpiresAt })
|
|
|
|
// if couldn't acquire it, stop execution
|
|
if (!lock) {
|
|
boundLogger.warn(
|
|
`Could not acquire task lock for ${taskName}, stopping execution.`
|
|
)
|
|
return null
|
|
}
|
|
|
|
// else continue executing the callback...
|
|
boundLogger.info(`Executing scheduled function ${taskName} at ${scheduledTime}`)
|
|
await callback(scheduledTime)
|
|
// update lock as succeeded
|
|
const finishDate = new Date()
|
|
boundLogger.info(
|
|
`Finished scheduled function ${taskName} execution in ${
|
|
(finishDate.getTime() - scheduledTime.getTime()) / 1000
|
|
} seconds`
|
|
)
|
|
} catch (error) {
|
|
boundLogger.error(
|
|
error,
|
|
`The triggered task execution ${taskName} failed at ${scheduledTime}, with error ${
|
|
ensureError(error, 'unknown reason').message
|
|
}`
|
|
)
|
|
}
|
|
}
|
|
|
|
export const scheduleExecution = (
|
|
cronExpression: string,
|
|
taskName: string,
|
|
callback: (scheduledTime: Date) => Promise<void>,
|
|
lockTimeout = 60 * 1000
|
|
): cron.ScheduledTask => {
|
|
const expressionValid = cron.validate(cronExpression)
|
|
if (!expressionValid)
|
|
throw new InvalidArgumentError(
|
|
`The given cron expression ${cronExpression} is not valid`
|
|
)
|
|
return cron.schedule(cronExpression, async (scheduledTime: Date) => {
|
|
await scheduledCallbackWrapper(
|
|
scheduledTime,
|
|
taskName,
|
|
lockTimeout,
|
|
callback,
|
|
acquireTaskLock
|
|
)
|
|
})
|
|
}
|