TaskAfter

This is a library to call a function after a set delay. Usage is as simple as: TaskAfter.task_after(500, fn -> do_something_after_500_ms() end)

Installation

Install this package by adding task_after to your list of dependencies in mix.exs:

def deps do
  [
    {:task_after, "~> 1.0.0"},
  ]
end

Global installation

To use this globally without needing to add it to your own supervision tree just add this to your configuration:

config :task_after, global_name: TaskAfter

Feel free to replace the global name of TaskAfter with anything you want. If the global name is unspecified then all usage of TaskAfter must have the :name or :pid options specified.

Local Installation

To use this locally to your application or to give distinct names so you can have different schedulars then just add the TaskAfter.Worker.start_link/1 to your supervision tree as normal, such as via:

children = [
  worker(TaskAfter.Worker, [[name: MyCustomName]]),
]

Note the 2 sets of list elements. You can have a nameless worker by leaving the name option out, such as:

children = [
  worker(TaskAfter.Worker, [[]]),
]

You will have to acquire the PID some other way, such as by querying your supervisor.

Usage

The main interface point is TaskAfter.task_after/2 and TaskAfter.task_after/3 where TaskAfter.task_after/2 just calls TaskAfter.task_after/3 with an empty set of options to use the defaults.

The arguments to TaskAfter.task_after/3 are, in this order:

  1. timeout_after_ms -> integer millisecond timeout
  2. callback -> The 0-arg callback function
  3. opts -> Can be:
    • name: name | pid: pid -> Specify a non-global task handler, if unspecified that the application :global_name must be specified
    • id: id -> A unique id, if nil or unspecified then it is auto-generated
    • call_timeout: timeout -> Override the timeout on calling to the TaskAfter.Worker
    • no_return: true -> Do not return the id or error, just try to register and forget results otherwise
    • send_result: pid -> Sends the result of the task to the specified pid
    • send_result: :in_process -> Runs the task in the TaskAfter.Worker process to do internal work, do not use this

You can also cancel a task via TaskAfter.cancel_task_after/1 and TaskAfter.cancel_task_after/2 where TaskAfter.cancel_task_after/1 just defaults to having an empty opts list.

The arguments to TaskAfter.cancel_task_after/2 are, in this order:

  1. task_id -> A task id
  2. opts -> Can be:
    • name: name | pid: pid -> Specify a non-global task handler, if unspecified that the application :global_name must be specified
    • call_timeout: timeout -> Override the timeout on calling to the TaskAfter.Worker
    • no_return: true -> Do not return the id or error, just try to register and forget results otherwise
    • run_result: pid -> Sends the result of the task to the specified pid after running it as an async task while returning the Task
    • run_result: :in_process -> Runs the task in the TaskAfter.Worker process to do internal work, do not use this, returns the value directly though
    • run_result: :async -> Runs the task as an async task and dismisses the result while returning the Task
    • run_result: nil -> Default: Does not run the task now, just cancels it immediately, returns the callback function

You can change a task via TaskAfter.change_task_after/2.

The arguments to TaskAfter.change_task_after/2 are, in this order:

  1. task_id -> A task ID
  2. opts -> Can be: