4 # Copyright (C) 2006, 2007 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Module implementing the job queue handling."""
29 from ganeti import constants
30 from ganeti import errors
31 from ganeti import utils
34 def _ReadNumericFile(file_name):
35 """Reads a file containing a number.
38 @return: None if file is not found, otherwise number
42 fd = open(file_name, "r")
44 if err.errno in (errno.ENOENT, ):
49 return int(fd.read(128))
55 """Read the serial file.
57 The queue should be locked while this function is called.
60 return _ReadNumericFile(constants.JOB_QUEUE_SERIAL_FILE)
64 """Read the queue version.
66 The queue should be locked while this function is called.
69 return _ReadNumericFile(constants.JOB_QUEUE_VERSION_FILE)
72 def InitAndVerifyQueue(exclusive):
73 """Open and lock job queue.
75 If necessary, the queue is automatically initialized.
78 @param exclusive: Whether to lock the queue in exclusive mode. Shared
80 @rtype: utils.FileLock
81 @return: Lock object for the queue. This can be used to change the
85 # Make sure our directories exists
86 for path in (constants.QUEUE_DIR, constants.JOB_QUEUE_ARCHIVE_DIR):
90 if err.errno not in (errno.EEXIST, ):
94 queue_lock = utils.FileLock(constants.JOB_QUEUE_LOCK_FILE)
96 # Determine locking function and call it
98 fn = queue_lock.Exclusive
100 fn = queue_lock.Shared
105 version = ReadVersion()
107 # Write new version file
108 utils.WriteFile(constants.JOB_QUEUE_VERSION_FILE,
109 data="%s\n" % constants.JOB_QUEUE_VERSION)
112 version = ReadVersion()
114 if version != constants.JOB_QUEUE_VERSION:
115 raise errors.JobQueueError("Found job queue version %s, expected %s",
116 version, constants.JOB_QUEUE_VERSION)
118 serial = ReadSerial()
120 # Write new serial file
121 utils.WriteFile(constants.JOB_QUEUE_SERIAL_FILE,
125 serial = ReadSerial()
128 # There must be a serious problem
129 raise errors.JobQueueError("Can't read/parse the job queue serial file")