Skip to content
This repository was archived by the owner on Mar 1, 2025. It is now read-only.
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Ondsel-Development/FC-Worker
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: FreeCAD/FC-Worker
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
  • 6 commits
  • 12 files changed
  • 2 contributors

Commits on Feb 1, 2025

  1. Copy the full SHA
    4ee0854 View commit details

Commits on Feb 12, 2025

  1. Merge pull request #1 from amrit3701/fix/docker_build_apt_updates

    Fixes apt source.list for updates
    yorikvanhavre authored Feb 12, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    a8dff15 View commit details

Commits on Apr 19, 2025

  1. Copy the full SHA
    bb9be79 View commit details

Commits on Apr 24, 2025

  1. Copy the full SHA
    435e8aa View commit details

Commits on Apr 26, 2025

  1. Merge pull request #2 from amrit3701/feature/implement_worker

    Feature: Implement Celery worker
    amrit3701 authored Apr 26, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    4252c68 View commit details

Commits on May 4, 2025

  1. Merge pull request #3 from amrit3701/feature/auto_reload_on_code_change

    Feature: Add Development Environment for fc-worker
    amrit3701 authored May 4, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    f258214 View commit details
Showing with 234 additions and 9 deletions.
  1. +1 −0 .gitignore
  2. +5 −3 Dockerfile
  3. +20 −0 Dockerfile.api
  4. +16 −0 Dockerfile.api.dev
  5. +17 −0 Dockerfile.celery
  6. +14 −0 Dockerfile.celery.dev
  7. +18 −4 README.md
  8. +32 −0 docker-compose.dev.yml
  9. +26 −0 docker-compose.yml
  10. +72 −0 main.py
  11. +7 −0 requirements/aws.txt
  12. +6 −2 requirements/main.txt
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__/
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -7,15 +7,17 @@ FROM amrit3701/freecad-cli:0.21-amd64-01.05.2024
WORKDIR /

ENV LANG=en_US.UTF-8
RUN apt-get update && apt-get install -y locales && \
RUN sed -i 's|http://archive.ubuntu.com|http://old-releases.ubuntu.com|g' /etc/apt/sources.list && \
sed -i 's|http://security.ubuntu.com|http://old-releases.ubuntu.com|g' /etc/apt/sources.list && \
apt-get update && apt-get install -y locales && \
sed -i -e "s/# $LANG.*/$LANG UTF-8/" /etc/locale.gen && \
dpkg-reconfigure --frontend=noninteractive locales && \
update-locale LANG=$LANG
ENV LC_ALL en_US.UTF-8
ENV LANGUAGE en_US:en

COPY requirements/main.txt .
RUN pip3 install -r main.txt --break-system-packages
COPY requirements/aws.txt requirements.txt
RUN pip3 install -r requirements.txt --break-system-packages

COPY fc_worker/ /fc_worker/.

20 changes: 20 additions & 0 deletions Dockerfile.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM amrit3701/freecad-cli:0.21-amd64-01.05.2024

# Set working directory
WORKDIR /app

# Copy requirements.txt
COPY requirements/main.txt requirements.txt

# Install dependencies
RUN pip3 install -r requirements.txt --break-system-packages

# Copy code
COPY main.py .
COPY fc_worker ./fc_worker

# Expose port for FastAPI
EXPOSE 8080

# Start FastAPI
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]
16 changes: 16 additions & 0 deletions Dockerfile.api.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM amrit3701/freecad-cli:0.21-amd64-01.05.2024

# Set working directory
WORKDIR /app

# Copy requirements.txt
COPY requirements/main.txt requirements.txt

# Install dependencies
RUN pip3 install -r requirements.txt --break-system-packages

# Expose port for FastAPI
EXPOSE 8080

# Start FastAPI
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080", "--reload"]
17 changes: 17 additions & 0 deletions Dockerfile.celery
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM amrit3701/freecad-cli:0.21-amd64-01.05.2024

# Set working directory
WORKDIR /app

# Copy requirements.txt
COPY requirements/main.txt requirements.txt

# Install dependencies
RUN pip3 install -r requirements.txt --break-system-packages

# Copy code
COPY main.py .
COPY fc_worker ./fc_worker

# Start Celery worker
CMD ["celery", "-A", "main.celery", "worker", "--loglevel", "info"]
14 changes: 14 additions & 0 deletions Dockerfile.celery.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM amrit3701/freecad-cli:0.21-amd64-01.05.2024

# Set working directory
WORKDIR /app

# Copy requirements.txt
COPY requirements/main.txt requirements.txt

# Install dependencies
RUN pip3 install -r requirements.txt --break-system-packages
RUN pip3 install watchdog --break-system-packages

# Start Celery worker
CMD ["watchmedo", "auto-restart", "--directory", ".", "--pattern", "*.py", "--recursive", "--", "celery", "-A", "main.celery", "worker", "--loglevel", "info"]
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -6,20 +6,34 @@ SPDX-License-Identifier: LGPL-2.0-or-later

