| 1 | package Exobrain::Agent::Poll; | ||
| 2 | use Moose::Role; | ||
| 3 | use Method::Signatures; | ||
| 4 | use Try::Tiny; | ||
| 5 | use Exobrain::Types qw(TimeOut PosInt); | ||
| 6 | |||
| 7 | # ABSTRACT: Role for exobrain sources which poll a source. | ||
| 8 | our $VERSION = '1.08'; # VERSION | ||
| 9 | |||
| 10 | |||
| 11 | with 'Exobrain::Agent'; | ||
| 12 | |||
| 13 | requires('poll'); | ||
| 14 | excludes('run'); | ||
| 15 | |||
| 16 | has frequency => (isa => TimeOut, is => 'rw', default => 90 ); | ||
| 17 | has max_errors => (isa => PosInt, is => 'rw', default => 5 ); | ||
| 18 | |||
| 19 | |||
| 20 | method start() { | ||
| 21 | my $errors = 0; | ||
| 22 | |||
| 23 | while (1) { | ||
| 24 | try { | ||
| 25 | $self->poll; | ||
| 26 | sleep( $self->frequency ); | ||
| 27 | $errors = 0; | ||
| 28 | } | ||
| 29 | catch { | ||
| 30 | warn $_; | ||
| 31 | $errors++; | ||
| 32 | |||
| 33 | # If we see too many continuous errors, bail out! | ||
| 34 | if ($errors > $self->max_errors) { | ||
| 35 | my $class = ref($self); | ||
| 36 | my $message = "Too many contiguous errors for $class. Bailing out."; | ||
| 37 | try { $self->exobrain->notify($message, priority => 1); }; | ||
| 38 | |||
| 39 | die $message; | ||
| 40 | } | ||
| 41 | }; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | 1; | ||
| 46 | |||
| 47 | __END__ |