Skip to content

Commit 55b86ce

Browse files
committed
Add celery worker and consensus tasks
1 parent 784ec52 commit 55b86ce

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

bin/worker

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
# Get real directory in case of symlink
4+
if [[ -L "${BASH_SOURCE[0]}" ]]
5+
then
6+
DIR="$( cd "$( dirname $( readlink "${BASH_SOURCE[0]}" ) )" && pwd )"
7+
else
8+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
9+
fi
10+
11+
source "$DIR/envvar"
12+
13+
celery -A gitconsensusservice.celery worker --loglevel=info

gitconsensusservice/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from celery import Celery
12
from flask import Flask
23
import os
34
import yaml
@@ -9,3 +10,9 @@
910
if os.path.isfile(os.environ['SETTINGS']):
1011
with open(os.environ['SETTINGS'], 'r') as infile:
1112
app.config.update(yaml.load(infile.read()))
13+
14+
if 'CELERY_BROKER' in app.config:
15+
print(app.config['CELERY_BROKER'])
16+
celery = Celery('gitconsensus', broker=app.config['CELERY_BROKER'])
17+
else:
18+
celery = Celery('gitconsensus')

gitconsensusservice/jobs/consensus.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from gitconsensusservice import app, celery
2+
from services.githubapp import gh
3+
4+
5+
@celery.task
6+
def process_installs(synchronous=False):
7+
print('Processing all installations')
8+
installs = gh.get_installations()
9+
for install in installs:
10+
if synchronous:
11+
process_installation(install, True)
12+
else:
13+
process_installation.delay(install)
14+
15+
16+
@celery.task
17+
def process_installation(installation_id, synchronous=False):
18+
print('Processing installation %s' % (installation_id))
19+
installation = gh.get_installation(installation_id)
20+
repositories = installation.get_repositories()
21+
for repository in repositories:
22+
user, repo = repository.split('/')
23+
process_repository(installation_id, user, repo, True)
24+
25+
26+
@celery.task
27+
def process_repository(installation_id, user, repo, synchronous=False):
28+
print('Processing %s/%s as installation %s' % (user, repo, installation_id))
29+
installation = gh.get_installation(installation_id)
30+
repository = installation.get_repository(user, repo)
31+
32+
# has consensus rules?
33+
if not repository.rules:
34+
print('%s/%s does not have any consensus rules.' % (user, repo))
35+
return
36+
37+
prs = installation.get_pr_numbers(user, repo)
38+
for pr in prs:
39+
if synchronous:
40+
process_pull_request(installation_id, user, repo, pr)
41+
else:
42+
process_pull_request.delay(installation_id, user, repo, pr)
43+
44+
45+
@celery.task
46+
def process_pull_request(installation_id, user, repo, pull_request):
47+
print('Processing %s/%s #%s as installation %s' % (user, repo, pull_request, installation_id))
48+
installation = gh.get_installation(installation_id)
49+
repository = installation.get_repository(user, repo)
50+
request = repository.getPullRequest(pull_request)
51+
if request.validate():
52+
print("Merging PR#%s" % (request.number,))
53+
request.vote_merge()
54+
elif request.shouldClose():
55+
print("Closing PR#%s" % (request.number,))
56+
request.close()
57+
else:
58+
request.addInfoLabels()

settings.dist.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ DEBUG: false
22
GITHUB_PRIVATE_KEY: '/absolute/path/to/private-key.pem'
33
GITHUB_APP_ID: 0
44
GITHUB_WEBHOOK_SECRET:
5+
CELERY_BROKER: 'pyamqp://guest@localhost//'

0 commit comments

Comments
 (0)