File Coverage

blib/lib/TheSchwartz/Worker/PubSubHubbubPublish.pm
Criterion Covered Total %
statement 12 49 24.4
branch 0 12 0.0
condition 0 6 0.0
subroutine 4 11 36.3
pod 0 6 0.0
total 16 84 19.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             TheSchwartz::Worker::PubSubHubbubPublish - ping pubsubhubbub hub servers
4              
5             =head1 SYNOPSIS
6              
7             use TheSchwartz;
8             use TheSchwartz::Worker::PubSubHubbubPublish;
9             my $sclient = TheSchwartz->new(databases => \@Conf::YOUR_DBS);
10             $sclient->can_do("TheSchwartz::Worker::PubSubHubbubPublish");
11             $sclient->work; # main loop of program; goes forever, pinging as needed
12              
13             =head1 DESCRIPTION
14              
15             This is a worker class for sending pings to PubSubHubbub hub servers.
16             See L and L for more
17             information.
18              
19             =head1 JOB ARGUMENTS
20              
21             When constructing a job using L's insert_job
22             method, construct your L instance with its
23             'argument' of the following form:
24              
25             {
26             hub => $hub_url, # the hub's endpoint URL
27             topic_url => $url, # Atom URL that was updated
28             }
29              
30             Also, if you set your L's C property to be
31             the hub URL, this worker will do batch pings instead, vastly reducing
32             the number of HTTP requests it does.
33              
34             =cut
35              
36             package TheSchwartz::Worker::PubSubHubbubPublish;
37 1     1   27824 use strict;
  1         2  
  1         44  
38 1     1   5 use base 'TheSchwartz::Worker';
  1         2  
  1         707  
39 1     1   1179 use Storable;
  1         5855  
  1         103  
40 1     1   959 use Net::PubSubHubbub::Publisher 0.91;
  1         81512  
  1         2088  
41              
42             our $VERSION = '1.00';
43              
44             our $MAX_BATCH_SIZE = 50;
45              
46             my $keep_exit_status_for = 0;
47 0     0 0   sub set_keep_exit_status_for { $keep_exit_status_for = shift; }
48              
49             my %publisher; # $hub -> Net::PubSubHubbub::Publisher
50              
51             sub work {
52 0     0 0   my ($class, $job) = @_;
53 0           my $client = $job->handle->client;
54 0           my $hub = $job->arg->{hub};
55 0 0 0       unless ($hub && $hub =~ m!^https?://\S+$!) {
56 0           $job->permanent_failure("Bogus hub $hub. Ignoring job.");
57 0           return;
58             }
59              
60 0           my @jobs;
61             my @topics;
62              
63             my $add_job = sub {
64 0     0     my $j = shift;
65 0           my $args = $j->arg;
66 0 0         unless ($args->{hub} eq $hub) {
67             # Each job must share the same hub.
68 0           warn "WARNING: coalesced job had different hub in its args. Skipping.";
69 0           return;
70             }
71              
72 0           push @jobs, $j;
73 0           push @topics, $args->{topic_url};
74 0           };
75 0           $add_job->($job);
76              
77 0   0       my $publisher = $publisher{$hub} ||=
78             Net::PubSubHubbub::Publisher->new(hub => $hub);
79              
80 0           while (@topics < $MAX_BATCH_SIZE) {
81 0           my $j = $client->find_job_with_coalescing_value(__PACKAGE__, $hub);
82 0 0         last unless $j;
83 0           $add_job->($j);
84             }
85              
86 0 0         if ($publisher->publish_update(@topics)) {
87 0           warn "Pinged $hub about topic(s): @topics.\n";
88 0           foreach my $j (@jobs) {
89 0           $j->completed;
90             }
91 0           return;
92             }
93              
94 0           my $failure_reason = $publisher->last_response->status_line;
95 0           warn "Failed to ping $hub about @topics: $failure_reason\n";
96 0           $job->failed($failure_reason);
97             }
98              
99             sub keep_exit_status_for {
100 0 0   0 0   return 0 unless $keep_exit_status_for;
101 0 0         return $keep_exit_status_for->() if ref $keep_exit_status_for eq "CODE";
102 0           return $keep_exit_status_for;
103             }
104              
105 0     0 0   sub grab_for { 30 }
106 0     0 0   sub max_retries { 10 }
107             sub retry_delay {
108 0     0 0   my ($class, $fails) = @_;
109 0           return 30 * $fails;
110             }
111              
112             =head1 AUTHOR
113              
114             Brad Fitzpatrick -- brad@danga.com
115              
116             =head1 COPYRIGHT, LICENSE, and WARRANTY
117              
118             Copyright 2009, Brad Fitzpatrick.
119              
120             License to use under the same terms as Perl itself.
121              
122             This software comes with no warranty of any kind.
123              
124             =head1 SEE ALSO
125              
126             L
127              
128             L
129              
130             =cut
131              
132             1;