Last active
February 21, 2022 08:04
-
-
Save Romakita/86b75d53d1ebe0e059aa248bf263f42e to your computer and use it in GitHub Desktop.
Using bull with Ts.ED
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
import {createInjector, InjectorService, Provider} from "@tsed/common"; | |
import {Store} from "@tsed/core"; | |
export class BullBuilder { | |
readonly injector: InjectorService; | |
constructor(settings: any) { | |
this.injector = createInjector({ | |
settings, | |
providers: [] | |
}); | |
} | |
static async bootstrap(settings: any) { | |
const builder = new BullBuilder(settings); | |
await builder.injector.load(); | |
return builder; | |
} | |
getJobs() { | |
return this.injector.getProviders("job:service").reduce((map: Map<string, any>, provider: Provider) => { | |
const jobs = provider.store.get("jobs"); | |
Object.entries(jobs).forEach(([jobName, propertyKey]: [string, string]) => { | |
map.set(jobName, (job: Job<any>) => { | |
const service: any = this.injector.get(provider.token); | |
return service[propertyKey](job); | |
}); | |
}); | |
return map; | |
}, new Map()); | |
} | |
runJob(job: Job<any>) { | |
const jobs = this.getJobs(); | |
const handler = jobs.get(job.data.jobName); | |
if (job) { | |
this.injector.logger.info(`Handle ${job.data.jobName}`); | |
return handler(job); | |
} | |
this.injector.logger.warning(`No job found for ${job.data.jobName}`); | |
// do somethings | |
} | |
} |
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
import {BullBuilder} from "./BullBuilder" | |
import {configuration} from "./path/to/your/config"; // share the Ts.ED server configuration with this platform | |
import "./jobs/MyJobService"; // import manually all job services in this files | |
export default async function boostrap(job: Job<any>) { | |
const platform = await BullBuilder.bootstrap(configuration); | |
return platform.runJob(job); | |
} |
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
/** | |
* Register a method to a jobName | |
* @param name | |
* @constructor | |
*/ | |
export function Job(name: string): MethodDecorator { | |
return (target, propertyKey, descriptor) => { | |
Store.from(target).merge("jobs", { | |
[name]: propertyKey | |
}); | |
}; | |
} |
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
export function JobService () { | |
return Injectable({type: "job:service"}) | |
} |
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
@JobService() | |
class MyJobService { | |
@Job("jobname") | |
myJobHandler(job) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment