Skip to content

Commit

Permalink
Merge pull request #27 from shad7/task/cleanup-dbapi
Browse files Browse the repository at this point in the history
[TASK] Database API access clean up
  • Loading branch information
shad7 committed Jun 8, 2015
2 parents a9f20e6 + cd00f79 commit 630dcaf
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 50 deletions.
12 changes: 5 additions & 7 deletions seedbox/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

cfg.CONF.import_group('database', 'seedbox.options')

_DBAPI = None
_DBAPI = {}


def _get_connection(conf):
Expand All @@ -31,9 +31,7 @@ def dbapi(conf=cfg.CONF):
:return: database API instance
:rtype: :class:`~seedbox.db.api.DBApi`
"""
global _DBAPI

if _DBAPI is None:
_DBAPI = api.DBApi(_get_connection(conf))
_DBAPI.shrink_db()
return _DBAPI
if not _DBAPI:
_DBAPI[DB_ENGINE_NAMESPACE] = api.DBApi(_get_connection(conf))
_DBAPI[DB_ENGINE_NAMESPACE].shrink_db()
return _DBAPI[DB_ENGINE_NAMESPACE]
4 changes: 2 additions & 2 deletions seedbox/process/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def _get_work(dbapi):

# search through the directory of torrents and load any
# new ones into the cache.
loader.load_torrents()
loader.load_torrents(dbapi)

flows = []
# now retrieve any torrents that are eligible for processing
Expand All @@ -28,7 +28,7 @@ def _get_work(dbapi):
tor.torrent_id, tor.media_files)
if tor.media_files:
LOG.debug('creating workflow for torrent: %s', tor)
flows.append(workflow.Workflow(tor))
flows.append(workflow.Workflow(dbapi, tor))

return flows

Expand Down
7 changes: 4 additions & 3 deletions seedbox/process/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import xworkflows

from seedbox import constants
from seedbox import db

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -48,15 +47,17 @@ class BaseFlow(xworkflows.WorkflowEnabled):
On binding plugin tasks to each step and determining which plugin
is capable of operating on the media files of the specified torrent.
:param dbapi: an instance of the database API
:type dbapi: :class:`~seedbox.db.api.DBApi`
:param torrent: an instance of a parsed torrent metadata
:type torrent: :class:`~seedbox.db.models.Torrent`
"""
state = Taskflow()

def __init__(self, torrent):
def __init__(self, dbapi, torrent):
super(BaseFlow, self).__init__()
self.dbapi = dbapi
self.torrent = torrent
self.dbapi = db.dbapi()

# if we failed in the middle of the flow last time
# we need to start from where we left off
Expand Down
2 changes: 1 addition & 1 deletion seedbox/process/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def run(self):
except (flow.WorkflowError, flow.AbortTransition,
flow.InvalidTransitionError,
flow.ForbiddenTransition) as wferr:
LOG.error('workflow error: %s', wferr)
LOG.exception('workflow error:')
self.torrent = self.dbapi.get_torrent(self.torrent.torrent_id)
self.torrent.error_msg = str(wferr)
self.torrent.failed = True
Expand Down
2 changes: 1 addition & 1 deletion seedbox/tests/db/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ApiDBTestCase(test.ConfiguredBaseTestCase):
def setUp(self):
super(ApiDBTestCase, self).setUp()

self.patch(db, '_DBAPI', None)
self.patch(db, '_DBAPI', {})
self.dbapi = db.dbapi(self.CONF)

def test_clear(self):
Expand Down
4 changes: 2 additions & 2 deletions seedbox/tests/process/test_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class FlowTestCase(test.ConfiguredBaseTestCase):
def setUp(self):
super(FlowTestCase, self).setUp()

self.patch(db, '_DBAPI', None)
self.patch(db, '_DBAPI', {})
self.dbapi = db.dbapi(self.CONF)

self.torrent = self.dbapi.save_torrent(
Expand Down Expand Up @@ -55,7 +55,7 @@ def test_base_flow(self):
))
self.dbapi.bulk_create_medias(_medias)

wf = flow.BaseFlow(self.torrent)
wf = flow.BaseFlow(self.dbapi, self.torrent)

tasks = wf.next_tasks()
self.assertEqual(len(list(tasks)), 2)
2 changes: 1 addition & 1 deletion seedbox/tests/process/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ProcessTestCase(test.ConfiguredBaseTestCase):
def setUp(self):
super(ProcessTestCase, self).setUp()

self.patch(db, '_DBAPI', None)
self.patch(db, '_DBAPI', {})
self.dbapi = db.dbapi(self.CONF)

def test_process_no_work(self):
Expand Down
8 changes: 4 additions & 4 deletions seedbox/tests/process/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class WorkflowTestCase(test.ConfiguredBaseTestCase):
def setUp(self):
super(WorkflowTestCase, self).setUp()

self.patch(db, '_DBAPI', None)
self.patch(db, '_DBAPI', {})
self.dbapi = db.dbapi(self.CONF)

self.torrent = self.dbapi.save_torrent(
models.Torrent(torrent_id=None,
name='fake1.torrent'))

def test_workflow(self):
wf = workflow.Workflow(self.torrent)
wf = workflow.Workflow(self.dbapi, self.torrent)
status = wf.run()
self.assertFalse(status)
status = wf.run()
Expand All @@ -29,7 +29,7 @@ def test_workflow(self):

