forked from packit/dist-git-to-source-git
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request packit#91 from jpopelka/worker
[deployment] Celery worker Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
- Loading branch information
Showing
27 changed files
with
437 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
*.egg-info | ||
tests/data/acl/SOURCES | ||
|
||
deployment/roles/deploy/files/secrets | ||
deployment/roles/deploy/vars/main.yml | ||
|
||
deployment/roles/deploy/vars/*.yml | ||
!deployment/roles/deploy/vars/local.yml |
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,10 @@ | ||
ANSIBLE_PYTHON := /usr/bin/python3 | ||
AP := ansible-playbook -vv -c local -i localhost, -e ansible_python_interpreter=$(ANSIBLE_PYTHON) | ||
# "By default, Ansible runs as if --tags all had been specified." | ||
# https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html#special-tags | ||
TAGS ?= all | ||
|
||
# example: | ||
# DEPLOYMENT=local make deploy TAGS=worker | ||
deploy: | ||
$(AP) deploy.yml --tags $(TAGS) |
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,68 @@ | ||
version: "2" | ||
|
||
services: | ||
redis: | ||
image: registry.fedoraproject.org/f31/redis | ||
container_name: redis | ||
ports: | ||
- 6379:6379 | ||
user: "123123" | ||
|
||
redis-commander: | ||
container_name: redis-commander | ||
hostname: redis-commander | ||
image: rediscommander/redis-commander:latest | ||
environment: | ||
REDIS_HOST: redis | ||
REDIS_PORT: "6379" | ||
ports: | ||
- 8081:8081 | ||
depends_on: | ||
- redis | ||
user: "123123" | ||
|
||
flower: | ||
image: mher/flower | ||
container_name: flower | ||
ports: | ||
- 5555:5555 | ||
depends_on: | ||
- redis | ||
environment: | ||
FLOWER_DEBUG: "True" | ||
CELERY_BROKER_URL: redis://redis:6379/0 | ||
user: "123123" | ||
|
||
worker: | ||
container_name: worker | ||
build: | ||
context: .. | ||
dockerfile: files/Containerfile.worker | ||
image: docker.io/usercont/dist2src-worker | ||
command: /usr/bin/run_worker.sh | ||
tty: true | ||
depends_on: | ||
- redis | ||
environment: | ||
DEPLOYMENT: dev | ||
APP: dist2src.worker.tasks | ||
CELERY_TASK_NAME: task.dist2src.process_message | ||
volumes: | ||
- ../dist2src/worker:/usr/local/lib/python3.7/site-packages/dist2src/worker:ro,z | ||
user: "123123" | ||
|
||
centosmsg: | ||
container_name: centosmsg | ||
image: docker.io/usercont/packit-service-centosmsg:prod | ||
depends_on: | ||
- redis | ||
environment: | ||
DEPLOYMENT: dev | ||
LOG_LEVEL: DEBUG | ||
REDIS_SERVICE_HOST: redis | ||
CELERY_TASK_NAME: task.dist2src.process_message | ||
#DISABLE_SENDING_CELERY_TASKS: disable | ||
volumes: | ||
- ./roles/deploy/files/secrets/dev/centos-server-ca.cert:/secrets/centos-server-ca.cert:ro,Z | ||
- ./roles/deploy/files/secrets/dev/centos.cert:/secrets/centos.cert:ro,Z | ||
user: "123123" |
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,15 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
--- | ||
- name: Deploy ConfigMap | ||
k8s: | ||
namespace: "{{ project }}" | ||
resource_definition: "{{ item }}" | ||
host: "{{ host }}" | ||
api_key: "{{ api_key }}" | ||
validate_certs: "{{ validate_certs }}" | ||
with_template: | ||
- configmap.yml.j2 | ||
tags: | ||
- always |
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,14 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
# https://docs.openshift.com/container-platform/4.5/builds/builds-configmaps.html | ||
--- | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: env-config | ||
type: Opaque | ||
data: | ||
DEPLOYMENT: "{{ deployment }}" | ||
CELERY_TASK_NAME: "{{ celery_task_name }}" | ||
REDIS_SERVICE_HOST: redis |
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,2 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT |
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,33 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
from os import getenv | ||
|
||
from celery import Celery | ||
from dist2src.worker.sentry import configure_sentry | ||
from lazy_object_proxy import Proxy | ||
|
||
|
||
class Celerizer: | ||
def __init__(self): | ||
self._celery_app = None | ||
|
||
@property | ||
def celery_app(self): | ||
if self._celery_app is None: | ||
host = getenv("REDIS_SERVICE_HOST", "redis") | ||
password = getenv("REDIS_PASSWORD", "") | ||
port = getenv("REDIS_SERVICE_PORT", "6379") | ||
db = getenv("REDIS_SERVICE_DB", "0") | ||
broker_url = f"redis://:{password}@{host}:{port}/{db}" | ||
|
||
self._celery_app = Celery(broker=broker_url) | ||
return self._celery_app | ||
|
||
|
||
def get_celery_application(): | ||
configure_sentry(runner_type="worker") | ||
return Celerizer().celery_app | ||
|
||
|
||
celery_app: Celery = Proxy(get_celery_application) |
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,28 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
from logging import getLogger | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
class only_once(object): | ||
""" | ||
Use as a function decorator to run function only once. | ||
""" | ||
|
||
def __init__(self, func): | ||
self.func = func | ||
self.configured = False | ||
|
||
def __call__(self, *args, **kwargs): | ||
if self.configured: | ||
logger.debug(f"Function {self.func.__name__} already called. Skipping.") | ||
return | ||
|
||
self.configured = True | ||
logger.debug( | ||
f"Function {self.func.__name__} called for the first time with " | ||
f"args: {args} and kwargs: {kwargs}" | ||
) | ||
return self.func(*args, **kwargs) |
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,13 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
from logging import getLogger | ||
from typing import Optional | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
class Processor: | ||
def process_message(self, event: dict, **kwargs) -> Optional[dict]: | ||
logger.info(f"Processing message with {event}") | ||
return None |
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,30 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
from logging import getLogger | ||
from os import getenv | ||
|
||
from dist2src.worker.decorators import only_once | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
@only_once | ||
def configure_sentry(runner_type: str) -> None: | ||
logger.debug("Setting up Sentry") | ||
|
||
if not getenv("SENTRY_DSN") or not getenv("DEPLOYMENT"): | ||
logger.warning("$SENTRY_DSN or $DEPLOYMENT not set") | ||
return | ||
|
||
# so that we don't have to have sentry sdk installed locally | ||
from sentry_sdk import init, configure_scope | ||
from sentry_sdk.integrations.celery import CeleryIntegration | ||
|
||
init( | ||
dsn=getenv("SENTRY_DSN"), | ||
integrations=[CeleryIntegration()], | ||
environment=getenv("DEPLOYMENT"), | ||
) | ||
with configure_scope() as scope: | ||
scope.set_tag("runner-type", runner_type) |
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,13 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
from os import getenv | ||
from typing import Optional | ||
|
||
from dist2src.worker.celerizer import celery_app | ||
from dist2src.worker.processor import Processor | ||
|
||
|
||
@celery_app.task(name=getenv("CELERY_TASK_NAME")) | ||
def process_message(event: dict, **kwargs) -> Optional[dict]: | ||
return Processor().process_message(event=event) |
Oops, something went wrong.