Igor Gariev (gariev) wrote in changelog,
Igor Gariev
gariev
changelog

[livejournal] r16143: TheSchwartz update: support of multiple ...

Committer: gariev
TheSchwartz update: support of multiple roles
U   trunk/bin/worker/antispam
U   trunk/bin/worker/esn-cluster-subs
D   trunk/bin/worker/esn-cluster-subs-mass
U   trunk/bin/worker/esn-filter-subs
D   trunk/bin/worker/esn-filter-subs-mass
U   trunk/bin/worker/esn-fired-event
D   trunk/bin/worker/esn-fired-event-mass
U   trunk/bin/worker/esn-process-sub
D   trunk/bin/worker/esn-process-sub-mass
U   trunk/bin/worker/friendchange
U   trunk/bin/worker/incoming-email
U   trunk/bin/worker/lazy-cleanup
U   trunk/bin/worker/notify_pingback
U   trunk/bin/worker/ping-hubbub
U   trunk/bin/worker/ping-weblogscom
U   trunk/bin/worker/process-esn
D   trunk/bin/worker/process-esn-mass
U   trunk/bin/worker/process-eventlogrecord
U   trunk/bin/worker/process-privacy
U   trunk/bin/worker/process-sms
U   trunk/bin/worker/send-email
D   trunk/bin/worker/send-email-mass
U   trunk/bin/worker/support-notify
U   trunk/bin/worker/update-fb-friends
U   trunk/cgi-bin/LJ/Event/OfficialPost.pm
U   trunk/cgi-bin/LJ/Event.pm
U   trunk/cgi-bin/LJ/NewWorker/Reminders.pm
U   trunk/cgi-bin/LJ/NewWorker/TheSchwartz.pm
U   trunk/cgi-bin/LJ/Worker/Reminders.pm
U   trunk/cgi-bin/LJ/Worker/TheSchwartz.pm
U   trunk/cgi-bin/ljlib.pl
Modified: trunk/bin/worker/antispam
===================================================================
--- trunk/bin/worker/antispam	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/antispam	2010-01-27 08:05:13 UTC (rev 16143)
@@ -10,7 +10,7 @@
 
 package LJ::NewWorker::TheSchwartz::AntiSpam;
 use base 'LJ::NewWorker::TheSchwartz';
-sub capabilities { return [ 'LJ::Worker::AntiSpam' ] }
+sub capabilities { 'LJ::Worker::AntiSpam' }
 __PACKAGE__->start();
 
 package LJ::Worker::AntiSpam;

Modified: trunk/bin/worker/esn-cluster-subs
===================================================================
--- trunk/bin/worker/esn-cluster-subs	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/esn-cluster-subs	2010-01-27 08:05:13 UTC (rev 16143)
@@ -10,6 +10,6 @@
 
 package LJ::NewWorker::TheSchwartz::FindSubsByCluster;
 use base 'LJ::NewWorker::TheSchwartz';
-sub capabilities { [ "LJ::Worker::FindSubsByCluster" => '' ] }
+sub capabilities { "LJ::Worker::FindSubsByCluster" }
 
 1;

Deleted: trunk/bin/worker/esn-cluster-subs-mass
===================================================================
--- trunk/bin/worker/esn-cluster-subs-mass	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/esn-cluster-subs-mass	2010-01-27 08:05:13 UTC (rev 16143)
@@ -1,15 +0,0 @@
-#!/usr/bin/perl
-use strict;
-use lib "$ENV{LJHOME}/cgi-bin";
-require 'ljlib.pl';
-require 'ljprotocol.pl';
-require 'ljlang.pl';
-use LJ::NewWorker::TheSchwartz;
-use LJ::ESN;
-LJ::NewWorker::TheSchwartz::FindSubsByClusterMass->start();
-
-package LJ::NewWorker::TheSchwartz::FindSubsByClusterMass;
-use base 'LJ::NewWorker::TheSchwartz';
-sub capabilities { [ "LJ::Worker::FindSubsByCluster" => 'mass' ] }
-
-1;

