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 | |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame^] | 25 | |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 26 | class DBHandler(object): |
| 27 | def __init__(self, config): |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame^] | 28 | logging.debug("Creating new DBHandler: %s", config["dbhost"]) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 29 | logging.getLogger().name = "DBHandler" |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame^] | 30 | self.conn = psycopg2.connect(database=config["dbname"], |
| 31 | user=config["dbuser"], |
| 32 | password=config["dbpass"], |
| 33 | host=config["dbhost"]) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 34 | self.cursor = self.conn.cursor() |
| 35 | |
| 36 | def do_sql(self, sql): |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame^] | 37 | logging.debug("executing sql: %s", sql) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 38 | self.cursor.execute(sql) |
| 39 | self.conn.commit() |
| 40 | |
| 41 | def do_sql_with_fetch(self, sql): |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame^] | 42 | logging.debug("executing sql: %s", sql) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 43 | self.cursor.execute(sql) |
| 44 | row = self.cursor.fetchone() |
| 45 | self.conn.commit() |
| 46 | return row |
| 47 | |
Matt Hart | fc64682 | 2015-02-27 14:51:16 +0000 | [diff] [blame] | 48 | def create_db(self): |
| 49 | logging.info("Creating db table if it doesn't exist") |
| 50 | sql = "create table if not exists pdu_queue (id serial, hostname " \ |
| 51 | "text, port int, request text, exectime int)" |
| 52 | self.cursor.execute(sql) |
| 53 | self.conn.commit() |
| 54 | sql = "select column_name from information_schema.columns where " \ |
| 55 | "table_name='pdu_queue' and column_name='exectime'" |
| 56 | self.cursor.execute(sql) |
| 57 | res = self.cursor.fetchone() |
| 58 | if not res: |
| 59 | logging.info("Old db schema discovered, upgrading") |
| 60 | sql = "alter table pdu_queue add column exectime int default 1" |
| 61 | self.cursor.execute(sql) |
| 62 | self.conn.commit() |
| 63 | |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 64 | def delete_row(self, row_id): |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame^] | 65 | logging.debug("deleting row %i", row_id) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 66 | self.do_sql("delete from pdu_queue where id=%i" % row_id) |
| 67 | |
| 68 | def get_res(self, sql): |
| 69 | return self.cursor.execute(sql) |
| 70 | |
| 71 | def get_next_job(self): |
| 72 | now = int(time.time()) |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame^] | 73 | row = self.do_sql_with_fetch("select id, hostname, port, " |
| 74 | "request from pdu_queue where " |
| 75 | "(exectime < %i or exectime is null)" |
| 76 | "order by id asc limit 1" % now) |
Matt Hart | 76fb254 | 2014-06-01 14:24:56 +0100 | [diff] [blame] | 77 | return row |
| 78 | |
| 79 | def close(self): |
| 80 | logging.debug("Closing DBHandler") |
| 81 | self.cursor.close() |
Matt Hart | fe04221 | 2015-07-09 14:13:29 +0100 | [diff] [blame^] | 82 | self.conn.close() |