Skip to content

Instantly share code, notes, and snippets.

@rodrigolive
Last active December 16, 2015 18:49
Show Gist options
  • Save rodrigolive/5480409 to your computer and use it in GitHub Desktop.
Save rodrigolive/5480409 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use v5.10;
use MongoDB;
use Time::HiRes qw/usleep /;
use Class::Date;
use DateTime;
my $connection = MongoDB::Connection->new();
my $db = $connection->get_database("queue");
my $coll_name = 'queue';
#$db->get_collection( $coll_name )->drop;
my $cmd = Tie::IxHash->new(
"create" => $coll_name,
"capped" => boolean::true,
"size" => 10240,
"max" => 100
);
$db->run_command($cmd);
my $q = $db->get_collection('queue');
$q->insert({ k => 'topic', msg => { xx => 11, yy => 22 } });
my $rs = $q->query({ k=>'topic' })->tailable(1)->hint( { '$natural' => 1 } );
$rs->await_data( 1 );
while(1) {
while ( my $r = $rs->next ) {
say "MSG=" . $r->{msg}{xx};
if( my $err = $db->last_error->{err} ) {
die $err;
}
}
say now() . ' - outside';
}
sub now {
my @t=split /\./, Time::HiRes::time();
return sprintf "%s.%03d", Class::Date::date( $t[0]), substr $t[1], 0, 3;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment