Expand description
Work that must not hold up the plugin’s hooks.
A plugin runs one call at a time, so a hook that waits for a database or
a web service keeps every other hook waiting. Background workers take
that work: each is a separate instance of the component, started when the
manifest sets [background] workers. The pattern has three steps (the
crate documentation has a complete example):
- In a decision hook (
login,initial_server,before_transfer,command,chatorkick), calldeferfor a ticket andrunwith the job’s payload, remember the ticket under the job’s handle and return. The hook’s own return value is ignored. - A worker calls
Plugin::workwith the payload. It may block. Plugin::job_donereceives the handle and the result on the main instance; callresolvewith the ticket and the answer.
The deferred hook’s deadline keeps running: an answer that comes too late
counts as a missed deadline, and resolve then returns false. run
also works without defer, from any hook, for work nobody waits for.