--- /dev/null
+#!/usr/bin/perl
+#
+# Copyright (c) 2012 ISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
+# This program is covered by the GNU General Public License 2
+#
+package Mubot4FB::FeedWorker;
+use strict;
+use utf8;
+
+use base 'Mubot4FB::Common';
+
+use Net::PubSubHubbub::Publisher;
+use JSON::XS;
+use Mubot4FB::DB;
+use File::Temp qw/tempfile/;
+
+use constant DATA_VERSION => '0.1';
+
+use Data::Dumper;
+
+sub init {
+ my ($me) = @_;
+ $me->{db} = Mubot4FB::DB->new($me->{cfg});
+ return 1;
+}
+
+sub work {
+ my ($me, $job) = @_;
+
+ my $data = $me->{db}->bulkget_latest();
+ my $tmpdir = defined $me->{cfg}->{tmpdir} ? $me->{cfg}->{tmpdir} : '/tmp';
+ warn Dumper($data) if defined $me->{cfg}->{debug};
+ if (defined $me->{cfg}->{atom_path}) {
+ if (defined $me->{cfg}->{hub}) {
+ my $pub = Net::PubSubHubbub::Publisher->new(hub => $me->{cfg}->{hub});
+ $pub->publish_update($me->{cfg}->{atom_url});
+ }
+ }
+
+ if (defined $me->{cfg}->{json_path}) {
+ my $out = {data_version => DATA_VERSION, timestamp => time, data => $data};
+ my ($fh, $tmp_file) = tempfile("tmpjsonXXXXXX", DIR => $tmpdir);
+ print $fh encode_json($out);
+ close($fh);
+ rename $tmp_file, $me->{cfg}->{json_path};
+ }
+}
+
+1;
sub dequeue {
my ($me) = @_;
my $job = $me->SUPER::dequeue;
- $job->{arg} = decode_json($job->{arg});
+ $job->{arg} = decode_json($job->{arg}) if defined $job && defined $job->{arg};
return $job;
}
--- /dev/null
+#!/usr/bin/perl
+#
+# Copyright (c) 2012 ISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
+# This program is covered by the GNU General Public License 2
+#
+package Mubot4FB::TweetWorker;
+
+use strict;
+use utf8;
+
+use Data::Dumper;
+
+use base 'TheSchwartz::Worker';
+use Mubot4FB::Tweet;
+
+sub sighandler {
+ warn "caught signal @_\n";
+ no warnings 'redefine';
+ *TheSchwartz::work_once = sub { exit };
+}
+
+sub compose_text {
+ my ($me, $args) = @_;
+
+ return $args->{submitter} . '曰く、'.$args->{prefix}.' '.$args->{comment};
+}
+
+sub work {
+ my ($me, $job) = @_;
+
+ $SIG{HUP} = $SIG{INT} = $SIG{TERM} = \&sighandler;
+
+ if ($job ->arg->{_command} eq 'remove') {
+ } else {
+ my $text = $me->compose_text($job->arg);
+ my $uri = $me->post_uri($job->{arg}->{post_id});
+ }
+ $job->completed();
+}
--- /dev/null
+#!/usr/bin/perl
+#
+# Copyright (c) 2012 ISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
+# This program is covered by the GNU General Public License 2
+#
+package Mubot4FB::Worker;
+use strict;
+use utf8;
+
+use Mubot4FB::Config;
+use Mubot4FB::DB;
+use Mubot4FB::Queue::Worker;
+
+use Data::Dumper;
+
+sub new {
+ my ($proto, $config_name, $worker_name) = @_;
+ my $class = ref $proto || $proto;
+ my $self = {config_name => $config_name, worker_name => $worker_name};
+ bless $self, $class;
+
+ $self->init($config_name, $worker_name);
+ return $self;
+}
+
+sub init {
+ my ($me, $config_name, $worker_name) = @_;
+ $me->{c} = Mubot4FB::Config->new($config_name);
+ my $cfg = $me->{cfg} = $me->{c}->{cfg};
+ $me->{db} = Mubot4FB::DB->new($me->{cfg});
+ $me->{queue} = Mubot4FB::Queue::Worker->new($me->{cfg}, $worker_name, $me->{db}->{dbh});
+ $me->{worker_file} = $worker_name . '.pm';
+ $me->{worker_file} =~ s/::/\//g;
+ require($me->{worker_file});
+ $me->{worker} = $worker_name->new($cfg);
+ $me->{interval} = defined $me->{cfg}->{$worker_name}->{interval} ? $me->{cfg}->{$worker_name}->{interval} : 3;
+ return $me;
+}
+
+sub run {
+ my ($me) = @_;
+ while (1) {
+ my $job = $me->{queue}->dequeue;
+ if ($me->{queue}->errstr) {
+ warn $me->{queue}->errstr;
+ sleep($me->{interval});
+ next;
+ }
+ if ($job) {
+ $me->{worker}->work($job);
+ } else {
+ sleep($me->{interval});
+ }
+ }
+}
+
+1;
--- /dev/null
+#!/usr/bin/perl
+
+use strict;
+use utf8;
+
+use Data::Dumper;
+
+use FindBin;
+use lib ("$FindBin::Bin/lib", '/usr/share/mubot4fb/lib');
+
+use Mubot4FB::FeedWorker;
+use Mubot4FB::Worker;
+
+my $config_name = $ARGV[0] || 'not_found';
+my $worker_name = 'Mubot4FB::FeedWorker';
+
+my $worker = Mubot4FB::Worker->new($config_name, $worker_name);
+
+$worker->run();