gen_task v0.1.2 GenTask behaviour

This module provides helper functions and extended GenServer behaviour to run concurrent tasks with guarantee that error results can be processed.

You need to implement two callback functions:

  • run/1 that defines task business logic
  • handle_info/3 that processes task result

Summary

Functions

Start function as a tack under Task.Supervisor without linking it to a current process and yield for it’s result

Same as start_task/2 but allows to call function in a module

Callbacks

This function will should be used to process task result

Callback function that should implement worker function that must be securely processed

Functions

start_task(fun, timeout \\ 30000)
start_task((... -> any), timeout) ::
  {:ok, term} |
  {:exit, term} |
  {:timeout, Task.t}

Start function as a tack under Task.Supervisor without linking it to a current process and yield for it’s result.

This function can be called without using behaviour, but you will need to match task result manually.

A timeout, in milliseconds, can be given with default value of 30_000.

If the time runs out before a message from the task is received, this function will return {:timeout, Task.t} and the monitor will remain active. Therefore yield/2 can be called on task.

This function assumes the task’s monitor is still active or the monitor’s :DOWN message is in the message queue. If it has been demonitored or the message already received, this function will wait for the duration of the timeout awaiting the message.

If you intend to shut the task down if it has not responded within timeout milliseconds, you should chain this together with shutdown/1. (This is default implementation for handle_result(:timeout, reason, state).)

start_task(module, fun, args, timeout \\ 30000)
start_task(module, atom, [term], timeout) ::
  {:ok, term} |
  {:exit, term} |
  {:timeout, Task.t}

Same as start_task/2 but allows to call function in a module.

Callbacks

handle_result(status, result, state)
handle_result(status :: atom, result :: term, state :: term) ::
  {:noreply, new_state} |
  {:noreply, new_state, timeout | :hibernate} |
  {:stop, reason :: term, new_state} when new_state: term

This function will should be used to process task result.

Result should be same as GenServer.handle_info/2.

run(state)
run(state :: term) :: term

Callback function that should implement worker function that must be securely processed.