Ian Kent > MangoX-Queue-0.10 > MangoX::Queue::Tutorial

Download:
MangoX-Queue-0.10.tar.gz

Annotate this POD

CPAN RT

Open  0
View/Report Bugs
Source   Latest Release: MangoX-Queue-0.15

NAME ^

MangoX::Queue::Tutorial

QUICK START ^

Non-blocking mode using Mojo::IOLoop

  1. Get a Mango collection
        my $mango = Mango->new('mongodb://localhost:27017');
        my $collection = $mango->db('foo')->collection('bar');
  2. Create a MangoX::Queue
        my $queue = MangoX::Queue->new(collection => $collection);
  3. Add a job to the queue
        enqueue $queue 'job_name' => sub {
            my ($job_id) = @_;
            # ...
        }
  4. Consume the queue to receive jobs
        my $consumer = consume $queue sub {
            my ($job) = @_;
            # ...
        };
  5. Stop consuming the queue
        release $queue $consumer;
  6. Update the job status in the queue
        $job->{status} = 'Finished';
        update $queue $job => sub {
            my ($job) = @_;
            # ...
        };
  7. Remove the job from the queue
        dequeue $queue $job => sub {
            # ...
        };

Blocking mode

  1. Get a Mango collection
        my $mango = Mango->new('mongodb://localhost:27017');
        my $collection = $mango->db('foo')->collection('bar');
  2. Create a MangoX::Queue
        my $queue = MangoX::Queue->new(collection => $collection);
  3. Add a job to the queue
        my $job_id = enqueue $queue 'job_name';
  4. Consume the queue to receive jobs
        while (my $job = consume $queue) {
            # ...
        };
  5. Update the job status in the queue
        $job->{status} = 'Finished';
        my $job = update $queue $job;
  6. Remove the job from the queue
        dequeue $queue $job;
syntax highlighting: