-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from colab/refactor-data-import
Refactor data import
- Loading branch information
Showing
43 changed files
with
685 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +0,0 @@ | ||
|
||
import os | ||
|
||
from django.core.management import ManagementUtility | ||
|
||
from .initconfig import initconfig | ||
|
||
|
||
def execute_from_command_line(argv=None): | ||
""" | ||
A simple method that runs a ManagementUtility. | ||
""" | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "colab.settings") | ||
|
||
utility = ManagementUtility(argv) | ||
utility.execute() | ||
|
||
|
||
def run_colab_config(argv=None): | ||
initconfig() | ||
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
from celery.bin import celery | ||
|
||
from colab.celery import app | ||
from colab.queue.command import CeleryCommand | ||
|
||
base = celery.CeleryCommand(app=app) | ||
|
||
|
||
# this is a reimplementation of the djcelery 'celery' command | ||
# taken from Sentry | ||
class Command(CeleryCommand): | ||
"""The celery command.""" | ||
help = 'celery commands, see celery help' | ||
options = (CeleryCommand.options | ||
+ base.get_options() | ||
+ base.preload_options) | ||
|
||
def run_from_argv(self, argv): | ||
argv = self.handle_default_options(argv) | ||
if self.requires_system_checks: | ||
self.validate() | ||
base.execute_from_commandline( | ||
['{0[0]} {0[1]}'.format(argv)] + argv[2:], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
from django.conf import settings | ||
|
||
|
||
def get_plugin_config(app_label): | ||
return settings.COLAB_APPS.get('gitlab', {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
from .base_importer import PluginDataImporter # noqa | ||
from .tasks import TASKS, data_import, register_tasks # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
import abc | ||
|
||
from django.conf import settings | ||
|
||
|
||
class PluginDataImporter(object): | ||
|
||
def __init__(self): | ||
self.config = settings.COLAB_APPS.get(self.app_label, {}) | ||
|
||
@abc.abstractmethod | ||
def fetch_data(self): | ||
raise NotImplementedError | ||
fetch_data.is_abstract = True | ||
|
||
@abc.abstractmethod | ||
def app_label(self): | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env python | ||
|
||
import importlib | ||
import logging | ||
|
||
import redis | ||
|
||
from django.conf import settings | ||
|
||
from colab.celery import app | ||
|
||
LOGGER = logging.getLogger('colab.plugins.data') | ||
TASKS = set() | ||
|
||
|
||
def lock(method, name): | ||
def wrapped_method(self, *args, **kwargs): | ||
lock_id = 'colab-data-importer-{}'.format(name) | ||
lock = redis.Redis().lock(lock_id) | ||
|
||
if lock.acquire(blocking=False): | ||
try: | ||
return method(*args, **kwargs) | ||
finally: | ||
lock.release() | ||
|
||
return wrapped_method | ||
|
||
|
||
def register_tasks(): | ||
|
||
global TASKS | ||
|
||
for app_name in settings.INSTALLED_APPS: | ||
|
||
module_name = '{}.data_importer'.format(app_name) | ||
try: | ||
module = importlib.import_module(module_name) | ||
except ImportError: | ||
continue | ||
|
||
for item_name in dir(module): | ||
item = getattr(module, item_name) | ||
|
||
if callable(getattr(item, 'fetch_data', None)): | ||
if getattr(item.fetch_data, 'is_abstract', False): | ||
continue | ||
instance = item() | ||
task_name = '{}.{}'.format(module.__name__, item_name) | ||
thread_safe_method = lock(instance.fetch_data, task_name) | ||
task = app.task(name=task_name, bind=True)(thread_safe_method) | ||
TASKS.add(task) | ||
LOGGER.debug('Registered task: %s', task_name) | ||
|
||
LOGGER.debug(TASKS) | ||
return TASKS | ||
|
||
|
||
def data_import(self): | ||
for task in TASKS: | ||
task.delay() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
|
||
|
||
default_app_config = 'colab.plugins.gitlab.apps.ProxyGitlabAppConfig' | ||
default_app_config = 'colab.plugins.gitlab.apps.GitlabPluginAppConfig' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
|
||
|
||
default_app_config = 'colab.plugins.mezuro.apps.ProxyMezuroAppConfig' | ||
default_app_config = 'colab.plugins.mezuro.apps.MezuroPluginAppConfig' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
|
||
from ..utils.apps import ColabProxiedAppConfig | ||
from ..utils.apps import ColabPluginAppConfig | ||
|
||
|
||
class ProxyMezuroAppConfig(ColabProxiedAppConfig): | ||
class MezuroPluginAppConfig(ColabPluginAppConfig): | ||
name = 'colab.plugins.mezuro' | ||
verbose_name = 'Mezuro Proxy' | ||
verbose_name = 'Mezuro Plugin' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
|
||
|
||
default_app_config = 'colab.plugins.noosfero.apps.ProxyNoosferoAppConfig' | ||
default_app_config = 'colab.plugins.noosfero.apps.NoosferoPluginAppConfig' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
|
||
from ..utils.apps import ColabProxiedAppConfig | ||
from ..utils.apps import ColabPluginAppConfig | ||
|
||
|
||
class ProxyNoosferoAppConfig(ColabProxiedAppConfig): | ||
class NoosferoPluginAppConfig(ColabPluginAppConfig): | ||
name = 'colab.plugins.noosfero' | ||
verbose_name = 'Noosfero Proxy' | ||
verbose_name = 'Noosfero Plugin' |
Oops, something went wrong.