A Python package for creating and dispatching Laravel queue jobs
Project description
Laravel Python SQS Queue
A Python package for creating and dispatching Laravel queue jobs to AWS SQS. This package allows Python applications to seamlessly integrate with Laravel's queue system by generating compatible job payloads.
Features
- Create Laravel-compatible job payloads from Python
- Dispatch jobs directly to AWS SQS queues
- Support for job options like max tries, timeout, backoff, and tags
- Builder pattern for easy job configuration
- LocalStack support for development
- Comprehensive test coverage
Installation
pip install laravel-python-sqs-queue
Requirements
- Python 3.8+
- phpserialize>=1.3
- boto3>=1.26.0
Quick Start
from laravel_queue import LaravelJob
# Create and configure a job
job = (LaravelJob("App\\Jobs\\ExampleJob", {
"message": "Hello from Python",
"data": {
"user_id": 123,
"action": "test"
}
})
.set_max_tries(3)
.set_timeout(60)
.add_tags("example", "test")
)
# Dispatch to SQS
response = job.dispatch_to_sqs(
queue_url="YOUR_SQS_QUEUE_URL",
aws_region="your-region"
)
Detailed Usage
Creating a Job
The LaravelJob class uses a builder pattern for configuration:
from laravel_queue import LaravelJob
from datetime import datetime, timezone
job = (LaravelJob("App\\Jobs\\UserJob", {
"user_id": 123,
"action": "process"
})
.set_max_tries(3) # Maximum number of retry attempts
.set_timeout(60) # Job timeout in seconds
.set_backoff(5) # Delay between retry attempts
.set_delay(10) # Initial delay before processing
.set_retry_until(datetime.now(timezone.utc)) # Retry until timestamp
.add_tags("user", "process") # Add tags for tracking
.add_middleware("custom-middleware") # Add Laravel middleware
)
Dispatching to SQS
# Basic dispatch
response = job.dispatch_to_sqs(
queue_url="https://sqs.us-east-1.amazonaws.com/123456789012/my-queue", # Replace with your actual queue URL
aws_region="us-east-1"
)
# With custom endpoint (e.g., LocalStack)
response = job.dispatch_to_sqs(
queue_url="http://localhost:4566/000000000000/default",
aws_region="ap-northeast-1",
endpoint_url="http://localhost:4566"
)
# With explicit credentials
response = job.dispatch_to_sqs(
queue_url="https://sqs.us-east-1.amazonaws.com/123456789012/my-queue", # Replace with your actual queue URL
aws_region="us-east-1",
aws_access_key_id="your-key",
aws_secret_access_key="your-secret"
)
Legacy Functions
For backward compatibility, the package also provides standalone functions:
from laravel_queue import create_laravel_job_payload, push_laravel_job_to_sqs
# Create payload only
payload = create_laravel_job_payload(
"App\\Jobs\\ExampleJob",
{"message": "test"}
)
# Create and dispatch
response = push_laravel_job_to_sqs(
"your-queue-url",
"App\\Jobs\\ExampleJob",
{"message": "test"},
aws_region="your-region"
)
Development
Setup
- Clone the repository:
git clone https://github.com/nguyenkhactien/laravel-python-sqs-queue.git
cd laravel-python-sqs-queue
- Create a virtual environment and install dependencies:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -e ".[dev]"
Running Tests
The package includes comprehensive tests using pytest. For local testing, you'll need LocalStack running:
- Start LocalStack (requires Docker):
docker run --rm -p 4566:4566 localstack/localstack
- Run tests:
pytest
Type Checking
mypy src/laravel_queue
Code Formatting
black src/laravel_queue tests
isort src/laravel_queue tests
Testing with Laravel
Laravel Job Classes
To test the integration, you can use these sample Laravel job classes:
ExampleJob
<?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 ExampleJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public string $message;
public array $data;
/**
* Create a new job instance.
*/
public function __construct(string $message, array $data)
{
$this->message = $message;
$this->data = $data;
}
/**
* Execute the job.
*/
public function handle(): void
{
Log::info('Message from Python: ' . $this->message);
Log::info('Data from Python:', $this->data);
if (isset($this->data['user_id'])) {
Log::info('Processing for user: ' . $this->data['user_id']);
}
if (isset($this->data['action'])) {
Log::info('Action to perform: ' . $this->data['action']);
}
}
}
TestJob
<?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 string $message;
public int $user_id;
/**
* Create a new job instance.
*/
public function __construct(string $message, int $user_id)
{
$this->message = $message;
$this->user_id = $user_id;
}
/**
* Execute the job.
*/
public function handle(): void
{
Log::info('Processing TestJob', [
'message' => $this->message,
'user_id' => $this->user_id
]);
}
}
Testing Integration
-
Place these job classes in your Laravel project under
app/Jobs/. -
From Python, you can dispatch jobs that match these classes:
# Testing ExampleJob
job = (LaravelJob("App\\Jobs\\ExampleJob", {
"message": "Hello from Python",
"data": {
"user_id": 123,
"action": "test"
}
})
.set_max_tries(3)
.set_timeout(60)
)
# Testing TestJob
job = (LaravelJob("App\\Jobs\\TestJob", {
"message": "Test message",
"user_id": 123
})
.set_max_tries(3)
.set_timeout(60)
)
- Check Laravel logs to verify job execution:
tail -f storage/logs/laravel.log
You should see log entries from the jobs showing the data passed from Python.
LocalStack Testing
For local development, you can use LocalStack to simulate AWS SQS:
- Start LocalStack:
docker run --rm -p 4566:4566 localstack/localstack
- Update your Laravel
.env:
QUEUE_CONNECTION=sqs
AWS_ACCESS_KEY_ID=test
AWS_SECRET_ACCESS_KEY=test
AWS_DEFAULT_REGION=ap-northeast-1
SQS_LOCALSTACK_URL=http://localstack:4566
SQS_PREFIX=http://localstack:4566/000000000000
SQS_QUEUE=default
- Update your
config\queue.php:
'sqs' => [
...
'endpoint' => env('SQS_LOCALSTACK_URL'),
],
- Run Laravel queue worker:
php artisan queue:work sqs
- Dispatch jobs from Python using LocalStack endpoint:
response = job.dispatch_to_sqs(
queue_url="http://localhost:4566/000000000000/default",
aws_region="ap-northeast-1",
endpoint_url="http://localhost:4566",
aws_access_key_id="test",
aws_secret_access_key="test"
)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file laravel_python_sqs_queue-0.2.0.tar.gz.
File metadata
- Download URL: laravel_python_sqs_queue-0.2.0.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0efa1ad6b5edacea96665f6cae81731ceb2422a7719d0e616cd4317296602b5b
|
|
| MD5 |
46651359b8eaa6a8f03c3c07c7aef940
|
|
| BLAKE2b-256 |
d60e929e44f9b9c6498d65703e65e887dacc3c9ceb26b689900742ff3e1b6185
|
File details
Details for the file laravel_python_sqs_queue-0.2.0-py3-none-any.whl.
File metadata
- Download URL: laravel_python_sqs_queue-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
883b97e684b75775a014c7aa81089db220f9d78886d79e8e83fd7f5a575cfaad
|
|
| MD5 |
78c9fadecb7493f3cf2e8e253262c26e
|
|
| BLAKE2b-256 |
9d7a7213018e6d299faf7a895908e19741a84a3f7c9f28e84e38edf04c8a24e8
|