Skip to content

Commit

Permalink
feat: allow to change job queue or setup options for it
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Oct 25, 2024
1 parent 03a2013 commit d2e0c2b
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 32 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ const redis = new IORedis({

const defineJob = initJobify(redis);

const job1 = defineJob("some")
const job1 = defineJob("some", {
queue: {
defaultJobOptions: {
delay: 100,
},
},
})
.input<{ date: string }>()
// WORKER OPTIONS
.options({
limiter: {
max: 10,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"description": "Right way to work with jobs",
"keywords": ["jobs", "queue", "bullmq", "bull", "pg-boss"],
"version": "0.0.2",
"version": "0.1.0",
"type": "module",
"devDependencies": {
"@biomejs/biome": "^1.8.3",
Expand Down
24 changes: 19 additions & 5 deletions src/define-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ import {
type JobsOptions,
type Processor,
Queue,
type QueueOptions,
type RepeatOptions,
Worker,
type WorkerOptions,
} from "bullmq";
import type { NoConnection } from "./utils";

type OptionsData = Omit<WorkerOptions, "connection">;
type OptionsData = NoConnection<WorkerOptions>;

export interface DefinedJobsOptions {
queue?: Queue | NoConnection<QueueOptions>;
}

export class Job<GlobalInput = never> {
private connection: ConnectionOptions;
Expand All @@ -18,13 +24,21 @@ export class Job<GlobalInput = never> {
worker!: Worker<GlobalInput>;
private optionsData?: OptionsData;

constructor(connection: ConnectionOptions, jobName: string) {
constructor(
connection: ConnectionOptions,
jobName: string,
options?: DefinedJobsOptions,
) {
this.connection = connection;
this.name = jobName;

this.queue = new Queue(jobName, {
connection,
});
this.queue =
options?.queue instanceof Queue
? options.queue
: new Queue(jobName, {
connection,
...options?.queue,
});
}

input<Input>(input?: Input): Job<Input> {
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { type ConnectionOptions, Queue } from "bullmq";
import type { ConnectionOptions } from "bullmq";
import { Job } from "define-job";
import type { Shift } from "./utils";

export function initJobify(connection: ConnectionOptions) {
return (name: string) => {
return new Job(connection, name);
return (...args: Shift<ConstructorParameters<typeof Job>>) => {
return new Job(connection, ...args);
};
}
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type Shift<T extends any[]> = ((...t: T) => void) extends (
h: any,
...r: infer R
) => void
? R
: never;

export type NoConnection<T> = Omit<T, "connection">;
20 changes: 20 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// TODO:
import { describe, expect, it } from "bun:test";
import { initJobify } from "index";
import IORedis from "ioredis";

const connection = new IORedis({});

describe("test", () => {
it("some", () => {
const defineJob = initJobify(connection);

const job = defineJob("ok", {
queue: {
defaultJobOptions: {
delay: 100,
},
},
});
});
});
44 changes: 22 additions & 22 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,

// Bundler mode
"moduleResolution": "bundler",
"verbatimModuleSyntax": true,
"declaration": true,
// Bundler mode
"moduleResolution": "Node",
"verbatimModuleSyntax": true,
"declaration": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,

// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false,
"baseUrl": "./src"
}
// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false,
"baseUrl": "./src"
}
}

0 comments on commit d2e0c2b

Please sign in to comment.