-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxt): add node-cron module (#60)
* feat(nuxt): add node-cron module * feat: add docs
- Loading branch information
1 parent
ec241a1
commit d4b8172
Showing
12 changed files
with
275 additions
and
2 deletions.
There are no files selected for viewing
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,80 @@ | ||
--- | ||
title: Installation | ||
description: 'Pergel Node Cron Module Installation' | ||
--- | ||
|
||
1. Add it to your `modules` section of `nuxt.config`: | ||
|
||
::code-group | ||
```ts [nuxt.config.ts] | ||
export default defineNuxtConfig({ | ||
modules: ['@pergel/nuxt'], | ||
pergel: { | ||
projects: { | ||
myproject: { | ||
nodeCron: true, | ||
}, | ||
// bookList: { | ||
// nodeCron: false | ||
// }, | ||
}, | ||
}, | ||
}) | ||
``` | ||
:: | ||
|
||
::callout{color="amber" icon="i-ph-warning-duotone"} | ||
Node >= 20.8.0 is required. | ||
Nuxt >= 3.9.0 is required. | ||
:: | ||
|
||
|
||
2. Auto install dependencies: | ||
|
||
::code-group | ||
```sh [pnpm] | ||
pergel install | ||
``` | ||
:: | ||
|
||
:read-more{title="Install Pergel CLI" to="/pergel/cli"} | ||
|
||
3. Create 'server/plugins/pluginName.ts' file: | ||
|
||
Note: You can change the plugin name. | ||
|
||
::code-group | ||
```ts [server/plugins/pluginName.ts] | ||
export default pergelMyproject().nodeCron().nitroPlugin({ | ||
setup: async (cron, nitroApp) => { | ||
cron.every.seconds(3, () => { | ||
console.warn('running a task every 3 seconds') | ||
}) | ||
// cron.every.minutes(5, () => { | ||
// console.warn('running a task every 5 minutes') | ||
// }) | ||
// cron.every.thirtyMinutes(() => { | ||
// console.warn('running a task every 30 minutes') | ||
// }) | ||
// cron.schedule('*/5 * * * * *', () => { | ||
// console.warn('running a task every 5 seconds') | ||
// }) | ||
}, | ||
onError: async (error: any) => { | ||
console.warn(error.message) | ||
}, | ||
config: { | ||
log: true, | ||
}, | ||
}) | ||
``` | ||
|
||
```ts [composables] | ||
pergelMyproject().nodeCron().nitroComposable({}) | ||
``` | ||
|
||
:: | ||
|
||
::callout{icon="i-ph-check-circle-duotone" color="green"} | ||
Well done! You have successfully Node Cron Module installed. | ||
:: |
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,2 @@ | ||
title: Node Cron | ||
icon: i-ph-timer |
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
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,22 @@ | ||
export default pergelTest().nodeCron().nitroPlugin({ | ||
setup: async (_cron) => { | ||
// cron.every.seconds(3, () => { | ||
// console.warn('running a task every 3 seconds') | ||
// }) | ||
// cron.every.minutes(5, () => { | ||
// console.warn('running a task every 5 minutes') | ||
// }) | ||
// cron.every.thirtyMinutes(() => { | ||
// console.warn('running a task every 30 minutes') | ||
// }) | ||
// cron.schedule('*/5 * * * * *', () => { | ||
// console.warn('running a task every 5 seconds') | ||
// }) | ||
}, | ||
onError: async (error: any) => { | ||
console.warn(error.message) | ||
}, | ||
config: { | ||
log: true, | ||
}, | ||
}) |
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
106 changes: 106 additions & 0 deletions
106
packages/nuxt/src/runtime/modules/nodeCron/composables/nitroPlugin.ts
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,106 @@ | ||
import consola from 'consola' | ||
import cron from 'node-cron' | ||
import type { getTasks, schedule, validate } from 'node-cron' | ||
import type { NitroApp } from 'nitropack' | ||
import { defineNitroPlugin } from 'nitropack/dist/runtime/plugin' | ||
|
||
interface EveryCronTime { | ||
seconds: (number: number, handleDate: (result: string) => void) => void | ||
minutes: (number: number, handleDate: (result: string) => void) => void | ||
hours: (number: number, handleDate: (result: string) => void) => void | ||
days: (number: number, handleDate: (result: string) => void) => void | ||
weeks: (number: number, handleDate: (result: string) => void) => void | ||
months: (number: number, handleDate: (result: string) => void) => void | ||
thirtyMinutes: (handleDate: (result: string) => void) => void | ||
} | ||
|
||
interface CronType { | ||
schedule: typeof schedule | ||
getTasks: typeof getTasks | ||
validate: typeof validate | ||
every: EveryCronTime | ||
} | ||
|
||
interface NodeCronConfig { | ||
setup: (cron: CronType, nitroApp: Promise<NitroApp> | NitroApp) => void | Promise<void> | ||
onError?: (err: Error | undefined) => void | Promise<void> | ||
config?: { | ||
log: boolean | ||
} | ||
} | ||
|
||
export function defineNitroPergelNodeCronPlugin( | ||
{ setup, onError, config }: NodeCronConfig, | ||
) { | ||
return defineNitroPlugin(async (_nitro) => { | ||
try { | ||
if (import.meta.prerender) { | ||
if (config?.log) | ||
consola.info('[server/plugins/node-cron.ts] Skipping node-cron, don\'t run in build context') | ||
return | ||
} | ||
|
||
if (config?.log) | ||
consola.info('[server/plugins/node-cron.ts] Starting node-cron and running once now...') | ||
|
||
await setup({ | ||
schedule: cron.schedule, | ||
getTasks: cron.getTasks, | ||
validate: cron.validate, | ||
every: { | ||
seconds: everySeconds, | ||
minutes: everyMinutes, | ||
hours: everyHours, | ||
days: everyDays, | ||
weeks: everyWeeks, | ||
months: everyMonths, | ||
thirtyMinutes: everyThirtyMinutes, | ||
|
||
}, | ||
}, _nitro) | ||
} | ||
catch (error: any) { | ||
if (onError) | ||
await onError(error) | ||
} | ||
}) | ||
} | ||
|
||
// # ┌────────────── second (optional) | ||
// # │ ┌──────────── minute | ||
// # │ │ ┌────────── hour | ||
// # │ │ │ ┌──────── day of month | ||
// # │ │ │ │ ┌────── month | ||
// # │ │ │ │ │ ┌──── day of week | ||
// # │ │ │ │ │ │ | ||
// # │ │ │ │ │ │ | ||
// # * * * * * * | ||
|
||
function everySeconds(seconds: number = 1, handleDate: (result: string) => void) { | ||
cron.schedule(`*/${seconds} * * * * *`, () => handleDate(new Date().toISOString())) | ||
} | ||
|
||
function everyMinutes(minutes: number, handleDate: (result: string) => void) { | ||
cron.schedule(`*/${minutes} * * * *`, () => handleDate(new Date().toISOString())) | ||
} | ||
|
||
function everyThirtyMinutes(handleDate: (result: string) => void) { | ||
cron.schedule(`*/30 * * * *`, () => handleDate(new Date().toISOString())) | ||
} | ||
|
||
function everyHours(hours: number, handleDate: (result: string) => void) { | ||
cron.schedule(`0 */${hours} * * *`, () => handleDate(new Date().toISOString())) | ||
} | ||
|
||
function everyDays(days: number, handleDate: (result: string) => void) { | ||
cron.schedule(`0 0 */${days} * *`, () => handleDate(new Date().toISOString())) | ||
} | ||
|
||
function everyMonths(number: number, handleDate: (result: string) => void) { | ||
cron.schedule(`*/${number} *`, () => handleDate(new Date().toISOString())) | ||
} | ||
|
||
function everyWeeks(number: number, handleDate: (result: string) => void) { | ||
const _day = 7 * number | ||
cron.schedule(`*/${_day} * *`, () => handleDate(new Date().toISOString())) | ||
} |
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,37 @@ | ||
import { addServerImportsDir, createResolver } from '@nuxt/kit' | ||
import { definePergelModule } from '../../core/definePergel' | ||
|
||
export default definePergelModule({ | ||
meta: { | ||
name: 'nodeCron', | ||
version: '0.0.1', | ||
dependencies: { | ||
'node-cron': '^3.0.3', | ||
}, | ||
devDependencies: { | ||
'@types/node-cron': '^3.0.11', | ||
}, | ||
}, | ||
defaults: {}, | ||
async setup(options) { | ||
const resolver = createResolver(import.meta.url) | ||
const projectName = options.resolvedModule.projectName | ||
|
||
addServerImportsDir(resolver.resolve('./composables')) | ||
|
||
options._contents.push({ | ||
moduleName: 'nodeCron', | ||
projectName, | ||
content: /* ts */` | ||
function nodeCron() { | ||
return { | ||
nitroPlugin: defineNitroPergelNodeCronPlugin | ||
} | ||
} | ||
`, | ||
resolve: /* ts */` | ||
nodeCron: nodeCron, | ||
`, | ||
}) | ||
}, | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.