fix bug in queue preventing actions with null values from being purged
This commit is contained in:
parent
a1cfa5eee0
commit
60095b4668
|
@ -14,6 +14,7 @@
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
import traceback
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,7 +25,6 @@ class Queue(object):
|
||||||
DB_FILE = os.path.join(os.path.expanduser('~'), '.wakatime.db')
|
DB_FILE = os.path.join(os.path.expanduser('~'), '.wakatime.db')
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
exists = os.path.exists(self.DB_FILE)
|
|
||||||
conn = sqlite3.connect(self.DB_FILE)
|
conn = sqlite3.connect(self.DB_FILE)
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
c.execute('''CREATE TABLE IF NOT EXISTS action (
|
c.execute('''CREATE TABLE IF NOT EXISTS action (
|
||||||
|
@ -41,27 +41,34 @@ class Queue(object):
|
||||||
|
|
||||||
|
|
||||||
def push(self, data, plugin):
|
def push(self, data, plugin):
|
||||||
conn, c = self.connect()
|
try:
|
||||||
action = {
|
conn, c = self.connect()
|
||||||
'file': data.get('file'),
|
action = {
|
||||||
'time': data.get('time'),
|
'file': data.get('file'),
|
||||||
'project': data.get('project'),
|
'time': data.get('time'),
|
||||||
'language': data.get('language'),
|
'project': data.get('project'),
|
||||||
'lines': data.get('lines'),
|
'language': data.get('language'),
|
||||||
'branch': data.get('branch'),
|
'lines': data.get('lines'),
|
||||||
'is_write': 1 if data.get('is_write') else 0,
|
'branch': data.get('branch'),
|
||||||
'plugin': plugin,
|
'is_write': 1 if data.get('is_write') else 0,
|
||||||
}
|
'plugin': plugin,
|
||||||
c.execute('INSERT INTO action VALUES (:file,:time,:project,:language,:lines,:branch,:is_write,:plugin)', action)
|
}
|
||||||
conn.commit()
|
c.execute('INSERT INTO action VALUES (:file,:time,:project,:language,:lines,:branch,:is_write,:plugin)', action)
|
||||||
conn.close()
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
except sqlite3.Error, e:
|
||||||
|
log.error(str(e))
|
||||||
|
|
||||||
|
|
||||||
def pop(self):
|
def pop(self):
|
||||||
tries = 3
|
tries = 3
|
||||||
wait = 0.1
|
wait = 0.1
|
||||||
action = None
|
action = None
|
||||||
conn, c = self.connect()
|
try:
|
||||||
|
conn, c = self.connect()
|
||||||
|
except sqlite3.Error, e:
|
||||||
|
log.debug(traceback.format_exc())
|
||||||
|
return None
|
||||||
loop = True
|
loop = True
|
||||||
while loop and tries > -1:
|
while loop and tries > -1:
|
||||||
try:
|
try:
|
||||||
|
@ -69,9 +76,20 @@ class Queue(object):
|
||||||
c.execute('SELECT * FROM action LIMIT 1')
|
c.execute('SELECT * FROM action LIMIT 1')
|
||||||
row = c.fetchone()
|
row = c.fetchone()
|
||||||
if row is not None:
|
if row is not None:
|
||||||
c.execute('''DELETE FROM action WHERE
|
values = []
|
||||||
file=? AND time=? AND project=? AND language=? AND
|
clauses = []
|
||||||
lines=? AND branch=? AND is_write=?''', row[0:7])
|
index = 0
|
||||||
|
for row_name in ['file', 'time', 'project', 'language', 'lines', 'branch', 'is_write']:
|
||||||
|
if row[index] is not None:
|
||||||
|
clauses.append('{0}=?'.format(row_name))
|
||||||
|
values.append(row[index])
|
||||||
|
else:
|
||||||
|
clauses.append('{0} IS NULL'.format(row_name))
|
||||||
|
index += 1
|
||||||
|
if len(values) > 0:
|
||||||
|
c.execute('DELETE FROM action WHERE {0}'.format(u' AND '.join(clauses)), values)
|
||||||
|
else:
|
||||||
|
c.execute('DELETE FROM action WHERE {0}'.format(u' AND '.join(clauses)))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
if row is not None:
|
if row is not None:
|
||||||
action = {
|
action = {
|
||||||
|
@ -86,8 +104,11 @@ class Queue(object):
|
||||||
}
|
}
|
||||||
loop = False
|
loop = False
|
||||||
except sqlite3.Error, e:
|
except sqlite3.Error, e:
|
||||||
log.debug(str(e))
|
log.debug(traceback.format_exc())
|
||||||
sleep(wait)
|
sleep(wait)
|
||||||
tries -= 1
|
tries -= 1
|
||||||
conn.close()
|
try:
|
||||||
|
conn.close()
|
||||||
|
except sqlite3.Error, e:
|
||||||
|
log.debug(traceback.format_exc())
|
||||||
return action
|
return action
|
||||||
|
|
Loading…
Reference in New Issue