Modified: trunk/bin/worker/esn-filter-subs
===================================================================
--- trunk/bin/worker/esn-filter-subs	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/esn-filter-subs	2010-01-27 08:05:13 UTC (rev 16143)
@@ -10,6 +10,6 @@
 
 package LJ::NewWorker::TheSchwartz::FilterSubs;
 use base 'LJ::NewWorker::TheSchwartz';
-sub capabilities { [ "LJ::Worker::FilterSubs" => '' ] }
+sub capabilities { "LJ::Worker::FilterSubs" }
 
 1;

Deleted: trunk/bin/worker/esn-filter-subs-mass
===================================================================
--- trunk/bin/worker/esn-filter-subs-mass	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/esn-filter-subs-mass	2010-01-27 08:05:13 UTC (rev 16143)
@@ -1,15 +0,0 @@
-#!/usr/bin/perl
-use strict;
-use lib "$ENV{LJHOME}/cgi-bin";
-require 'ljlib.pl';
-require 'ljprotocol.pl';
-require 'ljlang.pl';
-use LJ::NewWorker::TheSchwartz;
-use LJ::ESN;
-LJ::NewWorker::TheSchwartz::FilterSubsMass->start();
-
-package LJ::NewWorker::TheSchwartz::FilterSubsMass;
-use base 'LJ::NewWorker::TheSchwartz';
-sub capabilities { [ "LJ::Worker::FilterSubs" => 'mass' ] }
-
-1;

Modified: trunk/bin/worker/esn-fired-event
===================================================================
--- trunk/bin/worker/esn-fired-event	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/esn-fired-event	2010-01-27 08:05:13 UTC (rev 16143)
@@ -10,6 +10,6 @@
 
 package LJ::NewWorker::TheSchwartz::FiredEvent;
 use base 'LJ::NewWorker::TheSchwartz';
-sub capabilities { [ "LJ::Worker::FiredEvent" => '' ] }
+sub capabilities { "LJ::Worker::FiredEvent" }
 
 1;

Deleted: trunk/bin/worker/esn-fired-event-mass
===================================================================
--- trunk/bin/worker/esn-fired-event-mass	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/esn-fired-event-mass	2010-01-27 08:05:13 UTC (rev 16143)
@@ -1,15 +0,0 @@
-#!/usr/bin/perl
-use strict;
-use lib "$ENV{LJHOME}/cgi-bin";
-require 'ljlib.pl';
-require 'ljprotocol.pl';
-require 'ljlang.pl';
-use LJ::NewWorker::TheSchwartz;
-use LJ::ESN;
-LJ::NewWorker::TheSchwartz::FiredEventMass->start();
-
-package LJ::NewWorker::TheSchwartz::FiredEventMass;
-use base 'LJ::NewWorker::TheSchwartz';
-sub capabilities { [ "LJ::Worker::FiredEventMass" => 'mass' ] }
-
-1;

Modified: trunk/bin/worker/esn-process-sub
===================================================================
--- trunk/bin/worker/esn-process-sub	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/esn-process-sub	2010-01-27 08:05:13 UTC (rev 16143)
@@ -10,6 +10,6 @@
 
 package LJ::NewWorker::TheSchwartz::ProcessSub;
 use base 'LJ::NewWorker::TheSchwartz';
-sub capabilities { [ "LJ::Worker::ProcessSub" => '' ] }
+sub capabilities { "LJ::Worker::ProcessSub" }
 
 1;

Deleted: trunk/bin/worker/esn-process-sub-mass
===================================================================
--- trunk/bin/worker/esn-process-sub-mass	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/esn-process-sub-mass	2010-01-27 08:05:13 UTC (rev 16143)
@@ -1,15 +0,0 @@
-#!/usr/bin/perl
-use strict;
-use lib "$ENV{LJHOME}/cgi-bin";
-require 'ljlib.pl';
-require 'ljprotocol.pl';
-require 'ljlang.pl';
-use LJ::NewWorker::TheSchwartz;
-use LJ::ESN;
-LJ::NewWorker::TheSchwartz::ProcessSubMass->start();
-
-package LJ::NewWorker::TheSchwartz::ProcessSubMass;
-use base 'LJ::NewWorker::TheSchwartz';
-sub capabilities { [ "LJ::Worker::ProcessSubMass" => 'mass' ] }
-
-1;

