Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/noredink/until_then
Calculates offsets to regularly scheduled events.
https://github.com/noredink/until_then
Last synced: about 2 months ago
JSON representation
Calculates offsets to regularly scheduled events.
- Host: GitHub
- URL: https://github.com/noredink/until_then
- Owner: NoRedInk
- License: mit
- Created: 2016-04-08T22:19:31.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-04-11T15:40:08.000Z (over 8 years ago)
- Last Synced: 2024-04-14T04:46:06.818Z (9 months ago)
- Language: Elixir
- Size: 9.77 KB
- Stars: 5
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# UntilThen
This library tells you how many milliseconds to the next occurrence of a
scheduled event. This is very convenient to combine with `:timer.sleep/1`
or `Process.send_after/3` as a means of repeatedly invoking some code on a
schedule and not having those invocations drift.## Installation
Add `UntilThen` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[{:until_then, "~> 0.0.1"}]
end
```Ensure that `UntilThen` is started before your application:
```elixir
def application do
[applications: [:until_then]]
end
```## Usage
Using `:timer.sleep/1`:
```elixir
defmodule WeekdayCheckins do
def run_checkins do
UntilThen.next_occurrence(:weekdays, "12:00:00") |> :timer.sleep
checkin
run_checkins
enddef checkin do
# do the work here...
end
end
```Or using `Process.send_after/3`:
```elixir
defmodule WeekdayCheckins do
def setup_checkins do
worker = spawn(__MODULE__, :run_checkins, [ ])
spawn(__MODULE__, :schedule_checkins, [worker])
enddef run_checkins do
receive do
{:event, scheduler} ->
checkin
send(scheduler, :done)
run_checkins
end
enddef checkin do
# do the work here...
enddef schedule_checkins(pid) do
delay = UntilThen.next_occurrence(:weekdays, "10:27:00")
Process.send_after(pid, {:event, self}, delay)
receive do
:done -> schedule_checkins(pid)
end
end
end
```