-
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 #1 from wlatanowicz/wl/refactor-custom-backends
Custom backends
- Loading branch information
Showing
14 changed files
with
376 additions
and
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from time import sleep | ||
|
||
from themule import job | ||
|
||
|
||
@job() | ||
def do_something(job_number): | ||
print(f"Starting job {job_number}") | ||
sleep(60) | ||
print(f"Job finished {job_number}") |
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,9 @@ | ||
from time import sleep | ||
|
||
from jobs import do_something | ||
|
||
if __name__ == "__main__": | ||
for i in range(5): | ||
print(f"Submitting job {i}...") | ||
do_something.submit(job_number=i) | ||
sleep(30) |
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 @@ | ||
FROM python:3.10 | ||
|
||
COPY . /themule | ||
|
||
RUN pip install -e "/themule/[docker]" | ||
|
||
COPY ./examples/app/ /app/ | ||
|
||
WORKDIR /app | ||
|
||
ENV PYTHONUNBUFFERED=1 | ||
|
||
CMD ["python3", "/app/main.py"] |
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 @@ | ||
version: '3.0' | ||
services: | ||
main: | ||
build: | ||
context: ../.. | ||
dockerfile: examples/docker/Dockerfile | ||
image: themule-example-image | ||
volumes: | ||
- /var/run/docker.sock:/var/run/docker.sock | ||
environment: | ||
- THEMULE_BACKEND=themule.backends.LocalDockerBackend | ||
- THEMULE_DOCKER_IMAGE=themule-example-image | ||
- THEMULE_DOCKER_AUTO_REMOVE=false | ||
- DOCKER_HOST=unix:///var/run/docker.sock | ||
- PYTHONPATH=/app |
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,2 +1,2 @@ | ||
boto3>=1.23.5 | ||
click>=8.1.3 | ||
django-environ>=0.9.0 |
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 @@ | ||
boto3>=1.23.5 |
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 @@ | ||
docker>=6.1.3 |
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,161 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
from typing import TYPE_CHECKING | ||
|
||
from .conf import NOTSET, settings | ||
from .job import StartedJob | ||
|
||
if TYPE_CHECKING: | ||
from .job import Job | ||
from .serializers import BaseSerializer | ||
|
||
|
||
DEFAULT_BACKEND = "themule.backends.AwsBatchBackend" | ||
|
||
|
||
class BaseBackend: | ||
OPTION_PREFIX = "base" | ||
|
||
def __init__(self, **options) -> None: | ||
pass | ||
|
||
def submit_job(self, job: Job, serializer: BaseSerializer) -> StartedJob: | ||
raise NotImplementedError() | ||
|
||
def get_path(self): | ||
return f"{self.__module__}.{self.__class__.__name__}" | ||
|
||
def get_option_value(self, options, option, default=NOTSET, cast=None): | ||
return settings.get_value_for_job( | ||
options, | ||
self.OPTION_PREFIX, | ||
option, | ||
default=default, | ||
cast=cast, | ||
) | ||
|
||
|
||
class AwsBatchBackend(BaseBackend): | ||
OPTION_PREFIX = "aws_batch" | ||
|
||
def __init__(self, **options) -> None: | ||
self.queue_name = self.get_option_value(options, "queue_name") | ||
self.job_definition = self.get_option_value(options, "job_definition") | ||
|
||
def submit_job(self, job: Job, serializer: BaseSerializer) -> StartedJob: | ||
try: | ||
import boto3 | ||
except ImportError: | ||
raise ValueError("AWS support not installed") | ||
|
||
serialized_job = serializer.serialize(job) | ||
|
||
client = boto3.client("batch") | ||
response = client.submit_job( | ||
jobName=str(job.id), | ||
jobQueue=self.queue_name, | ||
jobDefinition=self.job_definition, | ||
containerOverrides={ | ||
"command": [ | ||
"themule", | ||
"execute-job", | ||
"--serializer", | ||
serializer.get_path(), | ||
serialized_job, | ||
], | ||
}, | ||
) | ||
|
||
job_id = str(response.get("jobId")) | ||
|
||
return StartedJob( | ||
self.get_path(), | ||
job, | ||
job_id, | ||
) | ||
|
||
|
||
class LocalDockerBackend(BaseBackend): | ||
OPTION_PREFIX = "docker" | ||
|
||
def __init__(self, **options) -> None: | ||
self.docker_image = self.get_option_value(options, "image") | ||
self.entrypoint = self.get_option_value( | ||
options, "entrypoint", default=None, cast=list | ||
) | ||
self.environment = self.get_option_value( | ||
options, "environment", default={}, cast=dict | ||
) | ||
self.pass_environment = self.get_option_value( | ||
options, "pass_environment", default=True, cast=bool | ||
) | ||
self.auto_remove = self.get_option_value( | ||
options, "auto_remove", default=True, cast=bool | ||
) | ||
|
||
def submit_job(self, job: Job, serializer: BaseSerializer) -> StartedJob: | ||
try: | ||
import docker | ||
except ImportError: | ||
raise ValueError("Docker support not installed") | ||
|
||
serialized_job = serializer.serialize(job) | ||
|
||
environment = self.environment | ||
if self.pass_environment: | ||
environment = { | ||
**os.environ, | ||
**environment, | ||
} | ||
|
||
docker_command = [ | ||
"themule", | ||
"execute-job", | ||
"--serializer", | ||
serializer.get_path(), | ||
serialized_job, | ||
] | ||
|
||
client = docker.from_env() | ||
container = client.containers.run( | ||
self.docker_image, | ||
docker_command, | ||
entrypoint=self.entrypoint, | ||
environment=environment, | ||
auto_remove=self.auto_remove, | ||
detach=True, | ||
) | ||
|
||
job_id = container.id | ||
|
||
return StartedJob( | ||
self.get_path(), | ||
job, | ||
job_id, | ||
) | ||
|
||
|
||
class LocalProcess(BaseBackend): | ||
def submit_job(self, job: Job, serializer: BaseSerializer) -> StartedJob: | ||
import subprocess | ||
|
||
serialized_job = serializer.serialize(job) | ||
|
||
command = [ | ||
"themule", | ||
"execute-job", | ||
"--serializer", | ||
serializer.get_path(), | ||
serialized_job, | ||
] | ||
|
||
process = subprocess.Popen(command) | ||
|
||
job_id = process.pid | ||
|
||
return StartedJob( | ||
self.get_path(), | ||
job, | ||
job_id, | ||
) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.