AnyEvent-SparkBot
view release on metacpan or search on metacpan
lib/AnyEvent/SparkBot.pm view on Meta::CPAN
my $args={
personId=>$self->currentUser->{id},
};
$self->run_lookup('que_listMemberships',sub {
my ($agent,$id,$result,$req,$resp)=@_;
$self->on_message->($self,$result,$eventType,$verb,$json,$req,$resp,$message);
},$args);
} elsif($verb=~ /lock|unlock|update/) {
$self->run_lookup('que_getRoom',sub {
my ($agent,$id,$result,$req,$resp)=@_;
$self->on_message->($self,$result,$eventType,$verb,$json,$req,$resp,$message);
},$activity->{object}->{id});
} else {
$self->on_message->($self,$self->new_false("Unsupported EventType: [$eventType] and Verb: [$verb]"),$eventType,$verb,$json);
}
} else {
$self->on_message->($self,$self->new_false("Unsupported EventType: [$eventType] and Verb: [$verb]"),$eventType,$verb,$json);
}
} else {
my $eventType=defined($json->{data}->{eventType}) ? $json->{data}->{eventType} : 'unknown';
my $verb=defined($json->{data}->{activity}->{verb}) ? $json->{data}->{activity}->{verb} : 'unknown';
$self->on_message->($self,$self->new_false("Unsupported EventType: [$eventType] and Verb: [$verb]"),$eventType,'unknown',$json);
}
} else {
my $eventType=defined($json->{data}->{eventType}) ? $json->{data}->{eventType} : 'unknown';
my $verb=defined($json->{data}->{activity}->{verb}) ? $json->{data}->{activity}->{verb} : 'unknown';
$self->on_message->($self,$self->new_false("Unsupported EventType: [$eventType] and Verb: [$verb]"),$eventType,$verb,$json);
}
}
$self->setPing();
}
=item * $self->run_lookup($method,$cb,@args);
Shortcut for:
$self->spark->$method($cb,@args);
$self->agent->run_next;
=cut
sub run_lookup {
my ($self,$method,$cb,@args)=@_;
$self->spark->$method($cb,@args);
$self->agent->run_next;
}
=item * $self->handle_reconnect()
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;
});
$self->ping($ping);
}
=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
Returns a Data::Result Object, when true it contains the hash, when false it contains why it failed.
=cut
sub getLastConn : BENCHMARK_DEBUG {
my ($self)=@_;
my $lc=$self->lastConn;
if(-r $lc) {
my $fh=IO::File->new($lc,'r');
return $self->new_false("Could not open file: $lc, error was: $!") unless $fh;
my $str=join '',$fh->getlines;
$fh->close;
my $json=eval { from_json($str) };
if($@) {
return $self->new_false("Could not parse $lc, error was: $@");
}
return $self->new_true($json);
}
return $self->new_false("Could not read $lc");
}
=item * my $result=$self->saveLastConn($ref)
Saves the last conenction, returns a Data::Result Object
$ref is assumed to be the data strucutre intended to be serialzied into json
=cut
sub saveLastConn : BENCHMARK_DEBUG {
my ($self,$ref)=@_;
my $json=to_json($ref,{pretty=>1});
my $fh=IO::File->new($self->lastConn,'w');
return $self->new_false("Failed to create: [".$self->lastConn."] error was: [$!]") unless $fh;
$fh->print($json);
return $self->new_true($json);
}
=item * my $job_id=$self->que_deleteLastUrl($cb)
Returns a Data::Result Object, when true it contains the url that was deleted, when false it contains why it failed.
( run in 0.598 second using v1.01-cache-2.11-cpan-677af5a14d3 )