mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-24 05:07:06 +08:00
chore(server): adjust job config (#11405)
This commit is contained in:
@@ -56,15 +56,9 @@
|
|||||||
"properties": {
|
"properties": {
|
||||||
"apolloDriverConfig": {
|
"apolloDriverConfig": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"description": "The config for underlying nestjs GraphQL and apollo driver engine.\n@default {\"buildSchemaOptions\":{\"numberScalarMode\":\"integer\"},\"useGlobalPrefix\":true,\"playground\":true,\"introspection\":true,\"sortSchema\":true}\n@link https://docs.nestjs.com/graphql/quick-start",
|
"description": "The config for underlying nestjs GraphQL and apollo driver engine.\n@default {\"introspection\":true}\n@link https://docs.nestjs.com/graphql/quick-start",
|
||||||
"default": {
|
"default": {
|
||||||
"buildSchemaOptions": {
|
"introspection": true
|
||||||
"numberScalarMode": "integer"
|
|
||||||
},
|
|
||||||
"useGlobalPrefix": true,
|
|
||||||
"playground": true,
|
|
||||||
"introspection": true,
|
|
||||||
"sortSchema": true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,16 +80,13 @@
|
|||||||
"properties": {
|
"properties": {
|
||||||
"queue": {
|
"queue": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"description": "The config for job queues\n@default {\"prefix\":\"affine_job\",\"defaultJobOptions\":{\"attempts\":5,\"removeOnComplete\":true,\"removeOnFail\":{\"age\":86400,\"count\":500}}}\n@link https://api.docs.bullmq.io/interfaces/v5.QueueOptions.html",
|
"description": "The config for job queues\n@default {\"attempts\":5,\"removeOnComplete\":true,\"removeOnFail\":{\"age\":86400,\"count\":500}}\n@link https://api.docs.bullmq.io/interfaces/v5.QueueOptions.html",
|
||||||
"default": {
|
"default": {
|
||||||
"prefix": "affine_job",
|
"attempts": 5,
|
||||||
"defaultJobOptions": {
|
"removeOnComplete": true,
|
||||||
"attempts": 5,
|
"removeOnFail": {
|
||||||
"removeOnComplete": true,
|
"age": 86400,
|
||||||
"removeOnFail": {
|
"count": 500
|
||||||
"age": 86400,
|
|
||||||
"count": 500
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -58,12 +58,10 @@ test.before(async () => {
|
|||||||
ConfigModule.override({
|
ConfigModule.override({
|
||||||
job: {
|
job: {
|
||||||
worker: {
|
worker: {
|
||||||
defaultWorkerOptions: {
|
// NOTE(@forehalo):
|
||||||
// NOTE(@forehalo):
|
// bullmq will hold the connection to check stalled jobs,
|
||||||
// bullmq will hold the connection to check stalled jobs,
|
// which will keep the test process alive to timeout.
|
||||||
// which will keep the test process alive to timeout.
|
stalledInterval: 100,
|
||||||
stalledInterval: 100,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { QueueOptions, WorkerOptions } from 'bullmq';
|
import { DefaultJobOptions, WorkerOptions } from 'bullmq';
|
||||||
|
|
||||||
import { defineModuleConfig, JSONSchema } from '../../config';
|
import { defineModuleConfig, JSONSchema } from '../../config';
|
||||||
import { Queue } from './def';
|
import { Queue } from './def';
|
||||||
@@ -6,10 +6,8 @@ import { Queue } from './def';
|
|||||||
declare global {
|
declare global {
|
||||||
interface AppConfigSchema {
|
interface AppConfigSchema {
|
||||||
job: {
|
job: {
|
||||||
queue: ConfigItem<Omit<QueueOptions, 'connection' | 'telemetry'>>;
|
queue: ConfigItem<Omit<DefaultJobOptions, 'connection' | 'telemetry'>>;
|
||||||
worker: ConfigItem<{
|
worker: ConfigItem<Omit<WorkerOptions, 'connection' | 'telemetry'>>;
|
||||||
defaultWorkerOptions: Omit<WorkerOptions, 'connection' | 'telemetry'>;
|
|
||||||
}>;
|
|
||||||
queues: {
|
queues: {
|
||||||
[key in Queue]: ConfigItem<{
|
[key in Queue]: ConfigItem<{
|
||||||
concurrency: number;
|
concurrency: number;
|
||||||
@@ -30,15 +28,12 @@ defineModuleConfig('job', {
|
|||||||
queue: {
|
queue: {
|
||||||
desc: 'The config for job queues',
|
desc: 'The config for job queues',
|
||||||
default: {
|
default: {
|
||||||
prefix: env.testing ? 'affine_job_test' : 'affine_job',
|
attempts: 5,
|
||||||
defaultJobOptions: {
|
// should remove job after it's completed, because we will add a new job with the same job id
|
||||||
attempts: 5,
|
removeOnComplete: true,
|
||||||
// should remove job after it's completed, because we will add a new job with the same job id
|
removeOnFail: {
|
||||||
removeOnComplete: true,
|
age: 24 * 3600 /* 1 day */,
|
||||||
removeOnFail: {
|
count: 500,
|
||||||
age: 24 * 3600 /* 1 day */,
|
|
||||||
count: 500,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
link: 'https://api.docs.bullmq.io/interfaces/v5.QueueOptions.html',
|
link: 'https://api.docs.bullmq.io/interfaces/v5.QueueOptions.html',
|
||||||
@@ -46,9 +41,7 @@ defineModuleConfig('job', {
|
|||||||
|
|
||||||
worker: {
|
worker: {
|
||||||
desc: 'The config for job workers',
|
desc: 'The config for job workers',
|
||||||
default: {
|
default: {},
|
||||||
defaultWorkerOptions: {},
|
|
||||||
},
|
|
||||||
link: 'https://api.docs.bullmq.io/interfaces/v5.WorkerOptions.html',
|
link: 'https://api.docs.bullmq.io/interfaces/v5.WorkerOptions.html',
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { getQueueToken } from '@nestjs/bullmq';
|
import { getQueueToken, getSharedConfigToken } from '@nestjs/bullmq';
|
||||||
import { Injectable, Logger, OnModuleDestroy } from '@nestjs/common';
|
import { Injectable, Logger, OnModuleDestroy } from '@nestjs/common';
|
||||||
import { ModuleRef } from '@nestjs/core';
|
import { ModuleRef } from '@nestjs/core';
|
||||||
import { Job, Queue as Bullmq, Worker } from 'bullmq';
|
import { Job, Queue as Bullmq, Worker, WorkerOptions } from 'bullmq';
|
||||||
import { difference, merge } from 'lodash-es';
|
import { difference, merge } from 'lodash-es';
|
||||||
import { CLS_ID, ClsServiceManager } from 'nestjs-cls';
|
import { CLS_ID, ClsServiceManager } from 'nestjs-cls';
|
||||||
|
|
||||||
@@ -118,16 +118,12 @@ export class JobExecutor implements OnModuleDestroy {
|
|||||||
async job => {
|
async job => {
|
||||||
return await this.run(job.name as JobName, job.data);
|
return await this.run(job.name as JobName, job.data);
|
||||||
},
|
},
|
||||||
merge(
|
merge({}, this.config.job.queue, this.config.job.worker, queueOptions, {
|
||||||
{},
|
prefix: this.ref.get(getSharedConfigToken(), { strict: false })
|
||||||
this.config.job.queue,
|
.prefix,
|
||||||
this.config.job.worker.defaultWorkerOptions,
|
concurrency,
|
||||||
queueOptions,
|
connection: this.redis,
|
||||||
{
|
} as WorkerOptions)
|
||||||
concurrency,
|
|
||||||
connection: this.redis,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
worker.on('error', error => {
|
worker.on('error', error => {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import './config';
|
|||||||
|
|
||||||
import { BullModule } from '@nestjs/bullmq';
|
import { BullModule } from '@nestjs/bullmq';
|
||||||
import { DynamicModule } from '@nestjs/common';
|
import { DynamicModule } from '@nestjs/common';
|
||||||
|
import { type QueueOptions } from 'bullmq';
|
||||||
|
|
||||||
import { Config } from '../../config';
|
import { Config } from '../../config';
|
||||||
import { QueueRedis } from '../../redis';
|
import { QueueRedis } from '../../redis';
|
||||||
@@ -17,9 +18,19 @@ export class JobModule {
|
|||||||
module: JobModule,
|
module: JobModule,
|
||||||
imports: [
|
imports: [
|
||||||
BullModule.forRootAsync({
|
BullModule.forRootAsync({
|
||||||
useFactory: (config: Config, redis: QueueRedis) => {
|
useFactory: (config: Config, redis: QueueRedis): QueueOptions => {
|
||||||
|
let prefix = 'affine_job';
|
||||||
|
if (env.testing) {
|
||||||
|
prefix += '_test';
|
||||||
|
} else if (!env.namespaces.production) {
|
||||||
|
prefix += '_' + env.NAMESPACE;
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
...config.job.queue,
|
// NOTE(@forehalo):
|
||||||
|
// we distinguish jobs by namespace,
|
||||||
|
// to avoid new jobs been dropped by old deployments
|
||||||
|
prefix,
|
||||||
|
defaultJobOptions: config.job.queue,
|
||||||
connection: redis,
|
connection: redis,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user