Modified: trunk/bin/worker/friendchange
===================================================================
--- trunk/bin/worker/friendchange	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/friendchange	2010-01-27 08:05:13 UTC (rev 16143)
@@ -28,8 +28,8 @@
 
 sub capabilities { 
     return (
-        [ 'LJ::NewWorker::TheSchwartz::FriendChange' => '' ], 
-        [ 'LJ::Worker::FriendChange' => '' ], 
+        'LJ::NewWorker::TheSchwartz::FriendChange', 
+        'LJ::Worker::FriendChange', 
     );
 };
 

Modified: trunk/bin/worker/incoming-email
===================================================================
--- trunk/bin/worker/incoming-email	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/incoming-email	2010-01-27 08:05:13 UTC (rev 16143)
@@ -10,7 +10,7 @@
 
 package LJ::NewWorker::TheSchwartz::IncomingEmail;
 use base 'LJ::NewWorker::TheSchwartz';
-sub capabilities { [ 'LJ::Worker::IncomingEmail' ] }
+sub capabilities { 'LJ::Worker::IncomingEmail' }
 LJ::NewWorker::TheSchwartz::IncomingEmail->start();
 
 

Modified: trunk/bin/worker/lazy-cleanup
===================================================================
--- trunk/bin/worker/lazy-cleanup	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/lazy-cleanup	2010-01-27 08:05:13 UTC (rev 16143)
@@ -6,7 +6,7 @@
 package LJ::NewWorker::TheSchwartz::DeleteEntry;
 use base 'LJ::NewWorker::TheSchwartz';
 
-sub capabilities { [ 'LJ::Worker::DeleteEntry' => '' ] }
+sub capabilities { 'LJ::Worker::DeleteEntry' }
 
 __PACKAGE__->start();
 

Modified: trunk/bin/worker/notify_pingback
===================================================================
--- trunk/bin/worker/notify_pingback	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/notify_pingback	2010-01-27 08:05:13 UTC (rev 16143)
@@ -9,6 +9,6 @@
 
 package LJ::NewWorker::TheSchwartz::NotifyPingbackServer;
 use base 'LJ::NewWorker::TheSchwartz';
-sub capabilities { [ 'TheSchwartz::Worker::NotifyPingbackServer' => '' ] }
+sub capabilities { 'TheSchwartz::Worker::NotifyPingbackServer' }
 
 1;

Modified: trunk/bin/worker/ping-hubbub
===================================================================
--- trunk/bin/worker/ping-hubbub	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/ping-hubbub	2010-01-27 08:05:13 UTC (rev 16143)
@@ -9,6 +9,6 @@
 
 package LJ::NewWorker::TheSchwartz::PubSubHubbubPublish;
 use base 'LJ::NewWorker::TheSchwartz';
-sub capabilities { [ 'TheSchwartz::Worker::PubSubHubbubPublish' => '' ] }
+sub capabilities { 'TheSchwartz::Worker::PubSubHubbubPublish' }
 
 1;

Modified: trunk/bin/worker/ping-weblogscom
===================================================================
--- trunk/bin/worker/ping-weblogscom	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/ping-weblogscom	2010-01-27 08:05:13 UTC (rev 16143)
@@ -4,7 +4,7 @@
 
 package LJ::NewWorker::TheSchwartz::Ping::WeblogsCom;
 use base 'LJ::NewWorker::TheSchwartz';
-sub capabilities { [ 'LJ::Worker::Ping::WeblogsCom' => '' ] }
+sub capabilities { 'LJ::Worker::Ping::WeblogsCom' }
 __PACKAGE__->start();
 
 package LJ::Worker::Ping::WeblogsCom;

