Skip to content

Commit e16d8e5

Browse files
committed
dockerize, use celery beat for automatic scheduling
1 parent ffd1404 commit e16d8e5

File tree

11 files changed

+88
-82
lines changed

11 files changed

+88
-82
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ fresh: clean dependencies
1010

1111
fulluninstall: uninstall clean
1212

13-
install:
13+
testenv: clean_testenv
14+
docker-compose up --build
15+
16+
clean_testenv:
17+
docker-compose down
1418

1519
dependencies:
1620
if [ ! -d $(ROOT_DIR)/venv ]; then python3 -m venv $(ROOT_DIR)/venv; fi

docker-compose.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: "3"
2+
3+
services:
4+
worker:
5+
build:
6+
context: .
7+
dockerfile: dockerfile
8+
volumes:
9+
- ./gitconsensusservice:/app/gitconsensusservice
10+
- ./github_app.private-key.pem:/app/github_app.private-key.pem
11+
environment:
12+
- 'CELERY_BROKER=pyamqp://guest@rabbitmq//'
13+
- 'DEBUG=true'
14+
- 'GITHUB_APP_ID=9231'
15+
depends_on:
16+
- rabbitmq
17+
18+
rabbitmq:
19+
image: rabbitmq

docker/start_worker.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
if [[ ! -v 'MINIMUM_WORKERS' ]]; then
4+
MINIMUM_WORKERS=2
5+
fi
6+
7+
if [[ ! -v 'MAXIMUM_WORKERS' ]]; then
8+
MAXIMUM_WORKERS=10
9+
fi
10+
11+
if [ "$DISABLE_BEAT" = "true" ]
12+
then
13+
echo 'Launching celery worker without beat'
14+
celery -A gitconsensusservice.worker.celery worker --loglevel=info --autoscale=$MAXIMUM_WORKERS,$MINIMUM_WORKERS
15+
else
16+
echo 'Launching celery worker with beat enabled'
17+
rm -f ~/celerybeat-schedule
18+
celery -A gitconsensusservice.worker.celery worker --loglevel=info --autoscale=$MAXIMUM_WORKERS,$MINIMUM_WORKERS -B -s ~/celerybeat-schedule
19+
fi

dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM python:3.6
2+
3+
ADD requirements.txt /app/requirements.txt
4+
WORKDIR /app/
5+
RUN pip install -r requirements.txt
6+
7+
ENV SETTINGS /app/settings.yaml
8+
ENV GITHUB_PRIVATE_KEY /app/github_app.private-key.pem
9+
10+
ADD ./gitconsensusservice/ /app/gitconsensusservice
11+
12+
RUN useradd -ms /bin/bash gitconsensusservice
13+
USER gitconsensusservice
14+
15+
ADD ./docker/start_worker.sh /home/gitconsensusservice/start_worker.sh
16+
17+
ENTRYPOINT /home/gitconsensusservice/start_worker.sh

gitconsensusservice/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
with open(os.environ['SETTINGS'], 'r') as infile:
1212
app.config.update(yaml.load(infile.read()))
1313

14+
15+
SETTINGS = ['DEBUG', 'GITHUB_PRIVATE_KEY', 'GITHUB_APP_ID', 'GITHUB_WEBHOOK_SECRET', 'CELERY_BROKER']
16+
for setting in SETTINGS:
17+
if setting in os.environ:
18+
app.config[setting] = os.environ[setting]
19+
20+
1421
if 'CELERY_BROKER' in app.config:
1522
celery = Celery('gitconsensus', broker=app.config['CELERY_BROKER'])
1623
else:

gitconsensusservice/cli.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@ def install_repos(install_id):
4141
def list_repos():
4242
installs = gh.get_installations()
4343
for install_id in installs:
44-
click.echo('Install %s:' % install_id)
45-
installation = get_githubapp_install(install_id)
46-
repos = installation.get_repositories()
47-
for repo in repos:
48-
user, repo = repo.split('/')
49-
repository = installation.get_repository(user, repo)
50-
if repository.rules:
51-
click.echo('\t%s/%s' % (user, repo))
44+
try:
45+
click.echo('Install %s:' % install_id)
46+
installation = get_githubapp_install(install_id)
47+
repos = installation.get_repositories()
48+
for repo in repos:
49+
user, repo = repo.split('/')
50+
repository = installation.get_repository(user, repo)
51+
if repository.rules:
52+
click.echo('\t%s/%s' % (user, repo))
53+
except:
54+
click.echo('Unable to process install %s' % install_id)
5255

5356

5457
@cli.command(short_help="List details about the current application.")

gitconsensusservice/worker.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1-
import gitconsensusservice.jobs.consensus
1+
from celery.schedules import crontab
2+
from gitconsensusservice.jobs import consensus
23
from gitconsensusservice import celery
4+
5+
6+
@celery.on_after_finalize.connect
7+
def setup_periodic_tasks(sender, **kwargs):
8+
sender.add_periodic_task(
9+
5 * 60.0,
10+
consensus.process_installs.s(),
11+
name='Root Task - Schedule Installation Jobs')

provisioning/etc/gitconsensus/celery

Lines changed: 0 additions & 29 deletions
This file was deleted.

provisioning/etc/systemd/system/gitconsensusworker.service

Lines changed: 0 additions & 16 deletions
This file was deleted.

provisioning/setup.sh

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)