Skip to content

Workers ​

What is a Worker? ​

A Worker is a Python script that runs continuously in a cloud sandbox. You write the code in a Worker block on the canvas, click Deploy, and it starts executing on the server.

Workers follow a tick-based model: your code runs at a configurable interval (1 second to 1 hour), with persistent state between ticks.

Code Structure ​

Every worker defines up to 3 functions:

python
def setup(ctx):
    """Called ONCE when the worker starts.
    Use this for initialization: set initial state, log startup message."""
    ctx.state.set("counter", 0)
    ctx.log.info("Worker started")


def tick(ctx):
    """Called every tick_interval seconds.
    This is where your main logic lives."""
    count = ctx.state.get("counter", 0) + 1
    ctx.state.set("counter", count)
    ctx.log.info(f"Tick #{count}")


def on_error(ctx, error):
    """OPTIONAL. Called when tick() raises an exception.
    Use this for error reporting or cleanup."""
    ctx.log.error(f"Error occurred: {error}")
FunctionRequiredWhen called
setup(ctx)NoOnce, when worker starts
tick(ctx)YesEvery tick_interval seconds
on_error(ctx, error)NoWhen tick() raises an exception

The ctx Object ​

The ctx (context) object is your SDK. It provides access to everything:

AdapterDescriptionRequires connected block
ctx.statePersistent key-value storage (Redis)No
ctx.logLogging (info, warn, error, debug)No
ctx.monitorReal-time dashboard widgetsMonitor block
ctx.httpHTTP GET/POST requestsNo
ctx.bybitBybit exchange trading APIBybit block
ctx.igIG Markets trading APIIG Markets block
ctx.telegramTelegram messagingTelegram block
ctx.filesFile read/write/list/deleteFile Explorer block
ctx.llmLLM queries (Claude, Grok, OpenAI)LLM Agent block

See the full API Reference for all methods.

How to Deploy ​

  1. Add a Worker block to your canvas
  2. Connect it to other blocks via edges (e.g., draw edge to a Bybit block)
  3. Open the Worker block and go to the Code tab
  4. Write your Python code
  5. Click Deploy
  6. Click Start (in the Control tab)

Tick Intervals ​

Configure how often tick() is called:

IntervalUse case
1sReal-time monitoring, HFT
5sActive trading, live dashboards
10sDefault, general purpose
30sModerate polling
60sPeriodic checks
5 minBackground monitoring
15 minSlow updates
1 hourScheduled tasks

You can change the interval without redeploying -- just update it in the Control tab.

Auto-Heal ​

Workers have built-in error recovery:

  • Max Errors: number of consecutive exceptions before auto-stop (default: 5)
  • Auto-Heal: when enabled, the system can automatically fix your code using AI and restart the worker

Each successful tick resets the error counter to 0.

Tick Timeout ​

Each tick() call has a 60 second timeout. If your tick takes longer, it will be killed with a TickTimeoutError. Keep your ticks fast -- use ctx.state to persist progress between ticks if processing large datasets.

State Persistence ​

All state stored via ctx.state is:

  • Kept in Redis during runtime (fast reads/writes)
  • Synced to PostgreSQL on worker startup and shutdown (durability)
  • Available across worker restarts and redeployments

Your state survives worker restarts, code changes, and server reboots.

NodeGraph Documentation