-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule Schedule do | ||
use GenServer | ||
|
||
def start_link(_opts) do | ||
GenServer.start_link(__MODULE__, %{}) | ||
end | ||
|
||
def init(state) do | ||
send(self(), :work) | ||
|
||
{:ok, state} | ||
end | ||
|
||
def handle_info(:work, state) do | ||
set_worker_mode() | ||
|
||
# Schedule next check for 15 minutes from now | ||
# Process.send_after(self(), :work, 15 * 60 * 1000) | ||
Process.send_after(self(), :work, 1000) | ||
|
||
{:noreply, state} | ||
end | ||
|
||
@max_worker_mode 300 | ||
@min_worker_mode 200 | ||
|
||
defp set_worker_mode() do | ||
{:ok, datetime_now} = DateTime.now("America/Chicago") | ||
|
||
is_day? = fn day -> | ||
Date.day_of_week(datetime_now) == day | ||
end | ||
|
||
is_night? = fn evening, morning -> | ||
datetime_now.hour >= evening || datetime_now.hour < morning | ||
end | ||
|
||
worker_mode = | ||
cond do | ||
is_day?.(6) -> @max_worker_mode | ||
is_day?.(7) -> @max_worker_mode | ||
is_night?.(20, 8) -> @max_worker_mode | ||
true -> @min_worker_mode | ||
end | ||
|
||
Chain.Worker.set_mode(worker_mode) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters