Skip to content

Commit

Permalink
schedule worker mode
Browse files Browse the repository at this point in the history
  • Loading branch information
zachongit committed Feb 27, 2024
1 parent 56ba6fa commit 1b614ac
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 3 deletions.
Empty file added .priv_env
Empty file.
2 changes: 2 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ config :logger,

config :logger, :console, format: "$time $metadata[$level] $message\n"

config :elixir, :time_zone_database, Tz.TimeZoneDatabase

case Mix.env() do
:benchmark ->
System.put_env("SEED", "none")
Expand Down
2 changes: 1 addition & 1 deletion lib/diode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ defmodule Diode do
base_children ++ network_children
end

children = children ++ [worker(Chain.Worker, [worker_mode()])]
children = children ++ [worker(Chain.Worker, [worker_mode()])] ++ [Schedule]
Supervisor.start_link(children, strategy: :rest_for_one, name: Diode.Supervisor)
end

Expand Down
48 changes: 48 additions & 0 deletions lib/schedule.ex
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
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
defmodule Diode.Mixfile do
use Mix.Project

@vsn "1.1.0"
@full_vsn "v1.1.0"
@vsn "1.0.3"
@full_vsn "v1.0.3-73-g56ba6fa-dirty"
@url "https://github.com/diodechain/diode_server"

def project do
Expand Down Expand Up @@ -98,6 +98,7 @@ defmodule Diode.Mixfile do
{:while, "~> 0.2"},
{:httpoison, "~> 2.0"},
{:oncrash, "~> 0.0"},
{:tz, "~> 0.26.5"},

# linting
{:dialyxir, "~> 1.1", only: [:dev], runtime: false},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"sqlitex": {:git, "https://github.com/diodechain/sqlitex.git", "17bc3cb6ca1fe7ff7ebb65dd24ff97b1630f3df1", []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
"telemetry": {:hex, :telemetry, "1.0.0", "0f453a102cdf13d506b7c0ab158324c337c41f1cc7548f0bc0e130bbf0ae9452", [:rebar3], [], "hexpm", "73bc09fa59b4a0284efb4624335583c528e07ec9ae76aca96ea0673850aec57a"},
"tz": {:hex, :tz, "0.26.5", "bfe8efa345670f90351c5c31d22455d0307c5d9895fbdede7deeb215a7b60dbe", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:mint, "~> 1.5", [hex: :mint, repo: "hexpm", optional: true]}], "hexpm", "c4f9392d710582c7108b6b8c635f4981120ec4b2072adbd242290fc842338183"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
"while": {:hex, :while, "0.2.3", "71a85cca7c979baad2d2968d390dda41527056b81c22a5b422e8cc4d02697f30", [:mix], [], "hexpm", "49e40bd15c219325e2586d1d7166b561e74b90bc02f2ad35890b0346d37ebadb"},
}

0 comments on commit 1b614ac

Please sign in to comment.