jqueue.py 3.43 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#
#

# Copyright (C) 2006, 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


"""Module implementing the job queue handling."""

import threading
import Queue

from ganeti import opcodes
Iustin Pop's avatar
Iustin Pop committed
28
from ganeti import errors
Iustin Pop's avatar
Iustin Pop committed
29 30 31 32 33 34 35 36 37 38 39 40

class JobObject:
  """In-memory job representation.

  This is what we use to track the user-submitted jobs (which are of
  class opcodes.Job).

  """
  def __init__(self, jid, jdesc):
    self.data = jdesc
    jdesc.status = opcodes.Job.STATUS_PENDING
    jdesc.job_id = jid
41 42
    jdesc.op_status = [opcodes.Job.STATUS_PENDING for i in jdesc.op_list]
    jdesc.op_result = [None for i in jdesc.op_list]
Iustin Pop's avatar
Iustin Pop committed
43 44 45 46 47 48
    self.lock = threading.Lock()

  def SetStatus(self, status, result=None):
    self.lock.acquire()
    self.data.status = status
    if result is not None:
Guido Trotter's avatar
Guido Trotter committed
49
      self.data.op_result = result
Iustin Pop's avatar
Iustin Pop committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    self.lock.release()

  def GetData(self):
    self.lock.acquire()
    #FIXME(iustin): make a deep copy of result
    result = self.data
    self.lock.release()
    return result


class QueueManager:
  """Example queue implementation.

  """
  def __init__(self):
    self.job_queue = {}
    self.jid = 1
    self.lock = threading.Lock()
    self.new_queue = Queue.Queue()

  def put(self, item):
    """Add a new job to the queue.

    This enters the job into our job queue and also puts it on the new
    queue, in order for it to be picked up by the queue processors.

    """
    self.lock.acquire()
    try:
      rid = self.jid
      self.jid += 1
      job = JobObject(rid, item)
      self.job_queue[rid] = job
    finally:
      self.lock.release()
    self.new_queue.put(job)
    return rid

  def query(self, rid):
    """Query a given job ID.

    """
    self.lock.acquire()
    result = self.job_queue.get(rid, None)
    self.lock.release()
    return result
Iustin Pop's avatar
Iustin Pop committed
96 97 98 99 100 101 102 103 104 105

  def query_jobs(self, fields, names):
    """Query all jobs.

    The fields and names parameters are similar to the ones passed to
    the OpQueryInstances.

    """
    result = []
    self.lock.acquire()
Iustin Pop's avatar
Iustin Pop committed
106 107 108 109
    if names:
      values = [self.job_queue[j_id] for j_id in names]
    else:
      values = self.job_queue.itervalues()
Iustin Pop's avatar
Iustin Pop committed
110
    try:
Iustin Pop's avatar
Iustin Pop committed
111
      for jobj in values:
Iustin Pop's avatar
Iustin Pop committed
112 113 114 115 116 117 118
        row = []
        jdata = jobj.data
        for fname in fields:
          if fname == "id":
            row.append(jdata.job_id)
          elif fname == "status":
            row.append(jdata.status)
119 120 121 122 123 124
          elif fname == "op_list":
            row.append([op.__getstate__() for op in jdata.op_list])
          elif fname == "op_status":
            row.append(jdata.op_status)
          elif fname == "op_result":
            row.append(jdata.op_result)
Iustin Pop's avatar
Iustin Pop committed
125 126 127 128 129 130 131
          else:
            raise errors.OpExecError("Invalid job query field '%s'" %
                                           fname)
        result.append(row)
    finally:
      self.lock.release()
    return result