How to use the celery.Task function in celery

To help you get started, we’ve selected a few celery examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github celery / celery / t / unit / tasks / test_tasks.py View on Github external
def test_bind__no_app(self, current_app):

        class XTask(Task):
            _app = None

        XTask._app = None
        XTask.__bound__ = False
        XTask.bind = Mock(name='bind')
        assert XTask.app is current_app
        XTask.bind.assert_called_with(current_app)
github ambitioninc / django-entity-emailer / entity_emailer / tasks.py View on Github external
It first tries to find a title block inside of a head block. If that exists,
    the title is used as the subject of the email. If it does not exist,
    the first 40 characters of the email are used as the subject. In the latter
    case, it is assumed that html tags are not actually present in the html content.
    """
    soup = BeautifulSoup(email_content)
    subject = soup.title.string.strip() if soup.title else None
    if not subject:
        subject = email_content.split('\n')[0].strip()[:40]
        if len(subject) == 40:
            subject = u'{}...'.format(subject)

    return subject


class SendUnsentScheduledEmails(Task):
    """
    Send all unsent emails, whose scheduled time has passed.

    This task should be added to a celery beat.
    """
    def run(self, *args, **kwargs):
        with db_mutex('send-unsent-scheduled-emails'):
            self.run_worker(*args, **kwargs)

    def run_worker(self, *args, **kwargs):
        current_time = datetime.utcnow()
        email_medium = get_medium()
        to_send = Email.objects.filter(
            scheduled__lte=current_time, sent__isnull=True).select_related('event').prefetch_related('recipients')

        # Fetch the contexts of every event so that they may be rendered
github mozilla / build-relengapi / relengapi / blueprints / slaveloan / tasks.py View on Github external
def wrapper(*args, **kwargs):
            bound_task = None
            loanid = kwargs.get("loanid", None)
            if args and isinstance(args[0], celery.Task):
                bound_task = args[0]
            if before:
                add_task_to_history(loanid, before.format(**locals()))
            retval = f(*args, **kwargs)
            if after:
                add_task_to_history(loanid, after.format(**locals()))
            return retval
        return wrapper
github carbonblack / cb-yara-connector / src / tasks.py View on Github external
from analysis_result import AnalysisResult
from celery_app import app
from config_handling import ConfigurationInit
from rule_handling import generate_yara_rule_map_hash

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

logger = get_task_logger(__name__)
logger.setLevel(logging.CRITICAL)

rulelogger = logging.getLogger("yaraworker")
rulelogger.setLevel(logging.INFO)


# noinspection PyAbstractClass
class MyTask(Task):

    def on_failure(self, exc, task_id, args, kwargs, einfo):
        pass
        # print('{0!r} failed: {1!r}'.format(task_id, exc))


# ----- Lock Object Class ------------------------------------------------------------


class ReadWriteLock:
    """
    A lock object that allows many simultaneous "read locks", but
    only one "write lock."
    """

    def __init__(self):
github briancaffey / django-postgres-vue-gitlab-ecs / backend / apps / banking / tasks.py View on Github external
import csv
import datetime
from io import StringIO

import celery
from celery.task import task

from apps.banking.models import StatementFile, Transaction


class BaseTask(celery.Task):
    pass


@task(bind=True, base=BaseTask)
def process_statement_file(self, statement_file_id):

    """
    This functions accepts one argument: the ID of a statement file
    It then opens the file, creates transactions and then uploads all
    of the transactions with one bulk create database operation
    """

    statement_file = StatementFile.objects.get(
        id=statement_file_id
    ).statement_file
    file_data = statement_file.read().decode("utf-8")
github fish2000 / django-signalqueue / signalqueue / worker / celeryqueue.py View on Github external
def taskmaster(sig):
    class CelerySignalTask(Task):
        name = "%s:%s" % (sig.regkey, sig.name)
        store_errors_even_if_ignored = True
        ignore_result = False
        track_started = True
        acks_late = True
        
        def __init__(self):
            self.signal_regkey = sig.regkey
            self.signal_name = sig.name
        
        @property
        def signal(self):
            for registered_signal in signalqueue.SQ_DMV[self.signal_regkey]:
                if registered_signal.name == self.signal_name:
                    return registered_signal
            return None
github ambitioninc / django-entity-emailer / entity_emailer / tasks.py View on Github external
email_addresses = []
    for entity in email.recipients.all():
        # Get the email address out of the entity meta data
        email_address = entity.entity_meta.get(email_key, None)

        # Make sure the email address exists and is not an empty string
        if email_address is not None and len(email_address):
            # If the exclude entity key is not set, or is set but the value is not none
            if not exclude_entity_key or entity.entity_meta.get(exclude_entity_key, None) is not None:
                email_addresses.append(email_address)

    # Return the email addresses
    return email_addresses


class ConvertEventsToEmails(Task):
    """
    Converts events to emails based on the email subscriptions.
    """
    def run(self, *args, **kwargs):
        with db_mutex('convert-events-to-emails'):
            self.run_worker(*args, **kwargs)

    def run_worker(self, *args, **kwargs):
        convert_events_to_emails()


def convert_events_to_emails():
    """
    Converts unseen events to emails and marks them as seen.
    """
    email_medium = get_medium()
github erigones / esdc-ce / que / internal.py View on Github external
from __future__ import absolute_import

# noinspection PyProtectedMember
from celery import Task

from que import Q_MGMT
from que.erigonesd import cq
from que.utils import generate_internal_task_id

ERIGONES_TASK_USER = cq.conf.ERIGONES_TASK_USER


# noinspection PyAbstractClass
class InternalTask(Task):
    """
    Abstract task for internal tasks that nobody should know about, running in mgmt queue.
    """
    abstract = True

    def call(self, *args, **kwargs):
        """
        Creates task in mgmt queue with same arguments. Returns task_id.
        """
        task_id = generate_internal_task_id()
        # First argument is always task TD
        args = list(args)
        args.insert(0, task_id)

        # Run task
        return self.apply_async(args=args, kwargs=kwargs, queue=Q_MGMT, task_id=task_id,
github girder / girder_worker / girder_worker / task.py View on Github external
class GirderAsyncResult(AsyncResult):
    def __init__(self, *args, **kwargs):
        self._job = None
        super(GirderAsyncResult, self).__init__(*args, **kwargs)

    @property
    def job(self):
        context = get_context()
        if self._job is None:
            self._job = context.get_async_result_job_property(self)
        return self._job


class Task(celery.Task):
    """
    Girder Worker Task object. Tasks defined by plugins must be subclasses of this class,
    however you will typically not need to reference it yourself, as it will be automatically
    instantiated by the girder_worker celery app. See :ref:`creating-tasks` for instructions.
    """

    _girder_job_title = ''
    _girder_job_type = 'celery'
    _girder_job_public = False
    _girder_job_handler = 'celery_handler'
    _girder_job_other_fields = {}

    @classmethod
    def girder_job_defaults(cls):
        return {
            'girder_job_title': cls._girder_job_title,