Skip to main content

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


Download files

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

Source Distribution

unrealon-0.1.2.tar.gz (38.2 kB view details)

Uploaded Source

Built Distribution

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

unrealon-0.1.2-py3-none-any.whl (73.3 kB view details)

Uploaded Python 3

File details

Details for the file unrealon-0.1.2.tar.gz.

File metadata

  • Download URL: unrealon-0.1.2.tar.gz
  • Upload date:
  • Size: 38.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for unrealon-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f7c4f442ac69ac622b66ce047d831d6606dfd6e10575ebdce6354a214fb5b82e
MD5 98f7503029f315f9c075fa66789444ff
BLAKE2b-256 ed7959e82086c5de2a1ddadab3f07f5059e282c0346741dc2d9fdcd7b8f7eaed

See more details on using hashes here.

File details

Details for the file unrealon-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: unrealon-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 73.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for unrealon-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2afcdad36bcd046aeccba3e3706acff1219d2b7c85d6d4105cef702e775ede3a
MD5 aa1135950688f545de9fe732bb27fd26
BLAKE2b-256 6a9d6aea330795688751214696dd406fd08402709f30fa62158dfe0c9c0883de

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