simplified providers a bit
This commit is contained in:
parent
35b0eb3783
commit
4aa2942e43
9 changed files with 48 additions and 209 deletions
|
|
@ -1,16 +1,6 @@
|
|||
import { Queue, Worker, QueueEvents } from 'bullmq';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import { providerRegistry } from './provider-registry.service';
|
||||
|
||||
export interface JobData {
|
||||
type: string;
|
||||
service: string;
|
||||
provider: string;
|
||||
operation: string;
|
||||
payload: any;
|
||||
priority?: number;
|
||||
immediately?: boolean;
|
||||
}
|
||||
import { providerRegistry, JobData } from './provider-registry.service';
|
||||
|
||||
export class QueueService {
|
||||
private logger = getLogger('queue-service');
|
||||
|
|
@ -135,13 +125,11 @@ export class QueueService {
|
|||
this.logger.error('Failed to register providers', { error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
private async processJob(job: any) {
|
||||
const { service, provider, operation, payload }: JobData = job.data;
|
||||
} private async processJob(job: any) {
|
||||
const { provider, operation, payload }: JobData = job.data;
|
||||
|
||||
this.logger.info('Processing job', {
|
||||
id: job.id,
|
||||
service,
|
||||
provider,
|
||||
operation,
|
||||
payloadKeys: Object.keys(payload || {})
|
||||
|
|
@ -155,10 +143,10 @@ export class QueueService {
|
|||
}
|
||||
|
||||
// Get handler from registry
|
||||
const handler = providerRegistry.getHandler(service, provider, operation);
|
||||
const handler = providerRegistry.getHandler(provider, operation);
|
||||
|
||||
if (!handler) {
|
||||
throw new Error(`No handler found for ${service}:${provider}:${operation}`);
|
||||
throw new Error(`No handler found for ${provider}:${operation}`);
|
||||
}
|
||||
|
||||
// Execute the handler
|
||||
|
|
@ -166,7 +154,6 @@ export class QueueService {
|
|||
|
||||
this.logger.info('Job completed successfully', {
|
||||
id: job.id,
|
||||
service,
|
||||
provider,
|
||||
operation
|
||||
});
|
||||
|
|
@ -177,7 +164,6 @@ export class QueueService {
|
|||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
this.logger.error('Job failed', {
|
||||
id: job.id,
|
||||
service,
|
||||
provider,
|
||||
operation,
|
||||
error: errorMessage
|
||||
|
|
@ -220,12 +206,10 @@ export class QueueService {
|
|||
let successCount = 0;
|
||||
let failureCount = 0;
|
||||
let updatedCount = 0;
|
||||
let newCount = 0;
|
||||
|
||||
// Process each scheduled job
|
||||
for (const { service, provider, job } of allScheduledJobs) {
|
||||
let newCount = 0; // Process each scheduled job
|
||||
for (const { provider, job } of allScheduledJobs) {
|
||||
try {
|
||||
const jobKey = `${service}-${provider}-${job.operation}`;
|
||||
const jobKey = `${provider}-${job.operation}`;
|
||||
|
||||
// Check if this job already exists
|
||||
const existingJob = existingJobs.find(existing =>
|
||||
|
|
@ -257,7 +241,6 @@ export class QueueService {
|
|||
|
||||
await this.addRecurringJob({
|
||||
type: job.type,
|
||||
service: service,
|
||||
provider: provider,
|
||||
operation: job.operation,
|
||||
payload: job.payload,
|
||||
|
|
@ -267,7 +250,6 @@ export class QueueService {
|
|||
|
||||
this.logger.info('Scheduled job registered', {
|
||||
type: job.type,
|
||||
service,
|
||||
provider,
|
||||
operation: job.operation,
|
||||
cronPattern: job.cronPattern,
|
||||
|
|
@ -280,7 +262,6 @@ export class QueueService {
|
|||
} catch (error) {
|
||||
this.logger.error('Failed to register scheduled job', {
|
||||
type: job.type,
|
||||
service,
|
||||
provider,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
});
|
||||
|
|
@ -300,12 +281,12 @@ export class QueueService {
|
|||
this.logger.error('Failed to setup scheduled tasks', error);
|
||||
}
|
||||
}
|
||||
|
||||
async addJob(jobData: JobData, options?: any) {
|
||||
if (!this.isInitialized) {
|
||||
throw new Error('Queue service not initialized. Call initialize() first.');
|
||||
}
|
||||
return this.queue.add(jobData.type, jobData, {
|
||||
const jobType = jobData.type || `${jobData.provider}-${jobData.operation}`;
|
||||
return this.queue.add(jobType, jobData, {
|
||||
priority: jobData.priority || 0,
|
||||
removeOnComplete: 10,
|
||||
removeOnFail: 5,
|
||||
|
|
@ -318,9 +299,8 @@ export class QueueService {
|
|||
throw new Error('Queue service not initialized. Call initialize() first.');
|
||||
}
|
||||
|
||||
try {
|
||||
// Create a unique job key for this specific job
|
||||
const jobKey = `${jobData.service}-${jobData.provider}-${jobData.operation}`;
|
||||
try { // Create a unique job key for this specific job
|
||||
const jobKey = `${jobData.provider}-${jobData.operation}`;
|
||||
|
||||
// Get all existing repeatable jobs
|
||||
const existingJobs = await this.queue.getRepeatableJobs();
|
||||
|
|
@ -336,19 +316,18 @@ export class QueueService {
|
|||
jobKey,
|
||||
existingPattern: existingJob.pattern,
|
||||
newPattern: cronPattern
|
||||
});
|
||||
|
||||
// Remove the existing job
|
||||
await this.queue.removeRepeatableByKey(existingJob.key);
|
||||
}); // Remove the existing job
|
||||
if (existingJob.key) {
|
||||
await this.queue.removeRepeatableByKey(existingJob.key);
|
||||
}
|
||||
|
||||
// Small delay to ensure cleanup is complete
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
} else {
|
||||
this.logger.info('Creating new recurring job', { jobKey, cronPattern });
|
||||
}
|
||||
|
||||
// Add the new/updated recurring job
|
||||
const job = await this.queue.add(jobData.type, jobData, {
|
||||
} // Add the new/updated recurring job
|
||||
const jobType = jobData.type || `${jobData.provider}-${jobData.operation}`;
|
||||
const job = await this.queue.add(jobType, jobData, {
|
||||
repeat: {
|
||||
pattern: cronPattern,
|
||||
tz: 'UTC',
|
||||
|
|
@ -435,21 +414,17 @@ export class QueueService {
|
|||
}
|
||||
return this.workers.length;
|
||||
}
|
||||
|
||||
getRegisteredProviders() {
|
||||
return providerRegistry.getProviders().map(({ key, config }) => ({
|
||||
key,
|
||||
name: config.name,
|
||||
service: config.service,
|
||||
operations: Object.keys(config.operations),
|
||||
scheduledJobs: config.scheduledJobs?.length || 0
|
||||
}));
|
||||
}
|
||||
|
||||
getScheduledJobsInfo() {
|
||||
return providerRegistry.getAllScheduledJobs().map(({ service, provider, job }) => ({
|
||||
id: `${service}-${provider}-${job.type}`,
|
||||
service,
|
||||
return providerRegistry.getAllScheduledJobs().map(({ provider, job }) => ({
|
||||
id: `${provider}-${job.type}`,
|
||||
provider,
|
||||
type: job.type,
|
||||
operation: job.operation,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue