blob: 92f42f945e8e12fe7d272f088b21f6546ee9c9b6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# mongo-cronjob
Cron-based job scheduler which persists events to MongoDB
## Usage example
```js
const mongoose = require('mongoose');
const { Client, Scheduler } = require('mongo-cronjob');
// Define schema for event context
const contextSchema = new Schema({ message: String });
// Initialize client with existing connection
const client = new Client(mongoose.connection, contextSchema);
const EventModel = client.Event;
const LogModel = client.Log;
// Initialize scheduler
const scheduler = new Scheduler(EventModel);
scheduler.registerHandler('test', async event => {
// Your complicated async logic goes here instead
const { message } = event.context;
event.log(message);
});
EventModel.create({
type: 'test', // Matches handler type
schedule: '*/30 * * * * *', // Every 30 seconds
context: {
message: 'Hello, world!'
}
});
```
|