Where each part goes
An actor holds the rules and the records, and it runs the same way on every machine that checks it. That’s why it can’t reach the network, read the clock or open a file itself. A runner, a machine that does work for actors, makes those calls for it and hands back the answer.
Where your code runs explains the last two.
Before: a cron script
Here’s a typical starting point. Once an hour, cron runs a script that reads a key from the environment, asks an API for an account’s follower count, and appends it to a file:- cron becomes
@every(HOUR) requests.getbecomesrunner.httpos.environ["EXAMPLE_API_KEY"]becomesrunner.secret("EXAMPLE_API_KEY", ...)counts.jsonbecomes the actor’s stateACCOUNTbecomes state too, set by a handler only the owner can call
After: an actor
Create a project next to your code withcowboy init follower_tracker, and replace actors/follower_tracker/main.py with:
- Starting it.
tracksets the account and starts the hourly schedule. After that,checkruns every hour with nothing on your side, and the actor pays for each run from its balance. - The request.
await runner.http(...)hands the request to a runner, andcheckpicks up from the same line when the answer arrives. The time comes fromruntime.get_timestamp_ms(), nottime.time(). - The key. It’s in the secret store, and the job carries only
"Bearer "and the key’s name (Call an API with a key). - The file.
countslives in the actor’s state, which anyone can read and anyone can query through thecountshandler. Keep private data in a private volume instead (An actor’s files).
awaits, so a script that calls an API once per item either makes one call that returns them all, or handles one item per run. And code that depends on randomness or the current time has to get them from the SDK (What actor code can and can’t do).
Test it the same way
Your old tests probably mockedrequests. On Cowboy, a test plays the API’s part: it moves time forward, sees the job the actor sent, and answers it. Replace tests/test_follower_tracker.py with:
Output
chain.advance_block(3600) moves time forward an hour, about one second a block, so the schedule fires once.