Skip to content

dinko7/mongo_cron

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MongoCron

A MongoDB-based cron job scheduler for Dart.

Features

  • Schedule jobs using cron syntax (use Cronitor if you're not familiar with cron syntax)
  • Persist jobs in MongoDB
  • Callbacks for various cron events (start, stop, idle, error)

Processing speed

Processing speed can be reduced when more and more documents are added into the collection. We can maintain the speed by creating the following indexes:

await _collection.createIndex(key: 'name', unique: true);
await _collection.createIndex(keys: {'sleepUntil': 1, 'name': 1});

Usage

Take a look at the example for a complete example.

import 'package:mongo_cron/mongo_cron.dart';
import 'package:mongo_dart/mongo_dart.dart';

void main() async {
  final db = await Db.create('mongodb://localhost:27017/your_database');
  await db.open();

  final config = MongoCronConfig(
    database: db,
    onStart: (MongoCron cron) async {
      print('MongoCron started');
    },
    onStop: (MongoCron cron) async {
      print('MongoCron stopped');
    },
    onIdle: (MongoCron cron) async {
      print('MongoCron is idle');
    },
    onError: (dynamic error, MongoCron cron) async {
      print('Error occurred: $error');
      if (!db.isConnected) {
        print('MongoDB disconnected—reconnecting...');
        await db.close();
        await db.open();
        print('MongoDB reconnected');
      }
    },
    onJobStart: (Job job) async {
      print(
          '${DateTime.now().toIso8601String()} Job started: ${job.name} #${job.id}');
    },
    onJobComplete: (Job job) async {
      print(
          '${DateTime.now().toIso8601String()} Job complete: ${job.name} #${job.id}');
    },
  );

  final mongoCron = MongoCron(config);

  // Add a job
  await mongoCron.addJob(
    cronExpression: '*/5 * * * *',
    name: 'job1',
    handler: (Job job) async {
      print('Executing job ${job.id} with data: ${job.data}');
    },
    jobData: {'message': 'Hello, World!'},
  );

  await mongoCron.start();

  // You can add more jobs while it's running
  await mongoCron.addJob(
    cronExpression: '* * * * *',
    name: 'job2',
    handler: (Job job) async {
      print('Second job');
    },
  );

  // Run for while
  await Future.delayed(const Duration(seconds: 30));

  // Stop the cron
  await mongoCron.stop();
  await db.close();
}

About

Use MongoDB collection as a job queue for cron jobs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages