From 755bc194dcea3481fa41b5884a98a5aa086fe09e Mon Sep 17 00:00:00 2001 From: Israel Madueme Date: Thu, 5 Apr 2018 16:23:43 -0400 Subject: Bug 1449282 - add jobqueue_status api --- Bugzilla/WebService/Bugzilla.pm | 38 ++++++++++++++++++++++ Bugzilla/WebService/Constants.pm | 3 ++ .../WebService/Server/REST/Resources/Bugzilla.pm | 5 +++ docs/en/rst/api/core/v1/bugzilla.rst | 29 +++++++++++++++++ template/en/default/global/code-error.html.tmpl | 4 +++ 5 files changed, 79 insertions(+) diff --git a/Bugzilla/WebService/Bugzilla.pm b/Bugzilla/WebService/Bugzilla.pm index 0d0393c28..145502445 100644 --- a/Bugzilla/WebService/Bugzilla.pm +++ b/Bugzilla/WebService/Bugzilla.pm @@ -13,7 +13,10 @@ use warnings; use base qw(Bugzilla::WebService); use Bugzilla::Constants; +use Bugzilla::Error; +use Bugzilla::Logging; use Bugzilla::Util qw(datetime_from); +use Try::Tiny; use DateTime; @@ -28,6 +31,7 @@ use constant READ_ONLY => qw( timezone time version + jobqueue_status ); use constant PUBLIC_METHODS => qw( @@ -35,6 +39,7 @@ use constant PUBLIC_METHODS => qw( time timezone version + jobqueue_status ); sub version { @@ -80,6 +85,39 @@ sub time { }; } +sub jobqueue_status { + my ( $self, $params ) = @_; + + Bugzilla->login(LOGIN_REQUIRED); + + my $dbh = Bugzilla->dbh; + my $query = q{ + SELECT + COUNT(*) AS total, + COALESCE( + (SELECT COUNT(*) + FROM ts_error + WHERE ts_error.jobid = j.jobid + ) + , 0) AS errors + FROM ts_job j + INNER JOIN ts_funcmap f + ON f.funcid = j.funcid; + }; + + my $status; + try { + $status = $dbh->selectrow_hashref($query); + $status->{errors} = 0 + $status->{errors}; + $status->{total} = 0 + $status->{total}; + } catch { + ERROR($_); + ThrowCodeError('jobqueue_status_error'); + }; + + return $status; +} + 1; __END__ diff --git a/Bugzilla/WebService/Constants.pm b/Bugzilla/WebService/Constants.pm index 93fddfc2b..71435c13a 100644 --- a/Bugzilla/WebService/Constants.pm +++ b/Bugzilla/WebService/Constants.pm @@ -199,6 +199,9 @@ use constant WS_ERROR_CODE => { # BugUserLastVisited errors user_not_involved => 1300, + # Job queue errors 1400-1500 + jobqueue_status_error => 1400, + # Errors thrown by the WebService itself. The ones that are negative # conform to http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php xmlrpc_invalid_value => -32600, diff --git a/Bugzilla/WebService/Server/REST/Resources/Bugzilla.pm b/Bugzilla/WebService/Server/REST/Resources/Bugzilla.pm index a8f3f9330..646355cd3 100644 --- a/Bugzilla/WebService/Server/REST/Resources/Bugzilla.pm +++ b/Bugzilla/WebService/Server/REST/Resources/Bugzilla.pm @@ -49,6 +49,11 @@ sub _rest_resources { GET => { method => 'parameters' } + }, + qr{^/jobqueue_status$}, { + GET => { + method => 'jobqueue_status' + } } ]; return $rest_resources; diff --git a/docs/en/rst/api/core/v1/bugzilla.rst b/docs/en/rst/api/core/v1/bugzilla.rst index 2dd40e0cb..e43472c30 100644 --- a/docs/en/rst/api/core/v1/bugzilla.rst +++ b/docs/en/rst/api/core/v1/bugzilla.rst @@ -300,3 +300,32 @@ name type description =============== ====== ==================================================== last_audit_time string The maximum of the at_time from the audit_log. =============== ====== ==================================================== + +Job Queue Status +---------------- + +Reports the status of the job queue. + +**Request** + +.. code-block:: text + + GET /rest/jobqueue_status + +This method requires an authenticated user. + +**Response** + +.. code-block:: js + + { + "total": 12, + "errors": 0 + } + +=============== ======= ==================================================== +name type description +=============== ======= ==================================================== +total integer The total number of jobs in the job queue. +errors integer The number of errors produced by jobs in the queue. +=============== ======= ==================================================== \ No newline at end of file diff --git a/template/en/default/global/code-error.html.tmpl b/template/en/default/global/code-error.html.tmpl index cf32548b6..bf1ff5ad3 100644 --- a/template/en/default/global/code-error.html.tmpl +++ b/template/en/default/global/code-error.html.tmpl @@ -313,6 +313,10 @@ to the JOB_MAP constant in Bugzilla::JobQueue, perhaps by using the 'job_map' hook. + [% ELSIF error == "jobqueue_status_error" %] + An error occurred while checking the job queue status. Try again at a + later time. + [% ELSIF error == "ldap_bind_failed" %] Failed to bind to the LDAP server. The error message was: [% errstr FILTER html %] -- cgit v1.2.3-24-g4f1b