|
| 1 | +/** |
| 2 | + * @athenna/core |
| 3 | + * |
| 4 | + * (c) João Lenon <lenon@athenna.io> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +import { debug } from '#src/debug' |
| 11 | +import { Log } from '@athenna/logger' |
| 12 | +import type { WorkerImpl } from '@athenna/queue' |
| 13 | +import { Path, Module, Options } from '@athenna/common' |
| 14 | +import type { CronOptions } from '#src/types/CronOptions' |
| 15 | + |
| 16 | +export class Worker { |
| 17 | + /** |
| 18 | + * Boot the Worker application. |
| 19 | + */ |
| 20 | + public static async boot(options?: CronOptions): Promise<WorkerImpl> { |
| 21 | + options = Options.create(options, { |
| 22 | + routePath: Config.get( |
| 23 | + 'rc.worker.route', |
| 24 | + Path.routes(`worker.${Path.ext()}`) |
| 25 | + ), |
| 26 | + kernelPath: Config.get( |
| 27 | + 'rc.worker.kernel', |
| 28 | + '@athenna/queue/kernels/WorkerKernel' |
| 29 | + ) |
| 30 | + }) |
| 31 | + |
| 32 | + const worker = ioc.safeUse('Athenna/Core/Worker') |
| 33 | + |
| 34 | + debug('booting worker application with options %o', options) |
| 35 | + |
| 36 | + await this.resolveKernel(options) |
| 37 | + |
| 38 | + if (Config.notExists('rc.bootLogs') || Config.is('rc.bootLogs', false)) { |
| 39 | + return worker |
| 40 | + } |
| 41 | + |
| 42 | + Log.channelOrVanilla('application').success( |
| 43 | + `Worker application successfully started` |
| 44 | + ) |
| 45 | + |
| 46 | + return worker |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Resolve the kernel by importing it and calling the methods to register |
| 51 | + * worker tasks and plugins. |
| 52 | + */ |
| 53 | + private static async resolveKernel(options?: CronOptions) { |
| 54 | + const Kernel = await Module.resolve( |
| 55 | + options.kernelPath, |
| 56 | + Config.get('rc.parentURL') |
| 57 | + ) |
| 58 | + |
| 59 | + const kernel = new Kernel() |
| 60 | + |
| 61 | + await kernel.registerLogger() |
| 62 | + await kernel.registerRTracer() |
| 63 | + await kernel.registerWorkers() |
| 64 | + await kernel.registerRoutes(options.routePath) |
| 65 | + |
| 66 | + if (Config.is('rc.bootLogs', true)) { |
| 67 | + Log.channelOrVanilla('application').success( |
| 68 | + `Kernel ({yellow} ${Kernel.name}) successfully booted` |
| 69 | + ) |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments