All files / src/queues index.ts

0% Statements 0/74
0% Branches 0/28
0% Functions 0/11
0% Lines 0/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170                                                                                                                                                                                                                                                                                                                                                   
import { AboutService } from "@billos/firefly-iii-sdk"
import { DelayedError, Job, Worker } from "bullmq"
import { DateTime } from "luxon"
import pino from "pino"
 
import { client } from "../client"
import { env } from "../config"
import { notifier } from "../modules/notifiers"
import { BaseJob, SimpleJob } from "./jobs/BaseJob"
import { budgetJobs, endpointJobs, simpleJobs, transactionJobs } from "./jobs/index"
import { getQueue } from "./queue"
import { BudgetJobArgs, EndpointJobArgs, isBudgetJob, isEndpointJob, isTransactionJob, QueueArgs, TransactionJobArgs } from "./queueArgs"
 
const logger = pino()
 
const startedAt = new Map<string, DateTime>()
 
const jobMap = new Map<string, BaseJob>([...simpleJobs, ...transactionJobs, ...endpointJobs, ...budgetJobs].map((j) => [j.id, j]))
 
let worker: Worker<QueueArgs> | null = null
 
function logJobDuration(success: boolean, jobId: string, name: string) {
  const startTime = startedAt.get(jobId)
  const successStr = success ? "completed" : "failed"
  const endTime = DateTime.now()
  if (startTime) {
    const duration = endTime.diff(startTime, "seconds").seconds
    logger.info(
      "******************************************************************************** Job(%s) %s %s in %d seconds",
      jobId,
      name,
      successStr,
      duration,
    )
    startedAt.delete(jobId)
  } else {
    logger.info("******************************************************************************** Job(%s) %s %s", jobId, name, successStr)
  }
}
 
async function delayJob(job: Job<QueueArgs>, err: Error): Promise<void> {
  const retryCount = (job.data.retryCount || 0) + 1
  const jobInstance = jobMap.get(job.data.job)
  const delayMs = jobInstance ? jobInstance.getRetryDelay(retryCount) : retryCount * 60 * 1000
  const delayed = DateTime.now().plus({ milliseconds: delayMs })
  const timestamp = delayed.toMillis()
  const title = err.message
  const message = [
    `Job **${job.data.job}** (${job.id}) failed with error ${err.message}.`,
    `Attempt: ${retryCount}`,
    `Delaying until ${delayed.toISOTime()} with data ${JSON.stringify(job.data)}.`,
  ].join("\n")
  const delayedMessageId = await notifier.sendMessage(title, message)
 
  logger.info(
    "Delaying job %s (%s) until %s - Attempt: %d - Error: %s",
    job.id,
    job.name,
    delayed.toISO(),
    retryCount,
    err?.message ?? "Unknown error",
  )
  await job.updateData({ ...job.data, delayedMessageId, retryCount })
  await job.moveToDelayed(timestamp, job.token)
  throw new DelayedError("Job delayed due to error")
}
 
async function initializeJobs(): Promise<void> {
  logger.info("Initializing job definitions")
  for (const instance of [...simpleJobs, ...transactionJobs, ...budgetJobs, ...endpointJobs]) {
    await instance.init()
  }
}
 
async function initializeWorker(): Promise<Worker<QueueArgs>> {
  if (worker) {
    return worker
  }
 
  const queue = await getQueue()
 
  // Clean up any stale jobs from previous runs
  queue.setGlobalConcurrency(1)
  await queue.pause()
  await queue.resume()
 
  worker = new Worker<QueueArgs>(
    "manager",
    async (job) => {
      try {
        const { data } = job
        await AboutService.getAbout({ client })
        const jobInstance = jobMap.get(data.job)
        if (!jobInstance) {
          throw new Error(`Unknown job: ${data.job}`)
        }
        if (isTransactionJob(jobInstance)) {
          await jobInstance.run((data as TransactionJobArgs).transactionId)
        } else if (isBudgetJob(jobInstance)) {
          await jobInstance.run((data as BudgetJobArgs).budgetId)
        } else if (isEndpointJob(jobInstance)) {
          await jobInstance.run((data as EndpointJobArgs).transactionId, (data as EndpointJobArgs).data)
        } else {
          await (jobInstance as SimpleJob).run()
        }
      } catch (err) {
        const jobInstance = jobMap.get(job.data.job)
        if (!jobInstance?.retryable) {
          throw err
        }
        await delayJob(job, err as Error)
      }
    },
    {
      connection: env.redisConnection,
      concurrency: 1,
      removeOnComplete: { count: 5000 },
      removeOnFail: { count: 5000 },
    },
  )
 
  worker.on("active", async ({ id, name, data }) => {
    logger.info("******************************************************************************** Job(%s) %s started", id, name)
    startedAt.set(id, DateTime.now())
    if (data.delayedMessageId) {
      logger.info("Deleting delayed message %s for job %s (%s)", data.delayedMessageId, id, name)
      await notifier.deleteMessage(data.delayedMessageId)
    }
  })
 
  worker.on("completed", async ({ id, name, data }) => {
    if (data.delayedMessageId) {
      logger.info("Deleting delayed message %s for job %s (%s)", data.delayedMessageId, id, name)
      await notifier.deleteMessage(data.delayedMessageId)
    }
    logJobDuration(true, id, name)
  })
 
  worker.on("failed", (job, err) => {
    logger.error({ err }, "Job %s failed with error %s", job.id, err.message)
    notifier.sendMessage(
      "Job Failed",
      `Job **${job.data.job}** (${job.id}) failed with error ${err.message} and data ${JSON.stringify(job.data)}`,
    )
 
    logJobDuration(false, job.id, job.name)
  })
 
  worker.on("ready", () => {
    logger.info("Worker is ready and connected to Redis")
  })
 
  return worker
}
 
async function processExit() {
  if (worker) {
    await worker.close()
  }
}
 
process.on("SIGTERM", processExit)
process.on("SIGINT", processExit)
 
export { initializeWorker, initializeJobs }
 
export { simpleJobs, transactionJobs, budgetJobs }
 
export { getQueue } from "./queue"