refactor progress
This commit is contained in:
162
packages/worker/src/providers/DirectQueueProvider.tsx
Normal file
162
packages/worker/src/providers/DirectQueueProvider.tsx
Normal file
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import type {CronScheduler} from '@fluxer/queue/src/cron/CronScheduler';
|
||||
import type {QueueEngine} from '@fluxer/queue/src/engine/QueueEngine';
|
||||
import type {JsonValue} from '@fluxer/queue/src/types/JsonTypes';
|
||||
import type {
|
||||
EnqueueOptions,
|
||||
LeasedQueueJob,
|
||||
TracingInterface,
|
||||
WorkerJobPayload,
|
||||
} from '@fluxer/worker/src/contracts/WorkerTypes';
|
||||
import type {IQueueProvider} from '@fluxer/worker/src/providers/IQueueProvider';
|
||||
|
||||
export interface DirectQueueProviderOptions {
|
||||
engine: QueueEngine;
|
||||
cronScheduler: CronScheduler;
|
||||
tracing?: TracingInterface;
|
||||
}
|
||||
|
||||
export class DirectQueueProvider implements IQueueProvider {
|
||||
private readonly engine: QueueEngine;
|
||||
private readonly cronScheduler: CronScheduler;
|
||||
private readonly tracing: TracingInterface | undefined;
|
||||
|
||||
constructor(options: DirectQueueProviderOptions) {
|
||||
this.engine = options.engine;
|
||||
this.cronScheduler = options.cronScheduler;
|
||||
this.tracing = options.tracing;
|
||||
}
|
||||
|
||||
private async withOptionalSpan<T>(
|
||||
options: {name: string; attributes?: Record<string, unknown>},
|
||||
fn: () => Promise<T>,
|
||||
): Promise<T> {
|
||||
if (this.tracing) {
|
||||
return this.tracing.withSpan(options, fn);
|
||||
}
|
||||
return fn();
|
||||
}
|
||||
|
||||
async enqueue(taskType: string, payload: WorkerJobPayload, options?: EnqueueOptions): Promise<string> {
|
||||
return this.withOptionalSpan(
|
||||
{
|
||||
name: 'queue.enqueue',
|
||||
attributes: {
|
||||
'queue.task_type': taskType,
|
||||
'queue.priority': options?.priority ?? 0,
|
||||
'queue.max_attempts': options?.maxAttempts ?? 5,
|
||||
'queue.scheduled': options?.runAt !== undefined,
|
||||
'queue.provider': 'direct',
|
||||
},
|
||||
},
|
||||
async () => {
|
||||
const runAtMs = options?.runAt ? options.runAt.getTime() : null;
|
||||
const result = await this.engine.enqueue(
|
||||
taskType,
|
||||
payload as JsonValue,
|
||||
options?.priority ?? 0,
|
||||
runAtMs,
|
||||
options?.maxAttempts ?? 5,
|
||||
null,
|
||||
);
|
||||
return result.job.id;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async dequeue(taskTypes: Array<string>, limit = 1): Promise<Array<LeasedQueueJob>> {
|
||||
return this.withOptionalSpan(
|
||||
{
|
||||
name: 'queue.dequeue',
|
||||
attributes: {
|
||||
'queue.task_types': taskTypes.join(','),
|
||||
'queue.limit': limit,
|
||||
'queue.provider': 'direct',
|
||||
},
|
||||
},
|
||||
async () => {
|
||||
const leasedJobs = await this.engine.dequeue(taskTypes, limit, 0, null);
|
||||
return leasedJobs.map(
|
||||
(leasedJob): LeasedQueueJob => ({
|
||||
receipt: leasedJob.receipt,
|
||||
visibility_deadline: new Date(leasedJob.visibilityDeadlineMs).toISOString(),
|
||||
job: {
|
||||
id: leasedJob.job.id,
|
||||
task_type: leasedJob.job.taskType,
|
||||
payload: JSON.parse(Buffer.from(leasedJob.job.payload).toString('utf-8')) as WorkerJobPayload,
|
||||
priority: leasedJob.job.priority,
|
||||
run_at: new Date(leasedJob.job.runAtMs).toISOString(),
|
||||
created_at: new Date(leasedJob.job.createdAtMs).toISOString(),
|
||||
attempts: leasedJob.job.attempts,
|
||||
max_attempts: leasedJob.job.maxAttempts,
|
||||
error: leasedJob.job.error,
|
||||
deduplication_id: leasedJob.job.deduplicationId,
|
||||
},
|
||||
}),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async upsertCron(id: string, taskType: string, payload: WorkerJobPayload, cronExpression: string): Promise<void> {
|
||||
await this.cronScheduler.upsert(id, taskType, payload as JsonValue, cronExpression);
|
||||
}
|
||||
|
||||
async complete(receipt: string): Promise<void> {
|
||||
return this.withOptionalSpan(
|
||||
{
|
||||
name: 'queue.complete',
|
||||
attributes: {
|
||||
'queue.receipt': receipt,
|
||||
'queue.provider': 'direct',
|
||||
},
|
||||
},
|
||||
async () => {
|
||||
await this.engine.ack(receipt);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async fail(receipt: string, error: string): Promise<void> {
|
||||
return this.withOptionalSpan(
|
||||
{
|
||||
name: 'queue.fail',
|
||||
attributes: {
|
||||
'queue.receipt': receipt,
|
||||
'queue.error_message': error,
|
||||
'queue.provider': 'direct',
|
||||
},
|
||||
},
|
||||
async () => {
|
||||
await this.engine.nack(receipt, error);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async cancelJob(jobId: string): Promise<boolean> {
|
||||
return this.engine.deleteJob(jobId);
|
||||
}
|
||||
|
||||
async retryDeadLetterJob(jobId: string): Promise<boolean> {
|
||||
const result = await this.engine.retryJob(jobId);
|
||||
return result !== null;
|
||||
}
|
||||
}
|
||||
261
packages/worker/src/providers/HttpWorkerQueue.tsx
Normal file
261
packages/worker/src/providers/HttpWorkerQueue.tsx
Normal file
@@ -0,0 +1,261 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import {DEFAULT_HTTP_WORKER_TIMEOUT_MS} from '@fluxer/constants/src/Timeouts';
|
||||
import type {
|
||||
EnqueueOptions,
|
||||
LeasedQueueJob,
|
||||
TracingInterface,
|
||||
WorkerJobPayload,
|
||||
} from '@fluxer/worker/src/contracts/WorkerTypes';
|
||||
import type {IQueueProvider} from '@fluxer/worker/src/providers/IQueueProvider';
|
||||
|
||||
export class HttpWorkerQueue implements IQueueProvider {
|
||||
private readonly baseUrl: string;
|
||||
private readonly timeoutMs: number;
|
||||
private readonly tracing: TracingInterface | undefined;
|
||||
|
||||
constructor(options: {baseUrl: string; timeoutMs?: number | undefined; tracing?: TracingInterface | undefined}) {
|
||||
this.baseUrl = options.baseUrl;
|
||||
this.timeoutMs = options.timeoutMs ?? DEFAULT_HTTP_WORKER_TIMEOUT_MS;
|
||||
this.tracing = options.tracing;
|
||||
}
|
||||
|
||||
private createTimeoutController(): AbortController {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), this.timeoutMs);
|
||||
|
||||
(controller as {timeoutId?: NodeJS.Timeout}).timeoutId = timeoutId;
|
||||
|
||||
return controller;
|
||||
}
|
||||
|
||||
private async fetchWithTimeout(input: string | URL, init?: RequestInit): Promise<Response> {
|
||||
const controller = this.createTimeoutController();
|
||||
|
||||
try {
|
||||
const response = await fetch(input, {
|
||||
...init,
|
||||
signal: controller.signal,
|
||||
});
|
||||
return response;
|
||||
} finally {
|
||||
const timeoutId = (controller as {timeoutId?: NodeJS.Timeout}).timeoutId;
|
||||
if (timeoutId !== undefined) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async withOptionalSpan<T>(
|
||||
options: {name: string; attributes?: Record<string, unknown>},
|
||||
fn: () => Promise<T>,
|
||||
): Promise<T> {
|
||||
if (this.tracing) {
|
||||
return this.tracing.withSpan(options, fn);
|
||||
}
|
||||
return fn();
|
||||
}
|
||||
|
||||
private addSpanEvent(name: string, attributes?: Record<string, unknown>): void {
|
||||
if (this.tracing) {
|
||||
this.tracing.addSpanEvent(name, attributes);
|
||||
}
|
||||
}
|
||||
|
||||
private setSpanAttributes(attributes: Record<string, unknown>): void {
|
||||
if (this.tracing) {
|
||||
this.tracing.setSpanAttributes(attributes);
|
||||
}
|
||||
}
|
||||
|
||||
async enqueue(taskType: string, payload: WorkerJobPayload, options?: EnqueueOptions): Promise<string> {
|
||||
return await this.withOptionalSpan(
|
||||
{
|
||||
name: 'queue.enqueue',
|
||||
attributes: {
|
||||
'queue.task_type': taskType,
|
||||
'queue.priority': options?.priority ?? 0,
|
||||
'queue.max_attempts': options?.maxAttempts ?? 5,
|
||||
'queue.scheduled': options?.runAt !== undefined,
|
||||
'net.peer.name': new URL(this.baseUrl).hostname,
|
||||
},
|
||||
},
|
||||
async () => {
|
||||
const body = {
|
||||
task_type: taskType,
|
||||
payload,
|
||||
priority: options?.priority ?? 0,
|
||||
run_at: options?.runAt?.toISOString(),
|
||||
max_attempts: options?.maxAttempts ?? 5,
|
||||
};
|
||||
|
||||
const response = await this.fetchWithTimeout(`${this.baseUrl}/enqueue`, {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
throw new Error(`Failed to enqueue job: ${response.status} ${text}`);
|
||||
}
|
||||
|
||||
const jobIdResult = (await response.json()) as {job_id: string};
|
||||
this.setSpanAttributes({'queue.job_id': jobIdResult.job_id});
|
||||
return jobIdResult.job_id;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async dequeue(taskTypes: Array<string>, limit = 1): Promise<Array<LeasedQueueJob>> {
|
||||
return await this.withOptionalSpan(
|
||||
{
|
||||
name: 'queue.dequeue',
|
||||
attributes: {
|
||||
'queue.task_types': taskTypes.join(','),
|
||||
'queue.limit': limit,
|
||||
'queue.service': 'fluxer-queue',
|
||||
},
|
||||
},
|
||||
async () => {
|
||||
this.addSpanEvent('dequeue.start');
|
||||
|
||||
const url = new URL(`${this.baseUrl}/dequeue`);
|
||||
url.searchParams.set('task_types', taskTypes.join(','));
|
||||
url.searchParams.set('limit', limit.toString());
|
||||
url.searchParams.set('wait_time_ms', '0');
|
||||
|
||||
const response = await this.fetchWithTimeout(url.toString(), {method: 'GET'});
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
throw new Error(`Failed to dequeue job: ${response.status} ${text}`);
|
||||
}
|
||||
|
||||
this.addSpanEvent('dequeue.parse_response');
|
||||
|
||||
const jobs = (await response.json()) as Array<LeasedQueueJob>;
|
||||
const jobCount = jobs?.length ?? 0;
|
||||
|
||||
this.setSpanAttributes({
|
||||
'queue.jobs_returned': jobCount,
|
||||
'queue.empty': jobCount === 0,
|
||||
});
|
||||
|
||||
this.addSpanEvent('dequeue.complete');
|
||||
|
||||
return jobs ?? [];
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async upsertCron(id: string, taskType: string, payload: WorkerJobPayload, cronExpression: string): Promise<void> {
|
||||
const response = await this.fetchWithTimeout(`${this.baseUrl}/cron`, {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({id, task_type: taskType, payload, cron_expression: cronExpression}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
throw new Error(`Failed to upsert cron job: ${response.status} ${text}`);
|
||||
}
|
||||
}
|
||||
|
||||
async complete(receipt: string): Promise<void> {
|
||||
return await this.withOptionalSpan(
|
||||
{
|
||||
name: 'queue.complete',
|
||||
attributes: {
|
||||
'queue.receipt': receipt,
|
||||
},
|
||||
},
|
||||
async () => {
|
||||
const response = await this.fetchWithTimeout(`${this.baseUrl}/ack`, {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({receipt}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
throw new Error(`Failed to complete job: ${response.status} ${text}`);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async fail(receipt: string, error: string): Promise<void> {
|
||||
return await this.withOptionalSpan(
|
||||
{
|
||||
name: 'queue.fail',
|
||||
attributes: {
|
||||
'queue.receipt': receipt,
|
||||
'queue.error_message': error,
|
||||
},
|
||||
},
|
||||
async () => {
|
||||
const response = await this.fetchWithTimeout(`${this.baseUrl}/nack`, {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({receipt, error}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
throw new Error(`Failed to fail job: ${response.status} ${text}`);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async cancelJob(jobId: string): Promise<boolean> {
|
||||
const response = await this.fetchWithTimeout(`${this.baseUrl}/job/${jobId}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
if (response.status === 404) {
|
||||
return false;
|
||||
}
|
||||
throw new Error(`Failed to cancel job: ${response.status} ${text}`);
|
||||
}
|
||||
|
||||
const result = (await response.json()) as {success: boolean};
|
||||
return result.success ?? true;
|
||||
}
|
||||
|
||||
async retryDeadLetterJob(jobId: string): Promise<boolean> {
|
||||
const response = await this.fetchWithTimeout(`${this.baseUrl}/retry/${jobId}`, {
|
||||
method: 'POST',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
if (response.status === 404) {
|
||||
return false;
|
||||
}
|
||||
throw new Error(`Failed to retry job: ${response.status} ${text}`);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
30
packages/worker/src/providers/IQueueProvider.tsx
Normal file
30
packages/worker/src/providers/IQueueProvider.tsx
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import type {EnqueueOptions, LeasedQueueJob, WorkerJobPayload} from '@fluxer/worker/src/contracts/WorkerTypes';
|
||||
|
||||
export interface IQueueProvider {
|
||||
enqueue(taskType: string, payload: WorkerJobPayload, options?: EnqueueOptions): Promise<string>;
|
||||
dequeue(taskTypes: Array<string>, limit?: number): Promise<Array<LeasedQueueJob>>;
|
||||
upsertCron(id: string, taskType: string, payload: WorkerJobPayload, cronExpression: string): Promise<void>;
|
||||
complete(receipt: string): Promise<void>;
|
||||
fail(receipt: string, error: string): Promise<void>;
|
||||
cancelJob(jobId: string): Promise<boolean>;
|
||||
retryDeadLetterJob(jobId: string): Promise<boolean>;
|
||||
}
|
||||
45
packages/worker/src/providers/QueueProviderFactory.tsx
Normal file
45
packages/worker/src/providers/QueueProviderFactory.tsx
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import type {TracingInterface} from '@fluxer/worker/src/contracts/WorkerTypes';
|
||||
import {HttpWorkerQueue} from '@fluxer/worker/src/providers/HttpWorkerQueue';
|
||||
import type {IQueueProvider} from '@fluxer/worker/src/providers/IQueueProvider';
|
||||
|
||||
export interface QueueProviderFactoryOptions {
|
||||
queueProvider?: IQueueProvider | undefined;
|
||||
queueBaseUrl?: string | undefined;
|
||||
timeoutMs?: number | undefined;
|
||||
tracing?: TracingInterface | undefined;
|
||||
}
|
||||
|
||||
export function createQueueProvider(options: QueueProviderFactoryOptions): IQueueProvider {
|
||||
if (options.queueProvider) {
|
||||
return options.queueProvider;
|
||||
}
|
||||
|
||||
if (!options.queueBaseUrl) {
|
||||
throw new Error('Queue provider requires either queueProvider or queueBaseUrl');
|
||||
}
|
||||
|
||||
return new HttpWorkerQueue({
|
||||
baseUrl: options.queueBaseUrl,
|
||||
timeoutMs: options.timeoutMs,
|
||||
tracing: options.tracing,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user