Run your own code
Embed a worker in your backend and run your own code when one of your rules fires.
Public workers only run the four actions. On a worker you run yourself, you can also run your own code when one of your rules fires: send an email, post to Slack, update a database.
import { startWorker } from 'arkiv-triggers/worker'
const worker = await startWorker({
privateKey: process.env.WORKER_KEY as `0x${string}`,
accessKey: process.env.ARKIV_ACCESS_KEY,
mode: 'own',
onFire: async (rule, { deadline, firedAt, eventId }) => {
await sendEmail(`${rule.name} fired at block ${firedAt}`)
},
})Then create rules assigned to it: worker: worker.address.
onFireis called only for rules whoseworkeris this worker's address, never for public rules, so a stranger's rule can never run your code.- It runs after the rule's own action. If it throws, the receipt records
failedwitherror: "onFire: …". - Keep it inside the worker's time budget (a few seconds).
eventIdis stable across retries, for de-duplication.
stop() finishes actions in flight and stops the loop:
process.on('SIGTERM', async () => { await worker.stop(); process.exit(0) })See Worker reference for every option.