task_after v1.2.0 TaskAfter
Documentation for TaskAfter.
This is a library to call a function after a set delay.
It will have the normal variation of the EVM/BEAM system and the underlying OS, so give or take a few milliseconds, like ~12 for Windows.
This keeps an ordered list of tasks to run, it should scale decently, however if it gets too large then you may want to create more Workers to shard the tasks across, this is entirely in your control.
Summary
Functions
cancel_task_after
task_id -> A task ID opts -> Can be:
name: name
|pid: pid
-> Specify a non-global task handler, if unspecified that the application:global_name
must be specifiedcall_timeout: timeout
-> Override the timeout on calling to theTaskAfter.Worker
no_return: true
-> Do not return the id or error, just try to register and forget results otherwiserun_result: pid
-> Sends the result of the task to the specified pid after running it as an async task while returning the Taskrun_result: :in_process
-> Runs the task in theTaskAfter.Worker
process to do internal work, do not use this, returns the value directly thoughrun_result: :async
-> Runs the task as an async task and dismisses the result while returning the Taskrun_result: nil
-> Default: Does not run the task now, just cancels it immediately, returns the callback function
Examples
iex> cb = fn -> 42 end
iex> {:ok, auto_id} = TaskAfter.task_after(500, cb)
iex> {:ok, ^cb} = TaskAfter.cancel_task_after(auto_id)
iex> is_function(cb, 0)
true
change_task_after
task_id -> A task ID opts -> Can be:
name: name
|pid: pid
-> Specify a non-global task handler, if unspecified that the application:global_name
must be specifiedcall_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 *
callback: fun-> Change the callback to this function *
timeout_after_ms: timeout-> Change the timeout to this new value *
send_result: pid-> Sends the result of the task to the specified pid after running it as an async task *
send_result: :in_process-> Runs the task in the [
TaskAfter.Worker](TaskAfter.Worker.html) process to do internal work, do not use this *
send_result: :async-> **Default**: Runs the task as an async task and dismisses the result *
recreate: true-> If this is passed in then
callback,
timeout_after_ms, and
send_result**must** be specified to be able to recreate the task if it is already elapsed. Note: Of course if the task has already run then changing a setting on it won't do anything unless
recreate: trueis passed in. Note: When
recreate: trueis used then
callback,
timeout_after_ms, and
send_resultcan be passed in their value wrapped in a tagged
:defaulttuple like
timeout_after_ms: {:default, 500}` and it will not change the existing value if not recreating but will use the value if it is. ## Examples iex> {:ok, auto_id} = TaskAfter.task_after(200, fn -> 1 end, send_result: self()) iex> {:ok, ^auto_id} = TaskAfter.change_task_after(auto_id, callback: fn -> 2 end) iex> assert_receive(2, 300) iex> receive do m -> m after 1 -> :no_message end :no_message
task_after
timeout_after_ms -> integer millisecond timeout callback -> The 0-argcallback function opts -> Can be:
name: name
|pid: pid
-> Specify a non-global task handler, if unspecified that the application:global_name
must be specifiedid: id
-> A unique id, if nil or unspecified then it is auto-generatedcall_timeout: timeout
-> Override the timeout on calling to theTaskAfter.Worker
no_return: true
-> Do not return the id or error, just try to register and forget results otherwisesend_result: pid
-> Sends the result of the task to the specified pid after running it as an async tasksend_result: :in_process
-> Runs the task in theTaskAfter.Worker
process to do internal work, do not use thissend_result: :async
-> Default: Runs the task as an async task and dismisses the result
Examples
iex> {:ok, _auto_id} = TaskAfter.task_after(500, fn -> 21 end)
iex> :ok
:ok
iex> {:ok, :myid} = TaskAfter.task_after(500, fn -> 42 end, send_result: self(), id: :myid)
iex> receive do m -> m after 5 -> :blah end
:blah
iex> receive do m -> m after 1000 -> :blah end
42