Skip to content

Commit

Permalink
borked
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed May 18, 2024
1 parent c06ae0f commit 2103873
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 430 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=linux/x86_64 ubuntu:24.04
FROM ubuntu:24.04

RUN apt-get update && \
apt-get install -y \
Expand All @@ -14,4 +14,4 @@ RUN python3 -m pip install --no-cache-dir -r requirements.txt

COPY distributaur/ ./distributaur/

CMD ["celery", "-A", "distributaur.example", "example", "--loglevel=info", "--concurrency=1"]
CMD ["celery", "-A", "distributaur.task_runner", "worker", "--loglevel=info", "--concurrency=1"]
Empty file added distributaur/__init__.py
Empty file.
197 changes: 0 additions & 197 deletions distributaur/batch.py

This file was deleted.

18 changes: 18 additions & 0 deletions distributaur/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import argparse
import os
import sys

sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../"))

from distributaur.core import submit_task

def main():
parser = argparse.ArgumentParser(description="Distributaur CLI")
parser.add_argument("--code", type=str, help="Python code to execute.")
args = parser.parse_args()

result = submit_task(args.code)
print(f"Execution Result: {result}")

if __name__ == "__main__":
main()
33 changes: 33 additions & 0 deletions distributaur/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# core.py

from celery import Celery
from .environment import get_redis_url
from .decorators import register_task

app = Celery("distributaur_tasks", broker=get_redis_url(), backend=get_redis_url())

@app.task(bind=True)
def execute_python_code(self, code):
try:
local_scope = {}
exec(code, {'__builtins__': None}, local_scope)
return local_scope.get('result', None) # Assuming the code defines 'result'
except Exception as e:
print(f"Error executing code: {str(e)}")
return None

def submit_task(code):
"""
Submits a Python code execution task to the Celery worker.
Args:
code (str): Python code to execute.
Returns:
result: The result of the Python code execution or None if an error occurs.
"""
try:
result = app.send_task('execute_python_code', args=[code])
return result.get(timeout=10) # Waits for the task to complete and returns the result
except Exception as e:
print(f"Failed to submit task: {str(e)}")
return None
5 changes: 5 additions & 0 deletions distributaur/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def register_task(app):
def decorator(func):
task = app.task(bind=True)(func)
return func
return decorator
17 changes: 17 additions & 0 deletions distributaur/environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os

def get_redis_url():
"""Constructs the Redis connection URL from environment variables."""
host = os.getenv("REDIS_HOST", "localhost")
password = os.getenv("REDIS_PASSWORD", None)
port = os.getenv("REDIS_PORT", 6379)
return f"redis://:{password}@{host}:{port}" if password else f"redis://{host}:{port}"

def get_env_vars():
"""Loads environment variables critical for Distributaur operations."""
required_vars = ["VAST_API_KEY", "REDIS_HOST", "REDIS_PORT"]
env_vars = {var: os.getenv(var) for var in required_vars}
if not all(env_vars.values()):
missing = [var for var, value in env_vars.items() if not value]
raise EnvironmentError(f"Missing critical environment variables: {', '.join(missing)}")
return env_vars
5 changes: 0 additions & 5 deletions distributaur/example.py

This file was deleted.

82 changes: 0 additions & 82 deletions distributaur/task_runner.py

This file was deleted.

2 changes: 1 addition & 1 deletion distributaur/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .vast import *
from distributaur.vast import *
2 changes: 1 addition & 1 deletion distributaur/tests/vast.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def rented_nodes(vast_api_key):

max_price = 0.5
max_nodes = 1
image = "arfx/simian-worker:latest"
image = "arfx/distributaur-example-worker-worker:latest"

nodes = rent_nodes(max_price, max_nodes, image, vast_api_key)
yield nodes
Expand Down
Loading

0 comments on commit 2103873

Please sign in to comment.