-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
419 additions
and
240 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-- CreateTable | ||
CREATE TABLE "Job" ( | ||
"id" TEXT NOT NULL, | ||
"state" TEXT NOT NULL, | ||
"priority" INTEGER NOT NULL DEFAULT 0, | ||
"kind" TEXT NOT NULL, | ||
"data" JSONB NOT NULL, | ||
"delay" INTEGER, | ||
"retry" INTEGER, | ||
"runs" INTEGER NOT NULL DEFAULT 0, | ||
"force" BOOLEAN NOT NULL DEFAULT false, | ||
"cron" TEXT, | ||
"error" TEXT, | ||
"createdAt" TIMESTAMP NOT NULL DEFAULT timezone('utc'::text, now()), | ||
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT timezone('utc'::text, now()), | ||
|
||
CONSTRAINT "Job_pkey" PRIMARY KEY ("id") | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { prisma } from '../prisma'; | ||
import { MessageBody } from '../../modules/nodemailer'; | ||
|
||
export const defaultJobDelay = process.env.WORKER_JOBS_DELAY ? parseInt(process.env.WORKER_JOBS_DELAY, 10) : 1000; | ||
|
||
export enum jobState { | ||
scheduled = 'scheduled', | ||
pending = 'pending', | ||
completed = 'completed', | ||
} | ||
|
||
export interface JobDataMap { | ||
email: { | ||
data: MessageBody; | ||
}; | ||
} | ||
|
||
export type JobKind = keyof JobDataMap; | ||
|
||
interface CreateJobProps<K extends keyof JobDataMap> { | ||
data: JobDataMap[K]; | ||
priority?: number; | ||
delay?: number; | ||
cron?: string; | ||
} | ||
|
||
export function createJob<K extends keyof JobDataMap>( | ||
kind: K, | ||
{ data, priority, delay = defaultJobDelay, cron }: CreateJobProps<K>, | ||
) { | ||
return prisma.job.create({ | ||
data: { | ||
state: jobState.scheduled, | ||
data, | ||
kind, | ||
priority, | ||
delay, | ||
cron, | ||
}, | ||
}); | ||
} | ||
|
||
export function createEmailJob(data: MessageBody) { | ||
return createJob('email', { data: { data } }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as Sentry from '@sentry/nextjs'; | ||
import { worker, Job } from '@taskany/bricks'; | ||
|
||
import config from '../../config'; | ||
|
||
import { defaultJobDelay } from './create'; | ||
import * as resolve from './resolve'; | ||
import { getNextJob, jobDelete, jobUpdate } from './jobOperations'; | ||
|
||
const queueInterval = config.worker.queueInterval ? parseInt(config.worker.queueInterval, 10) : 3000; | ||
const retryLimit = config.worker.retryLimit ? parseInt(config.worker.retryLimit, 10) : 3; | ||
|
||
// eslint-disable-next-line no-console | ||
const log = (...rest: unknown[]) => console.log('[WORKER]:', ...rest); | ||
|
||
log('Worker started successfully'); | ||
|
||
const onRetryLimitExeed = (error: any, job: Job) => | ||
Sentry.captureException(error, { | ||
fingerprint: ['worker', 'resolve', 'retry'], | ||
extra: { | ||
job, | ||
}, | ||
}); | ||
|
||
const onQueeTooLong = () => Sentry.captureMessage('Queue too long. Smth went wrong.'); | ||
|
||
// eslint-disable-next-line no-console | ||
const onError = (error: any) => console.log(error.message); | ||
|
||
const init = () => | ||
worker( | ||
getNextJob, | ||
jobUpdate, | ||
jobDelete, | ||
resolve, | ||
onRetryLimitExeed, | ||
onQueeTooLong, | ||
log, | ||
onError, | ||
defaultJobDelay, | ||
retryLimit, | ||
); | ||
|
||
(() => | ||
setInterval(async () => { | ||
await init(); | ||
}, queueInterval))(); |
Oops, something went wrong.