pg_job_queue 0.0.2 copy "pg_job_queue: ^0.0.2" to clipboard
pg_job_queue: ^0.0.2 copied to clipboard

A simple job queue on top of PostgreSQL. Supports parallel workers, job priorities, named queues, JSON payloads.

example/example.dart

import 'package:pg_job_queue/pg_job_queue.dart';
import 'package:postgres/postgres.dart';

void main() async {
  /// To run locally, start postgres:
  /// `docker run -p 5432:5432 -e POSTGRES_PASSWORD=postgres postgres`
  final connection = await Connection.open(
      Endpoint(
        host: 'localhost',
        database: 'postgres',
        username: 'postgres',
        password: 'postgres',
      ),
      settings: ConnectionSettings(sslMode: SslMode.disable));

  /// Create a new job queue with default settings.
  final jobs = PgJobQueue(connection);

  /// Initialize the DB tables. This is idempotent.
  await jobs.init();

  /// Schedule some jobs.
  for (var i = 0; i < 10; i++) {
    await jobs.schedule({'name': 'Job $i'});
  }

  await Future.wait(['Alice', 'Bob', 'Chris'].map((worker) async {
    print('Worker $worker started');
    while (true) {
      final job = await jobs.acquire(worker: worker);
      if (job == null) {
        print('All done. $worker shutting down');
        break;
      }
      print('Worker $worker processing job ${job.id} (${job.payload})');
      await jobs.complete(job.id);
    }
  }));
  await connection.close();
}
1
likes
160
points
46
downloads

Publisher

verified publisherkarapetov.com

Weekly Downloads

A simple job queue on top of PostgreSQL. Supports parallel workers, job priorities, named queues, JSON payloads.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

collection, migrant, migrant_db_postgresql, postgres, uuid

More

Packages that depend on pg_job_queue