Unrealon SDK - Service management for Django backend (registration, heartbeat, logging, commands)
Project description
Unrealon SDK
Parser management SDK for Unrealon platform. Provides registration, heartbeat, logging, and command handling.
Installation
pip install unrealon
Quick Start
from unrealon import ParserClient
# Using context manager (recommended)
with ParserClient(
api_key="pk_live_xxx",
parser_name="my-parser",
) as client:
client.info("Processing started")
for item in items:
process(item)
client.increment_processed()
client.info("Processing complete")
Configuration
Configuration via environment variables (recommended):
export UNREALON_API_KEY=pk_live_xxx
export UNREALON_PARSER_NAME=my-parser
export UNREALON_API_URL=https://api.unrealon.com # optional
Or pass directly to client:
client = ParserClient(
api_key="pk_live_xxx",
parser_name="my-parser",
api_url="https://api.unrealon.com",
source_code="encar",
heartbeat_interval=30,
)
Available Settings
| Env Variable | Default | Description |
|---|---|---|
UNREALON_API_KEY |
required | API key |
UNREALON_PARSER_NAME |
required | Parser identifier |
UNREALON_API_URL |
http://127.0.0.1:8000 |
API endpoint |
UNREALON_HEARTBEAT_INTERVAL |
30 |
Heartbeat interval (seconds) |
UNREALON_LOG_BATCH_SIZE |
100 |
Max logs per batch |
UNREALON_LOG_FLUSH_INTERVAL |
5.0 |
Log flush interval (seconds) |
UNREALON_COMMAND_POLL_INTERVAL |
10 |
Command poll interval (seconds) |
UNREALON_TIMEOUT |
30.0 |
HTTP timeout (seconds) |
Features
Registration & Lifecycle
# Manual control
client = ParserClient(api_key="...", parser_name="...")
parser_id = client.start(description="Production parser")
# ... work ...
client.stop()
# Context manager
with ParserClient(...) as client:
# Auto-registered, auto-deregistered
pass
Heartbeat
Heartbeats are sent automatically in background. Update status as needed:
with ParserClient(...) as client:
# Update metrics
client.update_status(
items_processed=1000,
errors_count=5,
)
# Or use convenience methods
client.increment_processed(100)
client.increment_errors(1)
Logging
Logs are batched and sent automatically:
with ParserClient(...) as client:
client.debug("Debug message")
client.info("Info message")
client.warning("Warning message")
client.error("Error message", exception=e)
client.critical("Critical error")
Commands
Handle commands from server:
def handle_restart(cmd):
print(f"Restart requested: {cmd.params}")
return {"status": "restarted"}
client = ParserClient(...)
client.on_command("restart", handle_restart)
client.on_command("stop", lambda cmd: sys.exit(0))
with client:
# Commands are automatically polled and executed
while True:
process_items()
Async Support
from unrealon import AsyncParserClient
async with AsyncParserClient(
api_key="...",
parser_name="...",
) as client:
await client.send_heartbeat(items_processed=100)
await client.log.log(client.parser_id, "info", "Message")
Individual Services
Use services directly for more control:
from unrealon import (
UnrealonConfig,
RegistrarService,
HeartbeatService,
LoggerService,
CommandService,
)
config = UnrealonConfig(api_key="...", parser_name="...")
# Registration
registrar = RegistrarService(config)
response = registrar.register()
parser_id = response.parser_id
# Heartbeat
heartbeat = HeartbeatService(config)
heartbeat.start_background(parser_id)
# Logging
logger_svc = LoggerService(config)
logger_svc.start_batching(parser_id)
logger_svc.info("Message")
logger_svc.flush()
# Commands
commands = CommandService(config)
pending = commands.poll(parser_id)
for cmd in pending:
commands.acknowledge(cmd.id, "completed")
Error Handling
from unrealon import (
UnrealonError,
AuthenticationError,
RegistrationError,
NetworkError,
)
try:
with ParserClient(...) as client:
pass
except AuthenticationError as e:
print(f"Invalid API key: {e}")
except RegistrationError as e:
print(f"Registration failed: {e}")
except NetworkError as e:
print(f"Network error: {e}")
except UnrealonError as e:
print(f"SDK error: {e}")
Parser Status
from unrealon import ParserClient, ParserStatus
with ParserClient(...) as client:
client.update_status(status=ParserStatus.RUNNING)
# Available statuses:
# ParserStatus.INITIALIZING
# ParserStatus.RUNNING
# ParserStatus.PAUSED
# ParserStatus.STOPPING
# ParserStatus.STOPPED
# ParserStatus.ERROR
Development
# Install dev dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Type checking
mypy src/unrealon
# Linting
ruff check src/unrealon
License
MIT
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 unrealon-0.1.1.tar.gz.
File metadata
- Download URL: unrealon-0.1.1.tar.gz
- Upload date:
- Size: 30.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3401148e5c8c45035add91a6c1962826616e3eec954b99e168f7dbcb77de52a8
|
|
| MD5 |
a4c555fd64dcb31e0700b54d1afd8597
|
|
| BLAKE2b-256 |
f8525993c3fd1b5a189188491922b802d145c6f8942ad41a45341f02283413f1
|
File details
Details for the file unrealon-0.1.1-py3-none-any.whl.
File metadata
- Download URL: unrealon-0.1.1-py3-none-any.whl
- Upload date:
- Size: 58.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ef661bcaff5b076292393765c6e19c8ec54cbf8bfce4718fd88b5957d96b68d
|
|
| MD5 |
0a429cdb9977a4a90dfe2e9da6a7436c
|
|
| BLAKE2b-256 |
87fdcea67383805f50f0bc603dfdabdba8c3e151a5c0fd47b334935e301e444a
|