Modified: trunk/bin/worker/process-esn
===================================================================
--- trunk/bin/worker/process-esn	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/process-esn	2010-01-27 08:05:13 UTC (rev 16143)
@@ -12,7 +12,7 @@
 
 # Capabilities must return array of references to pairs: (classname, role).
 # LJ::ESN->schwartz_capabilities() returns array to classnames.
-sub capabilities { map { [ $_ => '' ] } LJ::ESN->schwartz_capabilities() }
+sub capabilities { return LJ::ESN->schwartz_capabilities() }
 
 __PACKAGE__->start();
 

Deleted: trunk/bin/worker/process-esn-mass
===================================================================
--- trunk/bin/worker/process-esn-mass	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/process-esn-mass	2010-01-27 08:05:13 UTC (rev 16143)
@@ -1,19 +0,0 @@
-#!/usr/bin/perl
-use strict;
-use lib "$ENV{LJHOME}/cgi-bin";
-require 'ljlib.pl';
-require 'ljprotocol.pl';
-require 'ljlang.pl';
-
-package LJ::NewWorker::TheSchwartz::ESN;
-use strict;
-use base 'LJ::NewWorker::TheSchwartz';
-use LJ::ESN;
-
-# Capabilities must return array of references to pairs: (classname, role).
-# LJ::ESN->schwartz_capabilities() returns array to classnames.
-sub capabilities { map { [ $_ => 'mass' ] } LJ::ESN->schwartz_capabilities() }
-
-__PACKAGE__->start();
-
-1;

Modified: trunk/bin/worker/process-eventlogrecord
===================================================================
--- trunk/bin/worker/process-eventlogrecord	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/process-eventlogrecord	2010-01-27 08:05:13 UTC (rev 16143)
@@ -10,7 +10,7 @@
 require 'ljprotocol.pl';
 use LJ::EventLogRecord;
 
-sub capabilities { map { [ $_ => '' ] } LJ::EventLogRecord->schwartz_capabilities() }
+sub capabilities { return LJ::EventLogRecord->schwartz_capabilities }
 
 __PACKAGE__->start();
 

Modified: trunk/bin/worker/process-privacy
===================================================================
--- trunk/bin/worker/process-privacy	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/process-privacy	2010-01-27 08:05:13 UTC (rev 16143)
@@ -6,7 +6,7 @@
 use strict;
 use lib "$ENV{LJHOME}/cgi-bin";
 use base 'LJ::NewWorker::TheSchwartz';
-sub capabilities { map { [ $_ => '' ] } LJ::MassPrivacy->schwartz_capabilities() }
+sub capabilities { LJ::MassPrivacy->schwartz_capabilities() }
 __PACKAGE__->start();
 
 package LJ::Worker::MassPrivacy;

Modified: trunk/bin/worker/process-sms
===================================================================
--- trunk/bin/worker/process-sms	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/process-sms	2010-01-27 08:05:13 UTC (rev 16143)
@@ -11,7 +11,7 @@
 require 'ljlang.pl';
 use LJ::SMS;
 
-sub capabilities { map { [ $_ => '' ] } LJ::SMS->schwartz_capabilities() }
+sub capabilities { return LJ::SMS->schwartz_capabilities() }
 
 __PACKAGE__->start();
 

Modified: trunk/bin/worker/send-email
===================================================================
--- trunk/bin/worker/send-email	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/send-email	2010-01-27 08:05:13 UTC (rev 16143)
@@ -8,7 +8,6 @@
 package LJ::NewWorker::TheSchwartz::SendEmail;
 use base 'LJ::NewWorker::TheSchwartz';
 require 'ljlib.pl';
-use LJ::NewWorker::TheSchwartz;
 use TheSchwartz::Worker::SendEmail;
 use Time::HiRes ();
 
@@ -19,9 +18,10 @@
         my ($rcpt, $job, $details) = @_;
         LJ::errobj('DieString', message => "send_email to $rcpt failed: $details")->log;
     });
+}
 
