From f6a3f8319d442e832edc901ff12169ba17dceb81 Mon Sep 17 00:00:00 2001 From: Gervase Markham Date: Wed, 5 Jan 2011 11:48:49 +0000 Subject: Allow extensions to add new Jobs. r,a=mkanat. https://bugzilla.mozilla.org/show_bug.cgi?id=617012 --- Bugzilla/Hook.pm | 17 +++++++++++++++++ Bugzilla/JobQueue.pm | 12 +++++++++++- Bugzilla/JobQueue/Runner.pm | 2 +- extensions/Example/Extension.pm | 14 ++++++++++++++ template/en/default/global/code-error.html.tmpl | 3 ++- 5 files changed, 45 insertions(+), 3 deletions(-) diff --git a/Bugzilla/Hook.pm b/Bugzilla/Hook.pm index f6fff241a..b48ebb9fa 100644 --- a/Bugzilla/Hook.pm +++ b/Bugzilla/Hook.pm @@ -730,6 +730,23 @@ database when run. =back +=head2 job_map + +Bugzilla has a system - L - for running jobs +asynchronously, if the administrator has set it up. This hook allows the +addition of mappings from job names to handler classes, so an extension can +fire off jobs. + +Params: + +=over + +=item C - The job map hash. Key: the name of the job, as should be +passed to Bugzilla->job_queue->insert(). Value: the name of the Perl module +which implements the task (an instance of L). + +=back + =head2 mailer_before_send Called right before L sends a message to the MTA. diff --git a/Bugzilla/JobQueue.pm b/Bugzilla/JobQueue.pm index 1046cf2c3..2804b9179 100644 --- a/Bugzilla/JobQueue.pm +++ b/Bugzilla/JobQueue.pm @@ -35,6 +35,16 @@ use constant JOB_MAP => { send_mail => 'Bugzilla::Job::Mailer', }; +sub job_map { + if (!defined(Bugzilla->request_cache->{job_map})) { + my $job_map = JOB_MAP; + Bugzilla::Hook::process('job_map', { job_map => $job_map }); + Bugzilla->request_cache->{job_map} = $job_map; + } + + return Bugzilla->request_cache->{job_map}; +} + sub new { my $class = shift; @@ -69,7 +79,7 @@ sub insert { my $self = shift; my $job = shift; - my $mapped_job = JOB_MAP->{$job}; + my $mapped_job = Bugzilla::JobQueue->job_map()->{$job}; ThrowCodeError('jobqueue_no_job_mapping', { job => $job }) if !$mapped_job; unshift(@_, $mapped_job); diff --git a/Bugzilla/JobQueue/Runner.pm b/Bugzilla/JobQueue/Runner.pm index 8cfc965eb..20cf6439f 100644 --- a/Bugzilla/JobQueue/Runner.pm +++ b/Bugzilla/JobQueue/Runner.pm @@ -201,7 +201,7 @@ sub gd_run { my $jq = Bugzilla->job_queue(); $jq->set_verbose($self->{debug}); - foreach my $module (values %{ Bugzilla::JobQueue::JOB_MAP() }) { + foreach my $module (values %{ Bugzilla::JobQueue->job_map() }) { eval "use $module"; $jq->can_do($module); } diff --git a/extensions/Example/Extension.pm b/extensions/Example/Extension.pm index 0fb56a32f..ab773561d 100644 --- a/extensions/Example/Extension.pm +++ b/extensions/Example/Extension.pm @@ -386,6 +386,20 @@ sub install_before_final_checks { print "Install-before_final_checks hook\n" unless $args->{silent}; } +sub job_map { + my ($self, $args) = @_; + + my $job_map = $args->{job_map}; + + # This adds the named class (an instance of TheSchwartz::Worker) as a + # handler for when a job is added with the name "some_task". + $job_map->{'some_task'} = 'Bugzilla::Extension::Example::Job::SomeClass'; + + # Schedule a job like this: + # my $queue = Bugzilla->job_queue(); + # $queue->insert('some_task', { some_parameter => $some_variable }); +} + sub mailer_before_send { my ($self, $args) = @_; diff --git a/template/en/default/global/code-error.html.tmpl b/template/en/default/global/code-error.html.tmpl index 664958499..addde0d8a 100644 --- a/template/en/default/global/code-error.html.tmpl +++ b/template/en/default/global/code-error.html.tmpl @@ -304,7 +304,8 @@ [% ELSIF error == "jobqueue_no_job_mapping" %] Bugzilla::JobQueue has not been configured to handle the job "[% job FILTER html %]". You need to add this job type - to the JOB_MAP constant in Bugzilla::JobQueue. + to the JOB_MAP constant in Bugzilla::JobQueue, + perhaps by using the 'job_map' hook. [% ELSIF error == "ldap_bind_failed" %] Failed to bind to the LDAP server. The error message was: -- cgit v1.2.3-24-g4f1b