View Source Skitter.Worker (Skitter v0.7.1)

Worker which manages state and performs computations for an operation and its strategy.

Workers are spawned by strategies to manage state and perform computations for a given operation. Any operation or strategy state not stored in the deployment lives in a worker.

Skitter workers are created by strategies with an initial state. Any messages received by the worker are handled by the Skitter.Strategy.Operation.process/4 hook of its strategy. This callback receives the current worker state and may return a new, updated state to be stored by the worker.

Since strategies can create many separate workers, each worker is created with a role which can be used by the strategy to provide different implementations of Skitter.Strategy.Operation.process/4 based on the worker that received the message.

This module defines the worker types and various functions to deal with workers.

Summary

Types

Placement constraints.

Reference to a created worker.

Worker role.

Worker state.

Worker state or a function which returns a worker state.

Functions

Create a new worker on the local node.

Create a new worker on a remote node.

Send a message to the worker at ref.

Stop worker ref.

Types

@type placement() ::
  nil
  | :local
  | [{:on, node()}]
  | [{:with, ref()}]
  | [{:avoid, ref() | node()}]
  | [{:tagged, Skitter.Remote.tag()}]

Placement constraints.

When spawning a remote worker, it is often desirable to tweak on which node the worker will be placed. This type defines a set of placement constraints which can be passed as an argument to create_remote/4.

The following constraints are defined:

  • nil: No constraints.
  • on: node: Spawn the worker at the specified node.
  • with: ref: Spawn the worker on the same node as the worker identified by ref.
  • avoid: ref: Try to place the worker on a different node than the worker identified by ref.
  • avoid: node: Try to avoid placing the worker on node.
  • tagged: tag: Try to place the worker on a node with a specific t:Skitter.Nodes.tag/0.

Note that it is not always possible to match the desired constraints. When this is the case, a warning will be logged.

@type ref() :: pid()

Reference to a created worker.

@type role() :: atom()

Worker role.

Each worker is tagged with a role which allows the strategy to differentiate between the various workers it creates.

@type state() :: any()

Worker state.

@type state_or_state_fn() :: state() | (-> state())

Worker state or a function which returns a worker state.

Functions which create workers may be created with an initial state, or with a function which returns an initial state.

Functions

Link to this function

create_local(context, state, role)

View Source
@spec create_local(Skitter.Strategy.context(), state_or_state_fn(), role()) ::
  ref() | :error

Create a new worker on the local node.

This will raise when executed on a master node.

Link to this function

create_remote(context, state, role, placement \\ nil)

View Source
@spec create_remote(
  Skitter.Strategy.context(),
  state_or_state_fn(),
  role(),
  placement()
) :: ref()

Create a new worker on a remote node.

The worker will be placed on a random node, subject to the passed placement constraints.

@spec send(ref(), any()) :: :ok

Send a message to the worker at ref.

@spec stop(ref()) :: :ok

Stop worker ref.