-    my $sclient = LJ::theschwartz();
-    $sclient->set_verbose(sub {
+sub schwartz_verbose_handler {
+    return sub {
         my ($msg, $job) = @_;
         $msg =~ s/\s+$//;
         warn "$msg\n";
@@ -31,10 +31,10 @@
         my $handle = $job->handle->as_string;
         my $now = sprintf("%0.05f", Time::HiRes::time());
         LJ::errobj('DieString', message => "ts_info: $now: [$handle] $msg")->log;
-    });
+    };
 }
 
-sub capabilities { { [ 'TheSchwartz::Worker::SendEmail' => '' ] } }
+sub capabilities { 'TheSchwartz::Worker::SendEmail'; }
 sub on_idle { $0 = "send-email [idle]"; }
 
 1;

Deleted: trunk/bin/worker/send-email-mass
===================================================================
--- trunk/bin/worker/send-email-mass	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/send-email-mass	2010-01-27 08:05:13 UTC (rev 16143)
@@ -1,40 +0,0 @@
-#!/usr/bin/perl
-use strict;
-use lib "$ENV{LJHOME}/cgi-bin";
-use LJ::NewWorker::TheSchwartz;
-
-LJ::NewWorker::TheSchwartz::SendEmailMass->start();
-
-package LJ::NewWorker::TheSchwartz::SendEmailMass;
-use base 'LJ::NewWorker::TheSchwartz';
-require 'ljlib.pl';
-use LJ::NewWorker::TheSchwartz;
-use TheSchwartz::Worker::SendEmail;
-use Time::HiRes ();
-
-sub BEGIN {
-    TheSchwartz::Worker::SendEmail->set_HELO($LJ::DOMAIN);
-    TheSchwartz::Worker::SendEmail->set_keep_exit_status(sub { $LJ::KEEP_EXITSTATUS{'SendEmail'} || 0 });
-    TheSchwartz::Worker::SendEmail->set_on_5xx(sub {
-        my ($rcpt, $job, $details) = @_;
-        LJ::errobj('DieString', message => "send_email to $rcpt failed: $details")->log;
-    });
-
-    my $sclient = LJ::theschwartz( { role => 'mass' } );
-    $sclient->set_verbose(sub {
-        my ($msg, $job) = @_;
-        $msg =~ s/\s+$//;
-        warn "$msg\n";
-        return unless $LJ::DEBUG{'ts-sendemail'};
-        return if $msg =~ /found no jobs$/;
-
-        my $handle = $job->handle->as_string;
-        my $now = sprintf("%0.05f", Time::HiRes::time());
-        LJ::errobj('DieString', message => "ts_info: $now: [$handle] $msg")->log;
-    });
-}
-
-sub capabilities { { [ 'TheSchwartz::Worker::SendEmail' => 'mass' ] } }
-sub on_idle { $0 = "send-email [idle]"; }
-
-1;

Modified: trunk/bin/worker/support-notify
===================================================================
--- trunk/bin/worker/support-notify	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/support-notify	2010-01-27 08:05:13 UTC (rev 16143)
@@ -5,7 +5,7 @@
 package LJ::NewWorker::TheSchwartz::SupportNotify;
 use strict;
 use base 'LJ::NewWorker::TheSchwartz';
-sub capabilities { [ 'LJ::Worker::SupportNotify' ] }
+sub capabilities { 'LJ::Worker::SupportNotify' }
 __PACKAGE__->start();
 
 package LJ::Worker::SupportNotify;

Modified: trunk/bin/worker/update-fb-friends
===================================================================
--- trunk/bin/worker/update-fb-friends	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/bin/worker/update-fb-friends	2010-01-27 08:05:13 UTC (rev 16143)
@@ -12,9 +12,11 @@
 use RPC::XML::Client;
 
 sub capabilities {
-        [   'LJ::NewWorker::TheSchwartz::UpdateFotobilderFriends' => '' ],
-        [   'LJ::NewWorker::UpdateFotobilderFriends' => ''              ],
-        [   'LJ::Worker::UpdateFotobilderFriends' => ''                 ],
+    return (
+        'LJ::NewWorker::TheSchwartz::UpdateFotobilderFriends',
+        'LJ::NewWorker::UpdateFotobilderFriends',
+        'LJ::Worker::UpdateFotobilderFriends',
+    );
 }
 
 __PACKAGE__->start();

