From: Michael Tremer Date: Fri, 29 Mar 2013 12:55:31 +0000 (+0100) Subject: hub: Add functions to query job lists. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ba9f9092255b2d4ada39616823f22ee72d6d1fb1;p=pbs.git hub: Add functions to query job lists. --- diff --git a/hub/__init__.py b/hub/__init__.py index 683eb84b..8b2f7650 100644 --- a/hub/__init__.py +++ b/hub/__init__.py @@ -56,6 +56,9 @@ class Application(tornado.web.Application): (r"/builders/keepalive", handlers.BuildersKeepaliveHandler), # Jobs + (r"/jobs/active", handlers.JobsGetActiveHandler), + (r"/jobs/latest", handlers.JobsGetLatestHandler), + (r"/jobs/queue", handlers.JobsGetQueueHandler), (r"/jobs/(.*)", handlers.JobsGetHandler), # Packages diff --git a/hub/handlers.py b/hub/handlers.py index d8ba58c9..db1ec05e 100644 --- a/hub/handlers.py +++ b/hub/handlers.py @@ -337,25 +337,11 @@ class BuildsGetHandler(BaseHandler): # Jobs -class JobsGetHandler(BaseHandler): - def get(self, job_uuid): - job = self.backend.jobs.get_by_uuid(job_uuid) - if not job: - raise tornado.web.HTTPError(404, "Could not find job: %s" % job_uuid) - - # Check if user is allowed to view this job. - if job.build.public == False: - if not self.user: - raise tornado.web.HTTPError(401) - - # Check if an authenticated user has permission to see this build. - if not job.build.has_perm(self.user): - raise tornado.web.HTTPError(403) - +class JobsBaseHandler(BaseHandler): + def job2json(self, job): ret = { "arch" : job.arch.name, "build" : job.build.uuid, - "builder" : job.builder.hostname, "duration" : job.duration, "name" : job.name, "packages" : [p.uuid for p in job.packages], @@ -365,12 +351,74 @@ class JobsGetHandler(BaseHandler): "uuid" : job.uuid, } + if job.builder: + ret["builder"] = job.builder.hostname + if job.time_started: ret["time_started"] = job.time_started.isoformat() if job.time_finished: ret["time_finished"] = job.time_finished.isoformat() + return ret + + +class JobsGetActiveHandler(JobsBaseHandler): + def get(self): + # Get list of all active jobs. + jobs = self.backend.jobs.get_active() + + args = { + "jobs" : [self.job2json(j) for j in jobs], + } + + self.finish(args) + + +class JobsGetLatestHandler(JobsBaseHandler): + def get(self): + limit = self.get_argument_int("limit", 5) + + # Get the latest jobs. + jobs = self.backend.jobs.get_latest(age="24 HOUR", limit=limit) + + args = { + "jobs" : [self.job2json(j) for j in jobs], + } + + self.finish(args) + + +class JobsGetQueueHandler(JobsBaseHandler): + def get(self): + limit = self.get_argument_int("limit", 5) + + # Get the job queue. + jobs = self.backend.jobs.get_next(limit=limit) + + args = { + "jobs" : [self.job2json(j) for j in jobs], + } + + self.finish(args) + + +class JobsGetHandler(JobsBaseHandler): + def get(self, job_uuid): + job = self.backend.jobs.get_by_uuid(job_uuid) + if not job: + raise tornado.web.HTTPError(404, "Could not find job: %s" % job_uuid) + + # Check if user is allowed to view this job. + if job.build.public == False: + if not self.user: + raise tornado.web.HTTPError(401) + + # Check if an authenticated user has permission to see this build. + if not job.build.has_perm(self.user): + raise tornado.web.HTTPError(403) + + ret = self.job2json(job) self.finish(ret)