From c5eae90d75e7752c9d812f6b92d04c9179ebc590 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 24 Feb 2014 17:49:24 +0000 Subject: Assign job ids properly While 1 is a perfectly nice number, having every job get the same id is not entirely what is supposed to happen. --- lorry-controller-webapp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lorry-controller-webapp b/lorry-controller-webapp index aa62877..3b73527 100755 --- a/lorry-controller-webapp +++ b/lorry-controller-webapp @@ -156,9 +156,12 @@ class StateDB(object): c = self._conn.cursor() c.execute('BEGIN TRANSACTION') + # Table for holding the "are we scheduling jobs" value. c.execute('CREATE TABLE IF NOT EXISTS running_queue (running INT)') c.execute('INSERT INTO running_queue VALUES (0)') + # Table for all the known lorries (the "run queue"). + self.lorries_fields = [ ('path', 'TEXT PRIMARY KEY'), ('text', 'TEXT'), @@ -179,6 +182,11 @@ class StateDB(object): c.execute( 'CREATE TABLE IF NOT EXISTS lorries (%s)' % fields_sql) + # Table for the next available job id. + c.execute('CREATE TABLE IF NOT EXISTS next_job_id (job_id INT)') + c.execute('INSERT INTO next_job_id VALUES (1)') + + # Done. self._conn.commit() @property @@ -317,6 +325,16 @@ class StateDB(object): 'UPDATE lorries SET kill_job=? WHERE path=?', (value, path)) + def get_next_job_id(self): + logging.debug('StateDB.get_next_job_id called') + assert self.in_transaction + c = self.get_cursor() + c.execute('SELECT job_id FROM next_job_id') + row = c.fetchone() + job_id = row[0] + c.execute('UPDATE next_job_id SET job_id=?', (job_id + 1,)) + return job_id + class LorryControllerRoute(object): @@ -597,7 +615,7 @@ class GiveMeJob(LorryControllerRoute): for lorry_info in lorry_infos: if lorry_info['running_job'] is None: path = lorry_info['path'] - running_job = 1 + running_job = self.statedb.get_next_job_id() self.statedb.set_running_job(path, running_job) return { 'job_id': running_job, -- cgit v1.2.1