def test_cancelled(self):
self.torrent.state = 'cancelled'
wf = workflow.Workflow(self.torrent)
wf = workflow.Workflow(self.dbapi, self.torrent)
status = wf.run()
self.assertTrue(status)

Expand All @@ -48,6 +48,6 @@ def test_stop_workflow(self):
error_msg='bad thing happened')
media = self.dbapi.save_media(media)

wf = workflow.Workflow(torrent)
wf = workflow.Workflow(self.dbapi, torrent)
status = wf.run()
self.assertTrue(status)
10 changes: 7 additions & 3 deletions seedbox/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

class CliTestCase(test.ConfiguredBaseTestCase):

@mock.patch.object(db, '_DBAPI')
@mock.patch.object(cli.service, 'prepare_service')
def test_cli(self, mock_db, mock_service):
def setUp(self):
super(CliTestCase, self).setUp()

self.patch(db, '_DBAPI', {})

@mock.patch('seedbox.service.prepare_service')
def test_cli(self, mock_service):
self.assertIsNone(cli.main())
33 changes: 14 additions & 19 deletions seedbox/tests/torrent/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,72 +31,67 @@ def setUp(self):
torrent_path,
group='torrent')

self.patch(db, '_DBAPI', {})
self.dbapi = db.dbapi(self.CONF)

def test_load_torrents(self):
self.patch(db, '_DBAPI', None)
loader.load_torrents()
loader.load_torrents(self.dbapi)

def test_load_torrents_still_downloading(self):
self.patch(db, '_DBAPI', None)

def _is_torrent_downloading(media_items):
return True

self.patch(loader, '_is_torrent_downloading', _is_torrent_downloading)
loader.load_torrents()
loader.load_torrents(self.dbapi)

def test_load_torrents_parse_error(self):
self.patch(db, '_DBAPI', None)

class TorrentParser(object):

def __init__(self, tfile):
raise parser.ParsingError('failed to parse')

self.patch(parser, 'TorrentParser', TorrentParser)
loader.load_torrents()
loader.load_torrents(self.dbapi)

def test_load_torrents_no_parse(self):
self.patch(db, '_DBAPI', None)
dbapi = db.dbapi(self.CONF)
for tor in glob.glob(os.path.join(torrent_path, '*.torrent')):
_tor = models.Torrent.make_empty()
_tor.name = os.path.basename(tor)
_tor.invalid = True
dbapi.save_torrent(_tor)
self.dbapi.save_torrent(_tor)

loader.load_torrents()
loader.load_torrents(self.dbapi)

def test_parsing_required(self):
self.patch(db, '_DBAPI', None)
dbapi = db.dbapi(self.CONF)

_tor = models.Torrent.make_empty()
_tor.name = 'preq-check'
torrent = dbapi.save_torrent(_tor)
torrent = self.dbapi.save_torrent(_tor)
result = loader._is_parsing_required(torrent)
self.assertTrue(result)

torrent.invalid = True
torrent = dbapi.save_torrent(torrent)
torrent = self.dbapi.save_torrent(torrent)
result = loader._is_parsing_required(torrent)
self.assertFalse(result)

torrent.invalid = False
torrent.purged = True
torrent = dbapi.save_torrent(torrent)
torrent = self.dbapi.save_torrent(torrent)
result = loader._is_parsing_required(torrent)
self.assertFalse(result)

torrent.purged = False
torrent = dbapi.save_torrent(torrent)
torrent = self.dbapi.save_torrent(torrent)

_media = models.MediaFile.make_empty()
_media.filename = 'media'
_media.file_ext = '.mp4'
_media.torrent_id = torrent.torrent_id
dbapi.save_media(_media)
self.dbapi.save_media(_media)

torrent = dbapi.get_torrent(torrent.torrent_id)
torrent = self.dbapi.get_torrent(torrent.torrent_id)
result = loader._is_parsing_required(torrent)
self.assertFalse(result)

Expand Down
10 changes: 3 additions & 7 deletions seedbox/torrent/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from seedbox.common import tools
from seedbox import constants
from seedbox import db
from seedbox.db import models
from seedbox.torrent import parser

Expand All @@ -22,16 +21,14 @@
VIDEO_TYPES = tools.format_file_ext(cfg.CONF.torrent.video_filetypes)


def load_torrents():
def load_torrents(dbapi):
"""Loads torrents into database.
Find all the torrents in the specified directory, verify it is a valid
torrent file (via parsing) and capture the relevant details. Next create
a record in the cache for each torrent.
"""

dbapi = db.dbapi()

for torrent_file in glob.glob(os.path.join(cfg.CONF.torrent.torrent_path,
'*.torrent')):

Expand All @@ -43,12 +40,11 @@ def load_torrents():

try:
torparser = parser.TorrentParser(torrent_file)
except parser.ParsingError as ape:
except parser.ParsingError:
torrent.invalid = True
torrent.state = constants.CANCELLED
dbapi.save_torrent(torrent)
LOG.error('Torrent Parsing Error: [%s] [%s]',
torrent_file, ape)
LOG.exception('Torrent Parsing Error: [%s]', torrent_file)
continue

media_items = torparser.get_files_details()
Expand Down

0 comments on commit 630dcaf

Please sign in to comment.