Browse files

Importer now has a database connection and finds next task, but doesn…

…'t do anything with it.
  • Loading branch information...
1 parent 238d4f2 commit 5a27cf3012d12a518d49b186d62be2220857676a Jonathan Fritz committed Sep 26, 2012
Showing with 45 additions and 4 deletions.
  1. +7 −1 musik/db.py
  2. +38 −3 musik/library/importer.py
View
8 musik/db.py
@@ -2,6 +2,7 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column
from sqlalchemy.types import String, Integer, DateTime
+from sqlalchemy.orm import sessionmaker
from datetime import datetime
import os, os.path
@@ -60,7 +61,12 @@ def __init__(self):
db_path = os.path.abspath(os.path.join(os.curdir, 'musik.db'))
self.sa_engine = create_engine('sqlite:///%s' % db_path, echo=True)
Base.metadata.create_all(self.sa_engine)
+ self.sa_sessionmaker = sessionmaker(bind=self.sa_engine)
# returns an initialized instance of sqlalchemy.engine.base.Engine
def getEngine(self):
- return self.sa_engine
+ return self.sa_engine
+
+ # returns a database session that the caller can use to execute queries and stuff
+ def getSession(self):
+ return self.sa_sessionmaker()
View
41 musik/library/importer.py
@@ -1,15 +1,50 @@
+import musik.db
+from musik.db import ImportTask
+
+from datetime import datetime
import threading
import time
class ImportThread(threading.Thread):
+ # whether or not the thread should continue to run
running = True
+ # database session
+ sa_session = None
+
def run(self):
- while self.running:
- print "still here"
- time.sleep(1)
+ try:
+ # get a database connection
+ db = musik.db.DB()
+ self.sa_session = db.getSession()
+
+ # process 'till you drop
+ while self.running:
+
+ # find the first unprocessed import task
+ task = self.sa_session.query(ImportTask).filter(ImportTask.started == None).order_by(ImportTask.created).first()
+ if task != None:
+ # start processing it
+ task.started = datetime.utcnow()
+ self.sa_session.commit()
+ print "%s is processing task %s" % (self.getName(), str(task))
+
+ # TODO: process the task
+
+ task.completed = datetime.utcnow()
+ self.sa_session.commit()
+ print "%s has finished processing task %s" % (self.getName(), str(task))
+
+ time.sleep(1)
+
+ finally:
+ # always clean up - your mom doesn't work here
+ self.sa_session.close()
+ self.sa_session = None
+
+ # cleans up the thread
def stop(self):
print "%s.stop has been called" % self.getName()
self.running = False

0 comments on commit 5a27cf3

Please sign in to comment.