From 0422250ef29e1ec5cd0c0b581cfa15623b21d47c Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Thu, 11 Oct 2012 10:44:38 +0200
Subject: [PATCH] gnt-job: List archived jobs if requested
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

If requested via a filter or by including the β€œarchived” output,
archived jobs will be loaded and shown. This is significantly slower
than just listing normal jobs, therefore by default they are not loaded
at all.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/jqueue.py | 31 +++++++++++++++++++++++++------
 lib/query.py  |  4 ++++
 2 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/lib/jqueue.py b/lib/jqueue.py
index 0fad78df9..29896c088 100644
--- a/lib/jqueue.py
+++ b/lib/jqueue.py
@@ -1869,7 +1869,18 @@ class JobQueue(object):
                           "job-%s" % job_id)
 
   @staticmethod
-  def _GetJobIDsUnlocked(sort=True):
+  def _DetermineJobDirectories(archived):
+    result = [pathutils.QUEUE_DIR]
+
+    if archived:
+      archive_path = pathutils.JOB_QUEUE_ARCHIVE_DIR
+      result.extend(map(compat.partial(utils.PathJoin, archive_path),
+                        utils.ListVisibleFiles(archive_path)))
+
+    return result
+
+  @classmethod
+  def _GetJobIDsUnlocked(cls, sort=True, archived=False):
     """Return all known job IDs.
 
     The method only looks at disk because it's a requirement that all
@@ -1883,10 +1894,13 @@ class JobQueue(object):
 
     """
     jlist = []
-    for filename in utils.ListVisibleFiles(pathutils.QUEUE_DIR):
-      m = constants.JOB_FILE_RE.match(filename)
-      if m:
-        jlist.append(int(m.group(1)))
+
+    for path in cls._DetermineJobDirectories(archived):
+      for filename in utils.ListVisibleFiles(path):
+        m = constants.JOB_FILE_RE.match(filename)
+        if m:
+          jlist.append(int(m.group(1)))
+
     if sort:
       jlist.sort()
     return jlist
@@ -2425,6 +2439,11 @@ class JobQueue(object):
     qobj = query.Query(query.JOB_FIELDS, fields, qfilter=qfilter,
                        namefield="id")
 
+    # Archived jobs are only looked at if the "archived" field is referenced
+    # either as a requested field or in the filter. By default archived jobs
+    # are ignored.
+    include_archived = (query.JQ_ARCHIVED in qobj.RequestedData())
+
     job_ids = qobj.RequestedNames()
 
     list_all = (job_ids is None)
@@ -2432,7 +2451,7 @@ class JobQueue(object):
     if list_all:
       # Since files are added to/removed from the queue atomically, there's no
       # risk of getting the job ids in an inconsistent state.
-      job_ids = self._GetJobIDsUnlocked()
+      job_ids = self._GetJobIDsUnlocked(archived=include_archived)
 
     jobs = []
 
diff --git a/lib/query.py b/lib/query.py
index 01b434b00..d041ad5c2 100644
--- a/lib/query.py
+++ b/lib/query.py
@@ -101,6 +101,8 @@ from ganeti.constants import (QFT_UNKNOWN, QFT_TEXT, QFT_BOOL, QFT_NUMBER,
  CQ_QUEUE_DRAINED,
  CQ_WATCHER_PAUSE) = range(300, 303)
 
+(JQ_ARCHIVED, ) = range(400, 401)
+
 # Query field flags
 QFF_HOSTNAME = 0x01
 QFF_IP_ADDRESS = 0x02
@@ -2258,6 +2260,8 @@ def _BuildJobFields():
                 ("Current job priority (%s to %s)" %
                  (constants.OP_PRIO_LOWEST, constants.OP_PRIO_HIGHEST))),
      None, 0, _JobUnavail(lambda job: job.CalcPriority())),
+    (_MakeField("archived", "Archived", QFT_BOOL, "Whether job is archived"),
+     JQ_ARCHIVED, 0, lambda _, (job_id, job): job.archived),
     (_MakeField("ops", "OpCodes", QFT_OTHER, "List of all opcodes"),
      None, 0, _PerJobOp(lambda op: op.input.__getstate__())),
     (_MakeField("opresult", "OpCode_result", QFT_OTHER,
-- 
GitLab