Skip to content

Commit 58b88e9

Browse files
Miha Pleškomatejart
authored andcommitted
Configured unit tests for projects
Resolved bug that unit tests are adding messages to the main queue. Solution: tests use separated Celery queue (dice_deploy_tests) with own dummy worker (worker-tests). Also added Celery dashboard to control messages with ease. Actually just installed Celery Flower module and it works. Implemented upstart scripts for both Celery workers (main and test) and also for celery dashboard.
1 parent a10f36b commit 58b88e9

File tree

15 files changed

+224
-9
lines changed

15 files changed

+224
-9
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
.idea/*
88
dice_deploy_django/uploads/*
99
db.sqlite3
10+
dice_deploy_django/celery.err
11+
dice_deploy_django/celery.out

Vagrantfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@ Vagrant.configure(2) do |config|
44
config.vm.box = "ubuntu/trusty64"
55

66
config.vm.network "private_network", type: "dhcp"
7+
# django server
78
config.vm.network "forwarded_port", guest: 8080, host: 7080
9+
# celery flower dashboard (main)
10+
config.vm.network "forwarded_port", guest: 5556, host: 8056
11+
# celery flower dashboard (tests)
12+
config.vm.network "forwarded_port", guest: 5555, host: 8055
13+
814

915
config.vm.provision "shell", path: "provision-root.sh"
1016
config.vm.provision "shell", path: "provision-user.sh", privileged: false
1117

18+
# setup and run celery upstart service
19+
config.vm.provision "shell", inline: "cp /vagrant/install/upstart-services/* /etc/init/"
20+
config.vm.provision "shell", inline: "service celery-service start"
21+
1222
config.vm.synced_folder "dice_deploy_django", "/home/vagrant/dice_deploy_django"
1323

1424
config.vm.provider "virtualbox" do |v|

dice_deploy_django/cfy_wrapper/management/__init__.py

Whitespace-only changes.

dice_deploy_django/cfy_wrapper/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from django.core.management.base import BaseCommand, CommandError
2+
from django.conf import settings
3+
import subprocess
4+
5+
"""
6+
Example calls:
7+
python manage.py celery-service start
8+
python manage.py celery-service stop
9+
"""
10+
11+
12+
class Command(BaseCommand):
13+
help = 'Starts or stops celery service.'
14+
15+
def add_arguments(self, parser):
16+
parser.add_argument('command', type=str, help='start|stop')
17+
parser.add_argument('--unit_tests', action='store_true',
18+
help='Add this flag to run celery-test-service instead of celery-service')
19+
parser.set_defaults(unit_tests=False)
20+
21+
def handle(self, *args, **options):
22+
print('unit_tests: %s' % options['unit_tests'])
23+
24+
if options['command'].lower() == 'start':
25+
print('Starting celery for app %s' % settings.CELERY_APP_NAME)
26+
p = subprocess.Popen([
27+
'sudo',
28+
'service',
29+
'celery-service' if not options['unit_tests'] else 'celery-test-service',
30+
'start'
31+
], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
32+
out, err = p.communicate()
33+
if 'start/running' in out or 'already running' in err:
34+
print('Success')
35+
else:
36+
print('ERROR: %s\n%s' % (out, err))
37+
elif options['command'].lower() == 'stop':
38+
print('Stopping celery for app %s' % settings.CELERY_APP_NAME)
39+
p = subprocess.Popen([
40+
'sudo',
41+
'service',
42+
'celery-service' if not options['unit_tests'] else 'celery-test-service',
43+
'stop'
44+
], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
45+
out, err = p.communicate()
46+
if 'stop/waiting' in out:
47+
print('Success')
48+
else:
49+
print('ERROR: %s\n%s' % (out, err))
50+
else:
51+
raise CommandError('Invalid command argument, got: %s' % options['command'])
52+

dice_deploy_django/dice_deploy/celery.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from celery import Celery
99
from django.conf import settings
1010

11-
app = Celery("dice_deploy")
11+
# app = Celery(settings.CELERY_APP_NAME)
12+
app = Celery('BUREK')
1213
app.config_from_object("django.conf:settings")
1314
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

dice_deploy_django/dice_deploy/settings.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"django.contrib.contenttypes",
2525
"rest_framework",
2626
"cfy_wrapper",
27+
"unit_tests", # must be app in order for celery-test-service to work
2728
)
2829

2930
MIDDLEWARE_CLASSES = (
@@ -60,6 +61,13 @@
6061
CELERY_ACCEPT_CONTENT = ['json']
6162
CELERY_ENABLE_UTC = True
6263

64+
BROKER_CONNECTION_MAX_RETRIES = 0
65+
BROKER_FAILOVER_STRATEGY = "round-robin"
66+
BROKER_HEARTBEAT = 10
67+
CELERY_SEND_EVENTS = True
68+
69+
CELERY_APP_NAME = 'dice_deploy'
70+
6371
# Cloudify settings
6472
CFY_MANAGER_URL = "172.16.95.77"
6573
POOL_SLEEP_INTERVAL = 3 # In seconds
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
from dice_deploy.settings import *
22
import os
33

4+
# tests will check this flag pripr running
5+
IS_TEST_SETTINGS = True
6+
47
TEST_FILES_DIR = os.path.join(BASE_DIR, 'unit_tests', 'files')
8+
TEST_FILE_BLUEPRINT_EXAMPLE = os.path.join(TEST_FILES_DIR, 'blueprints', 'example_blueprint.tar.gz')
9+
10+
MEDIA_ROOT = os.path.join(BASE_DIR, "uploads_tests")
11+
12+
# make celery synchronous
13+
# CELERY_ALWAYS_EAGER = True
14+
CELERY_APP_NAME = 'dice_deploy_tests'

dice_deploy_django/requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ markdown
44
cloudify_rest_client
55
celery
66
enum34
7+
flower

dice_deploy_django/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ kombu==3.0.28 # via celery
1616
markdown==2.6.2
1717
pytz==2015.6 # via celery
1818
requests==2.7.0 # via cloudify-rest-client
19+
flower==0.8.4

0 commit comments

Comments
 (0)