Example — Python Worker
A background worker that processes messages from a queue.
Project structure
worker/
├── worker.py
└── requirements.txtCode
requirements.txt
redis
celeryworker.py
import os
import time
import redis
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Connect to Redis (configure in the console if needed)
redis_url = os.environ.get('REDIS_URL', 'redis://localhost:6379')
redis_client = redis.from_url(redis_url)
def process_message(message):
"""Process a single message."""
logger.info(f"Processing: {message}")
time.sleep(2) # Simulate work
logger.info(f"Done: {message}")
return True
def main():
logger.info("Worker started")
while True:
# Block until a message is available
message = redis_client.blpop('tasks', timeout=5)
if message:
data = message[1].decode('utf-8')
process_message(data)
if __name__ == '__main__':
main()Console configuration
Configure the worker in the console with GitHub source and the required environment variables.
Deploy
- Push the code to a GitHub repository.
- In the console, choose Project Source as GitHub Repository.
- Select the main branch and create the project.
- Push to update the worker automatically.
Push a task
# From another service or your local machine
redis-cli -h $REDIS_HOST LPUSH tasks '{"type": "email", "to": "[email protected]"}'Expected logs
INFO:worker:Worker started
INFO:worker:Processing: {"type": "email", "to": "[email protected]"}
INFO:worker:Done: {"type": "email", "to": "[email protected]"}