AnyEvent-SparkBot
view release on metacpan or search on metacpan
lib/AnyEvent/HTTP/Spark.pm view on Meta::CPAN
logger: sets the logging object
agent: AnyEvent::HTTP::MultiGet object
api_url: https://webexapis.com/v1/
# sets the web service the requests point to
retryCount: 1, how many retries to attempt when getting a 429 error
Options set at runtime
retries: anymous hash, used to trak AnyEvent->timer objects
=cut
# This method runs after the new constructor
sub BUILD {
my ($self)=@_;
}
# this method runs before the new constructor, and can be used to change the arguments passed to the module
around BUILDARGS => sub {
lib/AnyEvent/HTTP/Spark.pm view on Meta::CPAN
};
} else {
$wrap=sub {
my ($self,$id,$result,undef,$response)=@_;
return $cb->(@_) if $result || !(($response->code==429 || $response->code==404) && $count-- >0);
my $timeout=looks_like_number($response->header('Retry-After')) ? $response->header('Retry-After') : $self->retryTimeout;
$self->log_warn("Request: $id recived a 429 response, will retry in $timeout seconds");
if($count>0) {
my $ae;
$ae=AnyEvent->timer(after=>$timeout,cb=>sub {
my $next_id=$self->queue_request($request,sub {
my ($self,undef,$result,undef,$response)=@_;
$wrap->($self,$id,$result,$request,$response);
});
$self->add_ids_for_blocking($next_id);
$self->agent->run_next;
delete $self->retries->{$ae};
undef $ae;
});
return $self->retries->{$ae}=$ae;
}
my $code=sub {
my ($self,undef,$result,undef,$response)=@_;
$cb->($self,$id,$result,$request,$response);
};
my $ae;
$ae=AnyEvent->timer(after=>$timeout,cb=>sub {
my $next_id=$self->queue_request($request,$code);
$self->add_ids_for_blocking($next_id);
$self->agent->run_next;
delete $self->retries->{$ae};
undef $ae;
});
return $self->retries->{$ae}=$ae;
};
}
lib/AnyEvent/SparkBot.pm view on Meta::CPAN
Handles reconnecting to spark
=cut
sub handle_reconnect : BENCHMARK_INFO {
my ($self)=@_;
$self->ping(undef);
$self->connection->close if $self->connection;
my $ping=AnyEvent->timer(after=>$self->reconnect_sleep,cb=>sub {
$self->que_getWsUrl(sub { $self->start_connection });
$self->agent->run_next;
});
$self->ping($ping);
}
=item * $self->setPing()
Sets the next ping object
=cut
sub setPing {
my ($self)=@_;
$self->ping(undef);
my $ping=AnyEvent->timer(after=>$self->pingEvery,cb=>sub {
unless($self->connection) {
$self->ping(undef);
$self->log_error('current conenction is not valid?');
return;
}
my $id=$self->uuidv4;
$self->lastPing($id);
$self->connection->send(to_json({ type=>'ping', id=> $id, }));
$self->setPingWait;
lib/AnyEvent/SparkBot.pm view on Meta::CPAN
=item * $self->setPingWait()
This method is called by ping, sets a timeout to wait for the response.
=cut
sub setPingWait {
my ($self)=@_;
$self->ping(undef);
my $wait=AnyEvent->timer(after=>$self->pingWait,cb=>sub {
$self->ping(undef);
$self->handle_reconnect;
});
$self->ping($wait);
}
=item * my $result=$self->getLastConn()
Fetches the last connection info
t/AnyEvent-HTTP-Spark-nonBLock.t view on Meta::CPAN
},{roomId=>$room->{id},text=>'this is a test'});
},{teamId=>$team->{id}});
},{teamId=>$team->{id},title=>$testRoom});
});
},{name=>$teamName});
});
my $max=AnyEvent->timer(after=>300,cb=>sub { no warnings;last SKIP });
AnyEvent::Loop::run;
}
ok(!$final,'should have cleared final');
t/AnyEvent-SparkBot.t view on Meta::CPAN
SKIP: {
skip "$result", 1 unless $result;
my $ws=$result->get_data->{webSocketUrl};
ok($ws,'should have a websocket string');
$self->start_connection($ws);
my $t;
LOOP_TEST: {
$t=AnyEvent->timer(after=>4,cb=>sub { no warnings;last LOOP_TEST });
AnyEvent::Loop::run;
}
undef $t;
ok($self->connection,'should break out of the listener loop without an ussue');
$self->connection->close;
$self=$class->new(reconnect_sleep=>0,pingEvery=>1,logger=>$logger,token=>$token,on_message=>\&on_message);
$self->handle_reconnect;
LOOP_TEST: {
$t=AnyEvent->timer(after=>10,cb=>sub { no warnings;last LOOP_TEST });
AnyEvent::Loop::run;
}
undef $t;
ok($self->connection,'should break out of the listener loop without an ussue');
$self->connection->close;
}
}
sub on_message {
my ($self,$json,$message)=@_;
( run in 1.018 second using v1.01-cache-2.11-cpan-49f99fa48dc )