khepri 0.1.2
khepri: ^0.1.2 copied to clipboard
Laravel-style queues, jobs and workers for Maat applications.
import 'package:khepri/khepri.dart';
/// A job carries its own state as JSON and is rebuilt by [fromPayload].
class SendWelcomeEmail extends Job {
SendWelcomeEmail(this.userId);
static SendWelcomeEmail fromPayload(Map<String, Object?> payload) =>
SendWelcomeEmail(payload['user_id'] as int);
final int userId;
@override
Map<String, Object?> toPayload() => {'user_id': userId};
@override
int get tries => 3;
@override
List<Duration> get backoff => const [
Duration(seconds: 10),
Duration(minutes: 1),
];
@override
Future<void> handle() async {
// Send the mail. Throwing releases the job for another attempt.
}
}
/// Registered once, where the application boots.
void registerJobs(JobRegistry jobs) {
jobs.register<SendWelcomeEmail>(
'send_welcome_email',
SendWelcomeEmail.fromPayload,
);
}
/// With `QueueServiceProvider` installed, dispatching is one call.
Future<void> welcome(int userId) => dispatch(SendWelcomeEmail(userId));