generated from AthennaIO/Template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from AthennaIO/develop
feat(cron): add cron application
- Loading branch information
Showing
11 changed files
with
328 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@athenna/core", | ||
"version": "4.45.0", | ||
"version": "4.46.0", | ||
"description": "One foundation for multiple applications.", | ||
"license": "MIT", | ||
"author": "João Lenon <[email protected]>", | ||
|
@@ -15,6 +15,9 @@ | |
"rest-api", | ||
"http-server", | ||
"console", | ||
"cron", | ||
"cronjob", | ||
"scheduler", | ||
"ignite", | ||
"nodejs", | ||
"athenna", | ||
|
@@ -80,6 +83,7 @@ | |
"@athenna/artisan": "^4.45.0", | ||
"@athenna/common": "^4.46.0", | ||
"@athenna/config": "^4.27.0", | ||
"@athenna/cron": "^4.1.0", | ||
"@athenna/http": "^4.41.0", | ||
"@athenna/ioc": "^4.27.0", | ||
"@athenna/logger": "^4.29.0", | ||
|
@@ -218,6 +222,9 @@ | |
"directories": { | ||
"bootstrap": "bin" | ||
}, | ||
"schedulers": [ | ||
"#tests/fixtures/schedulers/HelloScheduler" | ||
], | ||
"commands": { | ||
"make:exception": { | ||
"path": "#src/commands/MakeExceptionCommand" | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* @athenna/core | ||
* | ||
* (c) João Lenon <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { debug } from '#src/debug' | ||
import { Log } from '@athenna/logger' | ||
import type { CronImpl } from '@athenna/cron' | ||
import { Path, Module, Options } from '@athenna/common' | ||
import type { CronOptions } from '#src/types/CronOptions' | ||
|
||
export class Cron { | ||
/** | ||
* Boot the Cron application. | ||
*/ | ||
public static async boot(options?: CronOptions): Promise<CronImpl> { | ||
options = Options.create(options, { | ||
trace: Config.get('cron.trace', false), | ||
routePath: Path.routes(`cron.${Path.ext()}`), | ||
kernelPath: '@athenna/cron/kernels/CronKernel' | ||
}) | ||
|
||
const cron = ioc.safeUse('Athenna/Core/Cron') | ||
|
||
debug('booting cron application with options %o', options) | ||
|
||
await this.resolveKernel(options) | ||
|
||
if (Config.notExists('rc.bootLogs') || Config.is('rc.bootLogs', false)) { | ||
return cron | ||
} | ||
|
||
Log.channelOrVanilla('application').success( | ||
`Cron application successfully started` | ||
) | ||
|
||
return cron | ||
} | ||
|
||
/** | ||
* Resolve the kernel by importing it and calling the methods to register | ||
* schedulers, plugins and exception handler. | ||
*/ | ||
private static async resolveKernel(options?: CronOptions) { | ||
const Kernel = await Module.resolve( | ||
options.kernelPath, | ||
Config.get('rc.parentURL') | ||
) | ||
|
||
const kernel = new Kernel() | ||
|
||
await kernel.registerRTracer(options.trace) | ||
await kernel.registerExceptionHandler(options.exceptionHandlerPath) | ||
await kernel.registerSchedulers() | ||
await kernel.registerRoutes(options.routePath) | ||
|
||
if (Config.is('rc.bootLogs', true)) { | ||
Log.channelOrVanilla('application').success( | ||
`Kernel ({yellow} ${Kernel.name}) successfully booted` | ||
) | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* @athenna/core | ||
* | ||
* (c) João Lenon <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
export type CronOptions = { | ||
/** | ||
* Create a custom trace uuid to all new requests done to the server using AsyncLocalStorage. | ||
* | ||
* @default Config.get('cron.trace', false) | ||
*/ | ||
trace?: boolean | ||
|
||
/** | ||
* The path to the cron routes. | ||
* | ||
* @default Path.routes(`cron.${Path.ext()}`) | ||
*/ | ||
routePath?: string | ||
|
||
/** | ||
* The path to the CronKernel. The cron kernel is responsible to register controllers, | ||
* all kind of middlewares, plugins and the exception handler. By default, | ||
* Athenna will use the built in Kernel. But you can do your own implementation | ||
* extending the "CronKernel" class from Http and setting the path to it here. | ||
* | ||
* @default '@athenna/cron/kernels/CronKernel' | ||
*/ | ||
kernelPath?: string | ||
|
||
/** | ||
* The path to the exception handler of cron schedulers. The exception | ||
* handler is responsible to handle all the exception that are throwed | ||
* inside route handlers. By default, Athenna will use the built in exception | ||
* handler. But you can do your own implementation extending the | ||
* "CronExceptionHandler" class from Http and setting the path to it here. | ||
* | ||
* @default '@athenna/cron/handlers/CronExceptionHandler' | ||
*/ | ||
exceptionHandlerPath?: string | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @athenna/core | ||
* | ||
* (c) João Lenon <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { Log } from '@athenna/logger' | ||
import { ConsoleExceptionHandler } from '@athenna/artisan' | ||
|
||
Log.info('importing CustomCronExceptionHandler') | ||
|
||
export class CustomCronExceptionHandler extends ConsoleExceptionHandler {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @athenna/core | ||
* | ||
* (c) João Lenon <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { Log } from '@athenna/logger' | ||
import { CronKernel } from '@athenna/cron' | ||
|
||
Log.info('importing CustomCronKernel') | ||
|
||
export class CustomCronKernel extends CronKernel {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* @athenna/core | ||
* | ||
* (c) João Lenon <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { Cron } from '@athenna/cron' | ||
|
||
Cron.schedule() | ||
.pattern('* * * * *') | ||
.handler(() => {}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* @athenna/core | ||
* | ||
* (c) João Lenon <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { Scheduler, type Context } from '@athenna/cron' | ||
|
||
@Scheduler({ pattern: '* * * * *' }) | ||
export class HelloScheduler { | ||
public async handle(ctx: Context) { | ||
console.log(ctx) | ||
} | ||
} |
Oops, something went wrong.