]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Allow extensions to add new Jobs. r,a=mkanat.
authorGervase Markham <gerv@gerv.net>
Wed, 5 Jan 2011 11:48:49 +0000 (11:48 +0000)
committerGervase Markham <gerv@mozilla.org>
Wed, 5 Jan 2011 11:48:49 +0000 (11:48 +0000)
https://bugzilla.mozilla.org/show_bug.cgi?id=617012

Bugzilla/Hook.pm
Bugzilla/JobQueue.pm
Bugzilla/JobQueue/Runner.pm
extensions/Example/Extension.pm
template/en/default/global/code-error.html.tmpl

index f6fff241a56a43078792c4ebfcd4eea38a5b7806..b48ebb9faa786cd0543878bb540d0ca32a785514 100644 (file)
@@ -730,6 +730,23 @@ database when run.
 
 =back
 
+=head2 job_map
+
+Bugzilla has a system - L<Bugzilla::JobQueue> - 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<job_map> - 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<TheSchwartz::Worker>). 
+
+=back
+
 =head2 mailer_before_send
 
 Called right before L<Bugzilla::Mailer> sends a message to the MTA.
index 1046cf2c3aebcf6b5c0444cbddb3d3cdb6cb1211..2804b9179ad1784b25940ee5819704ca6c9bf8cf 100644 (file)
@@ -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);
index 8cfc965eb6f7f429fd9b94c226bb726298e10589..20cf6439f6504a02127e3a5523308336eaebeefb 100644 (file)
@@ -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);
     }
index 0fb56a32f32496c9b1797d2d53c1baf92cedbaf3..ab773561dc51e3aa7664c0dd22424d667d5056c2 100644 (file)
@@ -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) = @_;
     
index 6649584990a5fdf2451fb659e41721d3beca6578..addde0d8ab7b9421867c00e97f86e0dae5c04a21 100644 (file)
   [% ELSIF error == "jobqueue_no_job_mapping" %]
     <code>Bugzilla::JobQueue</code> has not been configured to handle
     the job "[% job FILTER html %]".  You need to add this job type
-    to the <code>JOB_MAP</code> constant in <code>Bugzilla::JobQueue</code>.
+    to the <code>JOB_MAP</code> constant in <code>Bugzilla::JobQueue</code>,
+    perhaps by using the 'job_map' hook.
 
   [% ELSIF error == "ldap_bind_failed" %]
     Failed to bind to the LDAP server. The error message was: