A simple, parameter-driven worker framework with internal loops and file-based monitoring
Project description
Mini-Worker
A simple, parameter-driven worker framework with internal loops and file-based monitoring.
Features
- Parameter-driven: No external configuration files required - all settings passed as parameters
- Internal loops: Workers run continuously with configurable wait times between iterations
- File-based monitoring: Status and logs written to specified directories
- Process tracking: Creates worker_id files with process IDs for monitoring
- Worker management: Built-in manager for starting, stopping, and monitoring workers
- Simple CLI: Easy command-line interface for running workers
- Flexible execution: Support for batch processing and continuous operation
- Statistics tracking: Built-in performance monitoring and rate calculation
- Compatibility: Drop-in replacement for existing worker systems
Installation
pip install mini-worker
Quick Start
1. Create a Worker
from mini_worker import BaseMiniWorker
class MyWorker(BaseMiniWorker):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# Initialize your worker-specific components
# Dependencies are imported normally - no injection needed!
def do_work(self):
"""Implement your work logic here"""
with self.track_operation('my_operation'):
print("Doing some work...")
# Your actual work logic
def get_worker_id(self):
return "my_worker"
2. Run via CLI
# Run worker with 5-minute intervals
mini-worker run --worker-class=MyWorker --log-dir=/var/log/workers --wait-seconds=300
# Run with custom parameters
mini-worker run --worker-class=MyWorker --log-dir=/tmp/logs --wait-seconds=60 \
--worker-params='{"param1": "value1", "param2": 123}'
3. Run Programmatically
worker = MyWorker(
worker_id="my_worker_001",
log_dir="/var/log/workers",
stats_dir="/var/log/workers",
wait_seconds=300
)
worker.run()
4. Use Worker Manager
from mini_worker import MiniWorkerManager
# Create manager
manager = MiniWorkerManager(log_dir="/var/log/workers")
# Register workers
manager.register_worker("my_worker", "mymodule.MyWorker")
# Start worker
manager.start_worker_with_params("my_worker", {"param1": "value1"})
# Check status
status = manager.get_worker_status("my_worker")
# Stop worker
manager.stop_worker("my_worker")
CLI Commands
mini-worker run
Run a worker directly.
Options:
--worker-class: Python class name of the worker to run (required)--log-dir: Directory for log files (default: current directory)--stats-dir: Directory for stats files (default: same as log-dir)--wait-seconds: Seconds to wait between work cycles (default: 600)--worker-params: JSON string of worker-specific parameters--max-cycles: Maximum number of work cycles before stopping (default: unlimited)--worker-id: Override worker ID (default: use worker class default)
mini-worker status
Check worker status.
Options:
--stats-dir: Directory containing stats files (default: current directory)--worker-id: Show status for specific worker ID--format: Output format - 'text' or 'json' (default: text)
Monitoring
Mini-worker creates several files for monitoring:
{worker_id}.log: Worker log file with rotating logs{worker_id}.stats: Human-readable statistics{worker_id}.json: Detailed statistics in JSON format{worker_id}.pid: Process ID file for monitoring
Migration from Existing Workers
Mini-worker is designed as a drop-in replacement for existing worker systems:
# Before (existing worker)
from src.workers.base_worker import BaseWorker
class MyWorker(BaseWorker):
def __init__(self):
super().__init__()
# Same initialization code
def do_work(self):
with self.calc_one('my_operation'):
# Same work logic
pass
# After (mini-worker)
from mini_worker import BaseMiniWorker
class MyWorker(BaseMiniWorker):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# Same initialization code
def get_worker_id(self):
return "my_worker"
def do_work(self):
with self.calc_one('my_operation'): # Still works!
# Same work logic
pass
Key changes:
- Import
BaseMiniWorkerinstead ofBaseWorker - Add
**kwargsto__init__and callsuper().__init__(**kwargs) - Implement
get_worker_id()method calc_one()still works for compatibility, or usetrack_operation()
Examples
See the examples/ directory for complete working examples.
License
MIT License
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 mini_worker-0.1.1.tar.gz.
File metadata
- Download URL: mini_worker-0.1.1.tar.gz
- Upload date:
- Size: 35.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
05a693a9866e822a73b98d05cd7e4cf6aae1904b1f62d6da6a09c33fd14bcd0f
|
|
| MD5 |
a3a279f384f686fa932c382e6dabe50b
|
|
| BLAKE2b-256 |
b61dd7492778668fce7afc3f17b750b2d45f6144f5b8bb0d707ea67fccdb14af
|
File details
Details for the file mini_worker-0.1.1-py3-none-any.whl.
File metadata
- Download URL: mini_worker-0.1.1-py3-none-any.whl
- Upload date:
- Size: 17.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4edc7d03ec0d1cb6071123376e35e5a3f723572396406a53108ff9e9f9f6d614
|
|
| MD5 |
dfa457ab0a5a7ed7f2d754021f6c2854
|
|
| BLAKE2b-256 |
aa7d99c0b7274478c9bc320e7151dc4ea485a23925052db638be830a6bb43c47
|