Skip to content

centara/sb-framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

ServiceBus Task Processing Framework

A lightweight, standalone Python framework for processing Azure ServiceBus messages with automatic status reporting via REST API.

Features

  • Simple Installation: pip install servicebus-framework
  • ServiceBus Integration: Listen to Azure ServiceBus queues and topics
  • REST API Reporting: Automatically report task status to any REST API
  • Pluggable Task Handlers: Easy to extend with custom task processors
  • Robust Error Handling: Built-in retry mechanisms and error reporting
  • Container-Ready: Designed for Azure Container Jobs and Kubernetes
  • Zero Dependencies: No framework coupling - works with any backend

Quick Start

  1. Install the framework:

    pip install servicebus-framework
  2. Create a task handler:

    from servicebus_framework import TaskHandler, TaskResult
    
    class MyTaskHandler(TaskHandler):
        def process(self, task_data: dict) -> TaskResult:
            # Your task processing logic here
            message = task_data.get('message')
            return TaskResult(success=True, result=f"Processed: {message}")
  3. Configure and run:

    from servicebus_framework import ServiceBusWorker
    
    # Configure the worker
    worker = ServiceBusWorker(
        connection_string="Endpoint=sb://...",
        queue_name="my-queue",
        task_handler=MyTaskHandler(),
        api_base_url="https://myapi.com/api",
        api_token="my-token"
    )
    
    # Start processing
    await worker.start()
  4. Run as a script:

    servicebus-worker \
        --connection-string "Endpoint=sb://..." \
        --queue my-queue \
        --handler my_module.MyTaskHandler \
        --api-url https://myapi.com/api \
        --api-token my-token

Message Format

The framework expects ServiceBus messages in this format:

{
  "task_id": "unique-task-identifier",
  "task_type": "my_task_type",
  "task_data": {
    "key": "value",
    "param": "data"
  }
}

API Integration

The framework automatically reports task status to your REST API:

  • Start: POST /tasks/{task_id}/status/ with {"status": "running"}
  • Success: POST /tasks/{task_id}/status/ with {"status": "success", "result": {...}}
  • Error: POST /tasks/{task_id}/status/ with {"status": "failed", "error": "..."}

Configuration

All configuration can be done via environment variables or constructor parameters:

export SERVICEBUS_CONNECTION_STRING="Endpoint=sb://..."
export SERVICEBUS_QUEUE_NAME="my-queue"
export API_BASE_URL="https://myapi.com/api"
export API_TOKEN="my-token"

Docker Usage

The framework is designed to run in containers:

FROM python:3.11-slim
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD ["python", "-m", "servicebus_framework.worker"]

Django Integration

The framework provides utilities to integrate with your existing Django application:

  1. Task Status API: Endpoints to receive task status updates
  2. Task Submission: Methods to submit tasks to ServiceBus from Django
  3. Follow-up Scheduling: Queue additional tasks after completion

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

License

MIT License

sb-framework

About

Servicebus Framework is an very easy lightweight framework to use with Django applications in container apps

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors