Deadline v0.7.1 Deadline View Source

Deadline is a small library for managing deadlines and for performing deadline propagation across systems. It uses process dictionary both for performance and to make the library more ergonomic.

# Set a deadline in milliseconds
Deadline.set(1_000)

# Perform some work that takes longer than a second
Deadline.work(fn ->
  Service.call()
end)

# Won't be called because we've exceeded our deadline
Deadline.work(fn ->
  Service.call()
end)

if Deadline.reached?() do
  :cancel
else
  do_some_work()
end

Link to this section Summary

Functions

Forces the calling process to exit if the deadline is reached. This will start a new process and that process will live as long as the calling process lives or until the deadline is reached. The extra processes should not present a problem in most cases, but it could present memory pressure in low memory environments.

Returns the deadline context.

Checks if a deadline has been reached or exceeded.

Sets the deadline context. If a integer is passed it is assumed to be the desired deadline in milliseconds. This function also accepts a full deadline context. This is most commonly used when a deadline has already been set and the context needs to be propagated to another BEAM process.

Returns the remaining time before the dealine is reached, in a given unit. Defaults to :millisecond units. If the deadline has been exceeded than the time remaining will be 0.

work(f) deprecated

Link to this section Functions

Link to this function

exit_after(before_exit \\ nil)

View Source

Forces the calling process to exit if the deadline is reached. This will start a new process and that process will live as long as the calling process lives or until the deadline is reached. The extra processes should not present a problem in most cases, but it could present memory pressure in low memory environments.

Accepts a callback that will be executed prior to exiting the calling process.

Returns the deadline context.

Checks if a deadline has been reached or exceeded.

Sets the deadline context. If a integer is passed it is assumed to be the desired deadline in milliseconds. This function also accepts a full deadline context. This is most commonly used when a deadline has already been set and the context needs to be propagated to another BEAM process.

Link to this function

time_remaining(unit \\ :millisecond)

View Source

Returns the remaining time before the dealine is reached, in a given unit. Defaults to :millisecond units. If the deadline has been exceeded than the time remaining will be 0.

This function is deprecated. work/1 is not considered to be a safe operation. You should instead use the other primitives in Deadline, or spawn a `Task` with the specified deadline like so: Task.async(fn -> do_some_work() end) |> Task.await(Deadline.time_remaining()) .