forked from celery/django-celery
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:ask/django-celery
Conflicts: Changelog djcelery/tests/runners.py
- Loading branch information
Showing
22 changed files
with
375 additions
and
88 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
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,3 @@ | ||
#!/bin/bash | ||
(cd "${1:-.}"; | ||
find . -name "*.pyc" | xargs rm -- 2>/dev/null) || echo "ok" |
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,23 +1,23 @@ | ||
from __future__ import absolute_import | ||
|
||
from django.conf import settings | ||
from django.test.simple import run_tests as run_tests_orig | ||
from django.test.simple import DjangoTestSuiteRunner | ||
|
||
USAGE = """\ | ||
Custom test runner to allow testing of celery delayed tasks. | ||
""" | ||
|
||
|
||
def run_tests(test_labels, *args, **kwargs): | ||
class CeleryTestSuiteRunner(DjangoTestSuiteRunner): | ||
"""Django test runner allowing testing of celery delayed tasks. | ||
All tasks are run locally, not in a worker. | ||
To use this runner set ``settings.TEST_RUNNER``:: | ||
TEST_RUNNER = "celery.contrib.test_runner.run_tests" | ||
TEST_RUNNER = "celery.contrib.test_runner.CeleryTestSuiteRunner" | ||
""" | ||
settings.CELERY_ALWAYS_EAGER = True | ||
settings.CELERY_EAGER_PROPAGATES_EXCEPTIONS = True # Issue #75 | ||
return run_tests_orig(test_labels, *args, **kwargs) | ||
def setup_test_environment(self, **kwargs): | ||
super(CeleryTestSuiteRunner, self).setup_test_environment(**kwargs) | ||
settings.CELERY_ALWAYS_EAGER = True | ||
settings.CELERY_EAGER_PROPAGATES_EXCEPTIONS = True # Issue #75 |
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
Empty file.
Empty file.
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,62 @@ | ||
from collections import defaultdict | ||
|
||
import cl | ||
|
||
from celery import current_app as celery | ||
from celery.utils.timer2 import apply_interval | ||
|
||
|
||
from clickmuncher import models | ||
|
||
|
||
class Clicks(cl.Actor): | ||
default_routing_key = "Clicks" | ||
|
||
class state: | ||
model = models.Click | ||
clicks = defaultdict(lambda: 0) | ||
|
||
def increment(self, url, clicks=1): | ||
self.clicks[url] += clicks | ||
|
||
def flush(self): | ||
print("FLUSH!!!") | ||
print("STATE: %r" % (self.clicks, )) | ||
for url, clicks in self.clicks.iteritems(): | ||
self.model.objects.increment_clicks(url, clicks) | ||
self.clicks.clear() | ||
|
||
def __init__(self, connection=None, *args, **kwargs): | ||
if not connection: | ||
connection = celery.broker_connection() | ||
super(Clicks, self).__init__(connection, *args, **kwargs) | ||
|
||
def increment(self, url, clicks=1): | ||
self.cast("increment", {"url": url, "clicks": clicks}) | ||
|
||
|
||
class Agent(cl.Agent): | ||
actors = [Clicks()] | ||
flush_every = 5 | ||
|
||
def __init__(self, connection=None, *args, **kwargs): | ||
if not connection: | ||
connection = celery.broker_connection() | ||
self.clicks = Clicks() | ||
self.actors = [self.clicks] | ||
self.timers = [] | ||
super(Agent, self).__init__(connection, *args, **kwargs) | ||
|
||
def on_consume_ready(self, *args, **kwargs): | ||
print("INSTALLING TIMER") | ||
self.timers.append(apply_interval(self.flush_every * 1000, | ||
self.clicks.state.flush)) | ||
|
||
def stop(self): | ||
for entry in self.timers: | ||
entry.cancel() | ||
super(Agent, self).stop() | ||
|
||
|
||
if __name__ == "__main__": | ||
Agent().run_from_commandline() |
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,27 @@ | ||
from django.db import models | ||
from django.utils.translation import ugettext_lazy as _ | ||
from djcelery.managers import ExtendedManager | ||
|
||
|
||
class ClickManager(ExtendedManager): | ||
|
||
def increment_clicks(self, url, increment=1): | ||
"""Increment the click count for an URL.""" | ||
obj, created = self.get_or_create(url=url, | ||
defaults={"clicks": increment}) | ||
if not created: | ||
obj.clicks += increment | ||
obj.save() | ||
|
||
return obj.clicks | ||
|
||
|
||
class Click(models.Model): | ||
url = models.URLField(_(u"URL"), verify_exists=False, unique=True) | ||
clicks = models.PositiveIntegerField(_(u"clicks"), default=0) | ||
|
||
objects = ClickManager() | ||
|
||
class Meta: | ||
verbose_name = _(u"click") | ||
verbose_name_plural = _(u"clicks") |
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,14 @@ | ||
#!/usr/bin/env python | ||
from django.core.management import execute_manager | ||
import imp | ||
try: | ||
imp.find_module('settings') # Assumed to be in the same directory. | ||
except ImportError: | ||
import sys | ||
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__) | ||
sys.exit(1) | ||
|
||
import settings | ||
|
||
if __name__ == "__main__": | ||
execute_manager(settings) |
Oops, something went wrong.