Skip to main content

Module background

Module background 

Source
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):

  1. In a decision hook (login, initial_server, before_transfer, command, chat or kick), call defer for a ticket and run with the job’s payload, remember the ticket under the job’s handle and return. The hook’s own return value is ignored.
  2. A worker calls Plugin::work with the payload. It may block.
  3. Plugin::job_done receives the handle and the result on the main instance; call resolve with 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.

Re-exports§

pub use host::defer;
pub use host::is_worker;
pub use host::run;

Functions§

resolve
Answer a deferred hook with the ticket defer returned. Returns false when nobody waits for the ticket any more: the deadline passed, or it was answered already.