Modified: trunk/cgi-bin/LJ/Event/OfficialPost.pm
===================================================================
--- trunk/cgi-bin/LJ/Event/OfficialPost.pm	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/cgi-bin/LJ/Event/OfficialPost.pm	2010-01-27 08:05:13 UTC (rev 16143)
@@ -158,8 +158,6 @@
     return BML::ml('event.officialpost', { sitename => $LJ::SITENAME }); # $LJ::SITENAME makes a new announcement
 }
 
-sub schwartz_role { 'mass' }
-
 sub is_tracking { 0 }
 
 sub is_subscription_visible_to { 1 }

Modified: trunk/cgi-bin/LJ/Event.pm
===================================================================
--- trunk/cgi-bin/LJ/Event.pm	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/cgi-bin/LJ/Event.pm	2010-01-27 08:05:13 UTC (rev 16143)
@@ -464,15 +464,15 @@
     );
 }
 
-# return a string representing a Schwartz role [queue] used to handle this event
-#
-# this is a virtual function; base class function returns "default" which works
-# well for most events. it is perfectly fine not to override this unless
-# this event deals with a lot of users.
-#
-# my $schwartz = LJ::theschwartz({ 'role' => $event->schwartz_role});
-sub schwartz_role { 'default' }
+# Returns a string representing a Schwartz role [queue] used to handle this event
+# By default returns undef, which is ok for most cases.
+sub schwartz_role { 
+    my $self = shift;
+    my $class = ref $self || $self;
 
+    return $LJ::SCHWARTZ_ROLE_FOR_ESN_CLASS{$class};
+}
+
 # insert a job for TheSchwartz to process this event.
 #
 # $event->fire;

Modified: trunk/cgi-bin/LJ/NewWorker/Reminders.pm
===================================================================
--- trunk/cgi-bin/LJ/NewWorker/Reminders.pm	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/cgi-bin/LJ/NewWorker/Reminders.pm	2010-01-27 08:05:13 UTC (rev 16143)
@@ -33,8 +33,7 @@
 
     my $self = bless {}, $class;
 
