From 1b614ac7111f614a84571c3bb6190de40866d140 Mon Sep 17 00:00:00 2001 From: zachdev Date: Tue, 27 Feb 2024 01:55:10 -0600 Subject: [PATCH] schedule worker mode --- .priv_env | 0 config/config.exs | 2 ++ lib/diode.ex | 2 +- lib/schedule.ex | 48 +++++++++++++++++++++++++++++++++++++++++++++++ mix.exs | 5 +++-- mix.lock | 1 + 6 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 .priv_env create mode 100644 lib/schedule.ex diff --git a/.priv_env b/.priv_env new file mode 100644 index 0000000..e69de29 diff --git a/config/config.exs b/config/config.exs index 901df5e..7574536 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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") diff --git a/lib/diode.ex b/lib/diode.ex index f488ff9..60f3d6a 100644 --- a/lib/diode.ex +++ b/lib/diode.ex @@ -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 diff --git a/lib/schedule.ex b/lib/schedule.ex new file mode 100644 index 0000000..8dde4fd --- /dev/null +++ b/lib/schedule.ex @@ -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 diff --git a/mix.exs b/mix.exs index 9555d3c..f9476b4 100644 --- a/mix.exs +++ b/mix.exs @@ -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 @@ -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}, diff --git a/mix.lock b/mix.lock index 4482ce3..d82c86b 100644 --- a/mix.lock +++ b/mix.lock @@ -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"}, }