Skip to main content

Sync Laravel queue with Python - simple and lightweight.

Project description

LaraQueue

Simple and lightweight queue synchronization between Python and Laravel using Redis. Process Laravel jobs in Python and vice versa.

Fork Notice: This package is a fork of the original python-laravel-queue by @sinanbekar. This version includes critical bug fixes, comprehensive tests, and updated compatibility with newer dependencies.

NOTE: This package is in beta and only Redis is supported currently. Production usage is not recommended until stable release.

✨ New Features

🛡️ Robust Error Handling (v0.0.3)

The package now includes a comprehensive error handling system:

  • Automatic reconnection to Redis when connection is lost
  • Retry logic with smart delays
  • Detailed logging of all operations and errors
  • Protection against invalid data - worker continues running when encountering problematic messages

🔄 Graceful Shutdown (v0.0.3)

Advanced signal handling for clean worker termination:

  • Signal handlers for SIGINT (Ctrl+C) and SIGTERM (kill)
  • Current job completion - waits for job to finish before stopping
  • Automatic registration - handlers are set up when you call listen()
  • Manual shutdown - programmatically trigger shutdown with queue.shutdown()
  • No job loss - ensures current job completes successfully
import logging

# Enable logging for debugging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('lara_queue')
logger.setLevel(logging.DEBUG)

Installation

pip install LaraQueue

Usage

Listen for jobs in Python

from lara_queue import Queue
from redis import Redis

r = Redis(host='localhost', port=6379, db=0)
queue_python = Queue(r, queue='python')

@queue_python.handler
def handle(data):
    name = data['name']  # job name
    job_data = data['data']  # job data
    print('Processing: ' + job_data['a'] + ' ' + job_data['b'] + ' ' + job_data['c'])

queue_python.listen()

Send jobs from Laravel

<?php
$job = new \App\Jobs\TestJob('hi', 'send to', 'python');
dispatch($job)->onQueue('python');

Send jobs to Laravel from Python

from lara_queue import Queue
from redis import Redis

r = Redis(host='localhost', port=6379, db=0)
queue_laravel = Queue(r, queue='laravel')
queue_laravel.push('App\\Jobs\\TestJob', {'a': 'hello', 'b': 'send to', 'c': 'laravel'})

TestJob in Laravel

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class TestJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $a, $b, $c;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($a, $b, $c)
    {
        $this->a = $a;
        $this->b = $b;
        $this->c = $c;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Log::info('TEST: ' . $this->a . ' ' . $this->b . ' ' . $this->c);
    }
}

Process jobs in Laravel

You need to :listen (or :work) the preferred queue name to handle jobs sent from Python in Laravel.

php artisan queue:listen --queue=laravel

Graceful Shutdown Example

import logging
from lara_queue import Queue
from redis import Redis

# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

r = Redis(host='localhost', port=6379, db=0)
queue = Queue(r, queue='python_worker')

@queue.handler
def handle_job(data):
    logger.info(f"Processing job: {data['name']}")
    # Simulate some work
    import time
    time.sleep(5)
    logger.info("Job completed!")

logger.info("Worker starting...")
logger.info("Press Ctrl+C to trigger graceful shutdown")
logger.info("Current job will complete before stopping")

try:
    queue.listen()  # Signal handlers auto-registered
except KeyboardInterrupt:
    logger.info("Worker stopped gracefully")

Manual Shutdown Example

queue = Queue(r, queue='test')

@queue.handler
def handle_job(data):
    # Process job
    process_data(data)
    
    # Trigger shutdown programmatically
    if should_stop():
        queue.shutdown()

queue.listen()

Error Handling Example

from lara_queue import Queue
from redis import Redis
from redis.exceptions import ConnectionError

try:
    r = Redis(host='localhost', port=6379, db=0)
    queue = Queue(r, queue='python_worker')
    
    @queue.handler
    def handle_job(data):
        print(f"Processing job: {data['name']}")
    
    queue.listen()  # Worker is now resilient to Redis errors!
    
except ConnectionError as e:
    print(f"Failed to connect to Redis: {e}")
except KeyboardInterrupt:
    print("Worker stopped gracefully")

Features

  • Redis driver support - Queue communication between Python and Laravel
  • Bidirectional job processing - Send and receive jobs in both directions
  • PHP object serialization - Compatible with Laravel's job serialization format
  • Event-driven architecture - Simple decorator-based job handlers
  • Automatic reconnection - Resilient to network issues
  • Comprehensive error handling - Detailed logging and error recovery
  • Graceful shutdown - Signal handling (SIGINT, SIGTERM) with job completion
  • Production ready - Battle-tested with extensive test coverage
  • Tested - 40+ unit and integration tests included

Requirements

  • Python 3.7+
  • Redis 4.0+
  • Laravel 8+ (for Laravel side)

Development

# Install development dependencies
pip install -e .
pip install -r requirements-dev.txt

# Run tests
pytest tests/ -v

# Run specific test file
pytest tests/test_error_handling.py -v

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see LICENSE file for details.

Credits

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

laraqueue-0.0.3.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

laraqueue-0.0.3-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file laraqueue-0.0.3.tar.gz.

File metadata

  • Download URL: laraqueue-0.0.3.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.5

File hashes

Hashes for laraqueue-0.0.3.tar.gz
Algorithm Hash digest
SHA256 b348a2da7d8ac34e6a717a79f504fc171129647536bada272b046c73b7295c46
MD5 da2343b21d62cc016965e482278189dd
BLAKE2b-256 f9d230a46ffd4c89f971664878c7d24005ca7d2cbe2decfbe4f9642dd20739f9

See more details on using hashes here.

File details

Details for the file laraqueue-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: laraqueue-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.5

File hashes

Hashes for laraqueue-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5bc6da0e6f002795cfb481e61430948960ed41b8ac31ac090df6a991451df384
MD5 1d7166348fc21b26ffa1fa246740de0e
BLAKE2b-256 2e42b5c7686a0612ed6640e9c66c8ae4c627342ba7ecd76d2aaaa11e329c4d46

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page