# FreeCAD Worker

## Building
## Running in non-aws mode

```bash
docker build -t fc-worker .
# Build the docker image
docker-compose build

# Run the docker container
BACKEND_URL=<backend_url> docker-compose up -d

# For development
docker-compose -f docker-compose.dev.yml build
BACKEND_URL=<backend_url> docker-compose -f docker-compose.dev.yml up -d
```

## Running into development mode
## Running in aws mode

### Building

```bash
# Build the docker image
docker build -t fc-worker .

# Run the docker container
docker run -p 9000:8080 -v <path_of_fc_worker>:/fc_worker --name fc_worker fc-worker:latest
```

## Testing

```bash
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"command": "health_check"}'
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -H "Content-Type: application/json" -d '{"command": "health_check"}'
```
32 changes: 32 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
services:
redis:
image: redis:alpine

api:
build:
context: .
dockerfile: ./Dockerfile.api.dev
depends_on:
- redis
ports:
- "9000:8080"
environment:
- REDIS_HOST=redis
- REDIS_PORT=6379
volumes:
- ./fc_worker:/app/fc_worker
- ./main.py:/app/main.py

celery-worker:
build:
context: .
dockerfile: ./Dockerfile.celery.dev
depends_on:
- redis
environment:
- REDIS_HOST=redis
- REDIS_PORT=6379
- BACKEND_URL=${BACKEND_URL:-http://host.docker.internal:3030}
volumes:
- ./fc_worker:/app/fc_worker
- ./main.py:/app/main.py
26 changes: 26 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
services:
redis:
image: redis:alpine

api:
build:
context: .
dockerfile: ./Dockerfile.api
depends_on:
- redis
ports:
- "9000:8080"
environment:
- REDIS_HOST=redis
- REDIS_PORT=6379

celery-worker:
build:
context: .
dockerfile: ./Dockerfile.celery
depends_on:
- redis
environment:
- REDIS_HOST=redis
- REDIS_PORT=6379
- BACKEND_URL=${BACKEND_URL:-http://host.docker.internal:3030}
72 changes: 72 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import os

from celery import Celery
from dotenv import load_dotenv
from fastapi import FastAPI
from fc_worker import export_command, model_configurer_command
from fc_worker.api_utils import (
CONFIGURE_MODEL_CMD,
EXPORT_CMDS,
HEALTH_CHECK_CMD,
trace_log,
)
from pydantic import BaseModel

# Load environment variables from .env file
load_dotenv()

app = FastAPI()

redis_host = os.environ["REDIS_HOST"]
redis_port = os.environ["REDIS_PORT"]

# Configure Celery with Redis from environment variables
broker_url = result_backend = f"redis://{redis_host}:{redis_port}/0"
celery = Celery(__name__, broker=broker_url, backend=result_backend)


class ModelPayload(BaseModel):
id: str | None = None
fileName: str | None = None
command: str
accessToken: str | None = None
attributes: dict = {}
isSharedModel: bool | None = None
sharedModelId: str | None = None


@app.post("/2015-03-31/functions/function/invocations", status_code=202)
async def start_job(payload: ModelPayload):
command = payload.command
if command == HEALTH_CHECK_CMD:
return {"Status": "OK"}
elif command.upper() in [CONFIGURE_MODEL_CMD, EXPORT_CMDS]:
run_background_task.delay(payload.model_dump())
return {"Status": "Job started"}
else:
return f"Thank you strace, worker is running."


@celery.task
def run_background_task(payload):
print(f"Processing job with payload: {payload}")
result = lambda_handler(payload, {})
print(f"Completed processing job with payload: {payload} And result: {result}")


@trace_log
def lambda_handler(event, context):
print(f"Starting job for event: {event}")
command = event.get("command", None)
if command.upper() == CONFIGURE_MODEL_CMD:
return model_configurer_command(event, context)
elif command.upper() in EXPORT_CMDS:
export_command(event, command)
else:
return f"Invalid command: {command}"


if __name__ == "__main__":
import uvicorn

uvicorn.run(app, host="0.0.0.0", port=8080)
7 changes: 7 additions & 0 deletions requirements/aws.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-FileCopyrightText: 2024 Ondsel <development@ondsel.com>
#
# SPDX-License-Identifier: LGPL-2.0-or-later

awslambdaric==2.0.4
colorlog==6.7.0
requests==2.31.0
8 changes: 6 additions & 2 deletions requirements/main.txt
Original file line number Diff line number Diff line change
@@ -2,6 +2,10 @@
#
# SPDX-License-Identifier: LGPL-2.0-or-later

awslambdaric==2.0.4
colorlog==6.7.0
requests==2.31.0
celery==5.4.0
fastapi==0.115.8
python-dotenv==1.0.1
redis==5.2.1
requests==2.31.0
uvicorn==0.34.0