-    for my $db (@LJ::THESCHWARTZ_DBS) {
-        next if exists $db->{role} && exists $db->{role}->{mass} && $db->{role}->{mass}; # Skip mass roles
+    for my $db (map { $LJ::THESCHWARTZ_DBS{$_} } @{ $LJ::THESCHWARTZ_DBS_ROLES{default} }) {
         my $full = join '|', map { $db->{$_} || '' } qw( dsn user pass );
         # FIXME: Different roles can has same $full line!
         $self->{databases}{ md5_hex($full) } = $db;

Modified: trunk/cgi-bin/LJ/NewWorker/TheSchwartz.pm
===================================================================
--- trunk/cgi-bin/LJ/NewWorker/TheSchwartz.pm	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/cgi-bin/LJ/NewWorker/TheSchwartz.pm	2010-01-27 08:05:13 UTC (rev 16143)
@@ -5,13 +5,15 @@
 use base "LJ::NewWorker", "Exporter";
 require "ljlib.pl";
 
-my  $interval   = 10;
-my  $verbose    = 0;
+my $interval        = 10;
+my $verbose         = 0;
+my $schwartz_role   = 'worker';
 
 sub options {
     my $self = shift;
     return (
-        'interval|i=i'  => \$interval,
+        'interval|i=i'          => \$interval,
+        'schwartz-role|r=s'     => \$schwartz_role,
         $self->SUPER::options(),
     );
 }
@@ -20,30 +22,35 @@
     my $self = shift;
     return
         $self->SUPER::help() .
-        "-i | --interval n  set sleep interval to n secounds\n";
+        "-i | --interval=n          set sleep interval to n secounds\n";
+        "-r | --schwartz-role=role  connect to db with specified role (defualt is '$schwartz_role')\n";
 }
 
 sub capabilities    { }
-
 sub on_idle         { }
 sub on_afterwork    { }
 sub on_prework      { 1 }  # return 1 to proceed and do work
+sub schwartz_verbose_handler { } ## may return coderef for TheSchartz->set_verbose method
 
 my $sclient;
-my $used_role;
 
 sub _init {
-    my ($class, $role) = @_;
-    $role ||= 'drain';
+    my $class = shift;
+    
+    if ($sclient) {
+        warn "Schwartz client has been initialized already" if $verbose;
+        return;
+    }
+    
+    warn "The Schwartz _init(): init with role '$schwartz_role'.\n" if $verbose;
 
-    die "Already connected to TheSchwartz with role '$used_role'"
-        if defined $used_role and $role ne $used_role;
-
-    print STDERR "The Schwartz _init(): init with role $role.\n" if $verbose;
-
-    $sclient = LJ::theschwartz({ role => $role }) or die "Could not get schwartz client";
-    $used_role = $role; # save success role
-    $sclient->set_verbose($verbose);
+    $sclient = LJ::theschwartz({ role => $schwartz_role }) or die "Could not get schwartz client";
+    $sclient->set_verbose( $class->schwartz_verbose_handler || $class->verbose );
+    foreach my $classname ($class->capabilities) {
+        warn "The Schwartz run(): can_do('$classname').\n" if $verbose;
+        $sclient->can_do($classname);
+    }
+    
 }
 
 sub run {
@@ -51,21 +58,10 @@
     my $sleep = 0;
 
     $verbose = $class->verbose;
-
-    print STDERR "The Schwartz run(): init.\n" if $verbose;
-
-    foreach my $cap ($class->capabilities()) {
-        my ($classname, $role) = @$cap;
-        print STDERR "The Schwartz run(): _init('$role').\n" if $verbose;
-        $class->_init($role) unless $sclient;
-        print STDERR "The Schwartz run(): can_do('$classname').\n" if $verbose;
-        $sclient->can_do($classname);
-    }
-
-    $class->_init() unless $sclient;
-
-    print STDERR "The Schwartz run(): init complete, let's do a work.\n" if $verbose;
-
+    
+    $class->_init;
+    warn "The Schwartz run(): init complete.\n" if $verbose;
+    
     my $last_death_check = time();
     while ( ! $class->should_quit()) {
         eval {
@@ -73,12 +69,12 @@
             $class->check_limits();
 
             my $did_work = 0;
-            print STDERR "looking for work..." if $verbose;
+            warn "looking for work...\n" if $verbose;
             if ($class->on_prework()) {
                 $did_work = $sclient->work_once();
                 $class->on_afterwork($did_work);
             }
-            print STDERR "   did work = ", ($did_work || '') if $verbose;
+            warn "did work = " . ($did_work || '') . "\n" if $verbose;
 
             return if $class->should_quit();
 

Modified: trunk/cgi-bin/LJ/Worker/Reminders.pm
===================================================================
--- trunk/cgi-bin/LJ/Worker/Reminders.pm	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/cgi-bin/LJ/Worker/Reminders.pm	2010-01-27 08:05:13 UTC (rev 16143)
@@ -30,8 +30,7 @@
 
     my $self = bless {}, $class;
 
-    for my $db (@LJ::THESCHWARTZ_DBS) {
-        next if exists $db->{role} && exists $db->{role}->{mass} && $db->{role}->{mass}; # Skip mass roles
+    for my $db (map { $LJ::THESCHWARTZ_DBS{$_} } @{ $LJ::THESCHWARTZ_DBS_ROLES{default} }) {
         my $full = join '|', map { $db->{$_} || '' } qw( dsn user pass );
         # FIXME: Different roles can has same $full line!
         $self->{databases}{ md5_hex($full) } = $db;

Modified: trunk/cgi-bin/LJ/Worker/TheSchwartz.pm
===================================================================
--- trunk/cgi-bin/LJ/Worker/TheSchwartz.pm	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/cgi-bin/LJ/Worker/TheSchwartz.pm	2010-01-27 08:05:13 UTC (rev 16143)
@@ -15,25 +15,14 @@
 
 my $on_prework = sub { 1 };  # return 1 to proceed and do work
 
-my $used_role;
-
 sub schwartz_init {
-    my ($role) = @_;
-    $role ||= 'drain';
-
-    $sclient = LJ::theschwartz({ role => $role }) or die "Could not get schwartz client";
-    $used_role = $role; # save success role
+    $sclient = LJ::theschwartz({ role => 'worker' }) or die "Could not get schwartz client";
     $sclient->set_verbose(LJ::Worker::VERBOSE());
 }
 
 sub schwartz_decl {
-    my ($classname, $role) = @_;
-    $role ||= 'drain';
-
-    die "Already connected to TheSchwartz with role '$used_role'" if defined $used_role and $role ne $used_role;
-
-    schwartz_init($role) unless $sclient;
-
+    my $classname = shift;
+    schwartz_init() unless $sclient;
     $sclient->can_do($classname);
 }
 

Modified: trunk/cgi-bin/ljlib.pl
===================================================================
--- trunk/cgi-bin/ljlib.pl	2010-01-27 04:40:56 UTC (rev 16142)
+++ trunk/cgi-bin/ljlib.pl	2010-01-27 08:05:13 UTC (rev 16143)
@@ -374,31 +374,37 @@
 }
 
 sub theschwartz {
-    return LJ::Test->theschwartz(@_) if $LJ::_T_FAKESCHWARTZ;
-
     my $opts = shift;
+    
+    return LJ::Test->theschwartz() if $LJ::_T_FAKESCHWARTZ;
 
-    my $role = $opts->{role} || "default";
+    if (%LJ::THESCHWARTZ_DBS_ROLES) {
+        ## new config - with roles
+        my $role = $opts->{role} || "default";
+        return $LJ::SchwartzClient{$role} if $LJ::SchwartzClient{$role};
 
-    return $LJ::SchwartzClient{$role} if $LJ::SchwartzClient{$role};
+        my @dbs;
+        die "LJ::theschwartz(): unknown role '$role'" unless $LJ::THESCHWARTZ_DBS_ROLES{$role};
+        foreach my $name (@{ $LJ::THESCHWARTZ_DBS_ROLES{$role} }) {
+            die "LJ::theschwartz(): unknown database name '$name' in role '$role'"
+                unless $LJ::THESCHWARTZ_DBS{$name};
+            push @dbs, $LJ::THESCHWARTZ_DBS{$name};
+        }
+        die "LJ::theschwartz(): no databases for role '$role'" unless @dbs;
 
-    unless (scalar grep { defined $_->{role} } @LJ::THESCHWARTZ_DBS) { # old config
-        $LJ::SchwartzClient{$role} = TheSchwartz->new(databases => \@LJ::THESCHWARTZ_DBS);
-        return $LJ::SchwartzClient{$role};
-    }
+        my $client = TheSchwartz->new(databases => \@dbs);
 
-    my @dbs = grep { $_->{role}->{$role} } @LJ::THESCHWARTZ_DBS;
-    die "Unknown role in LJ::theschwartz: '$role'" unless @dbs;
-
-    my $client = TheSchwartz->new(databases => \@dbs);
-
-    if ($client && $client->can('delete_every_n_errors')) {
-        $client->delete_every_n_errors($LJ::DELETE_EVERY_N_ERRORS);
+        if ($client && $client->can('delete_every_n_errors')) {
+            $client->delete_every_n_errors($LJ::DELETE_EVERY_N_ERRORS);
+        }
+        
+        $LJ::SchwartzClient{$role} = $client;
+        return $client;
+    } else {
+        ## old config
+        $LJ::SchwartzClient ||= TheSchwartz->new(databases => \@LJ::THESCHWARTZ_DBS);
+        return $LJ::SchwartzClient;
     }
-
-    $LJ::SchwartzClient{$role} = $client;
-
-    return $client;
 }
 
 sub sms_gateway {

Tags: gariev, livejournal, pl, pm
Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments