Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | # Copyright 2013 Linaro Limited |
| 4 | # Author Matt Hart <matthew.hart@linaro.org> |
| 5 | # |
| 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. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 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, |
| 19 | # MA 02110-1301, USA. |
| 20 | |
| 21 | import logging |
| 22 | import psycopg2 |
| 23 | import time |
| 24 | |
| 25 | class DBHandler(object): |
| 26 | def __init__(self, config): |
| 27 | logging.debug("Creating new DBHandler: %s" % config["dbhost"]) |
| 28 | logging.getLogger().name = "DBHandler" |
| 29 | self.conn = psycopg2.connect(database=config["dbname"], user=config["dbuser"], |
| 30 | password=config["dbpass"], host=config["dbhost"]) |
| 31 | self.cursor = self.conn.cursor() |
| 32 | |
| 33 | def do_sql(self, sql): |
| 34 | logging.debug("executing sql: %s" % sql) |
| 35 | self.cursor.execute(sql) |
| 36 | self.conn.commit() |
| 37 | |
| 38 | def do_sql_with_fetch(self, sql): |
| 39 | logging.debug("executing sql: %s" % sql) |
| 40 | self.cursor.execute(sql) |
| 41 | row = self.cursor.fetchone() |
| 42 | self.conn.commit() |
| 43 | return row |
| 44 | |
Matt Hart | fc64682 | 2015-02-27 14:51:16 +0000 | [diff] [blame^] | 45 | def create_db(self): |
| 46 | logging.info("Creating db table if it doesn't exist") |
| 47 | sql = "create table if not exists pdu_queue (id serial, hostname " \ |
| 48 | "text, port int, request text, exectime int)" |
| 49 | self.cursor.execute(sql) |
| 50 | self.conn.commit() |
| 51 | sql = "select column_name from information_schema.columns where " \ |
| 52 | "table_name='pdu_queue' and column_name='exectime'" |
| 53 | self.cursor.execute(sql) |
| 54 | res = self.cursor.fetchone() |
| 55 | if not res: |
| 56 | logging.info("Old db schema discovered, upgrading") |
| 57 | sql = "alter table pdu_queue add column exectime int default 1" |
| 58 | self.cursor.execute(sql) |
| 59 | self.conn.commit() |
| 60 | |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 61 | def delete_row(self, row_id): |
| 62 | logging.debug("deleting row %i" % row_id) |
| 63 | self.do_sql("delete from pdu_queue where id=%i" % row_id) |
| 64 | |
| 65 | def get_res(self, sql): |
| 66 | return self.cursor.execute(sql) |
| 67 | |
| 68 | def get_next_job(self): |
| 69 | now = int(time.time()) |
Matt Hart | 132d43c | 2015-02-25 18:19:53 +0000 | [diff] [blame] | 70 | row = self.do_sql_with_fetch("select id,hostname,port,request from pdu_queue where (exectime < %i or exectime is null) order by id asc limit 1" % now) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 71 | return row |
| 72 | |
| 73 | def close(self): |
| 74 | logging.debug("Closing DBHandler") |
| 75 | self.cursor.close() |
| 76 | self.conn.close() |