summaryrefslogtreecommitdiff
path: root/lorry-controller-webapp
diff options
context:
space:
mode:
Diffstat (limited to 'lorry-controller-webapp')
-rwxr-xr-xlorry-controller-webapp37
1 files changed, 26 insertions, 11 deletions
diff --git a/lorry-controller-webapp b/lorry-controller-webapp
index c01bf37..efd7cfa 100755
--- a/lorry-controller-webapp
+++ b/lorry-controller-webapp
@@ -85,6 +85,7 @@ class StateDB(object):
'path TEXT PRIMARY KEY, '
'text TEXT, '
'generated INT, '
+ 'running_job INT, '
'due INT, '
'interval INT '
')')
@@ -110,11 +111,11 @@ class StateDB(object):
self._conn.commit()
def get_lorry_info(self, path):
- logging.debug('StateDB.get_lorry_info called, path=%r', path)
+ logging.debug('StateDB.get_lorry_info(path=%r) called', path)
self._open()
c = self._conn.cursor()
c.execute(
- 'SELECT path, text, generated, due, interval '
+ 'SELECT path, text, generated, due, interval, running_job '
'FROM lorries WHERE path IS ?',
(path,))
row = c.fetchone()
@@ -126,6 +127,7 @@ class StateDB(object):
'generated': row[2],
'due': row[3],
'interval': row[4],
+ 'running_job': row[5],
}
def get_all_lorries_info(self):
@@ -133,7 +135,8 @@ class StateDB(object):
self._open()
c = self._conn.cursor()
c.execute(
- 'SELECT path, text, generated, due, interval FROM lorries '
+ 'SELECT path, text, generated, due, interval, running_job '
+ 'FROM lorries '
'ORDER BY due')
return [
{
@@ -142,6 +145,7 @@ class StateDB(object):
'generated': row[2],
'due': row[3],
'interval': row[4],
+ 'running_job': row[5],
}
for row in c.fetchall()
]
@@ -176,9 +180,9 @@ class StateDB(object):
c.execute('BEGIN TRANSACTION')
c.execute(
'INSERT OR REPLACE INTO lorries '
- '(path, text, generated, due, interval) '
- 'VALUES (?, ?, ?, ?, ?)',
- (path, text, generated, due, interval))
+ '(path, text, generated, due, interval, running_job) '
+ 'VALUES (?, ?, ?, ?, ?, ?)',
+ (path, text, generated, due, interval, None))
self._conn.commit()
def remove_lorry(self, path):
@@ -189,6 +193,15 @@ class StateDB(object):
c.execute('DELETE FROM lorries WHERE path IS ?', (path,))
self._conn.commit()
+ def set_running_job(self, path, job_id):
+ logging.debug(
+ 'StateDB.set_running_job(%r, %r) called', path, job_id)
+ self._open()
+ c = self._conn.cursor()
+ c.execute(
+ 'UPDATE lorries SET running_job=? WHERE path=?',
+ (job_id, path))
+
class LorryControllerRoute(object):
@@ -429,11 +442,13 @@ class GiveMeJob(LorryControllerRoute):
def run(self, **kwargs):
logging.debug('%s %s called', self.http_method, self.path)
- paths = self.statedb.get_lorries_paths()
- if paths:
- return { 'job-id': paths[0] }
- else:
- return { 'job-id': None }
+ lorry_infos = self.statedb.get_all_lorries_info()
+ for lorry_info in lorry_infos:
+ if lorry_info['running_job'] is None:
+ path = lorry_info['path']
+ self.statedb.set_running_job(path, 1)
+ return { 'job-id': path}
+ return { 'job-id': None }
class WEBAPP(cliapp.Application):