Skip to content

Commit

Permalink
Merge pull request akira#124 from akira/exq_scheduler
Browse files Browse the repository at this point in the history
add support for exq_scheduler
  • Loading branch information
ananthakumaran authored Aug 16, 2022
2 parents cdf4992 + cb90c80 commit 964cd5c
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 5 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,23 @@ defmodule DemoWeb.Router do
end
```

## Exq Scheduler

ExqUI provides support for [Exq Scheduler](https://github.com/activesphere/exq-scheduler). It can be enabled by
giving a name to exq scheduler and specifying the same name in exq ui config.

```elixir
config :exq_scheduler,
name: ExqScheduler

config :exq_ui,
exq_scheduler_name: ExqScheduler
```

## Development

```sh
mix setup # on first run
mix run dev.exs
mix run --no-halt dev.exs
open http://localhost:4000/exq
```
30 changes: 29 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ config :exq,
namespace: "exq"

config :exq_ui,
api_name: Exq.Api
api_name: Exq.Api,
exq_scheduler_name: ExqScheduler

config :logger, :console,
format: "$time $metadata[$level] $message\n",
Expand All @@ -20,3 +21,30 @@ config :phoenix, :json_library, Jason
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs"

config :exq_scheduler, :storage,
exq_namespace: "exq",
json_serializer: Jason

config :exq_scheduler, :redis,
name: ExqScheduler.Redis.Client,
child_spec: {Redix, [host: "127.0.0.1", port: 6379, name: ExqScheduler.Redis.Client]}

config :exq_scheduler, name: ExqScheduler

config :exq_scheduler, :schedules,
hard_worker: %{
description: "Schedule a Hardworker job every 2 minutes",
cron: "*/2 * * * * Asia/Jakarta",
class: "Hardworker",
args: [435_493],
queue: "hard"
},
soft_worker: %{
description: "Schedule a Hardworker job every 3 minutes",
cron: "*/3 * * * *",
class: "Hardworker",
include_metadata: true,
args: [],
queue: "soft"
}
2 changes: 1 addition & 1 deletion dev.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Logger.configure(level: :debug)
Application.ensure_all_started(:exq_scheduler)

# Configures the endpoint
Application.put_env(:exq_ui, DemoWeb.Endpoint,
Expand Down Expand Up @@ -81,4 +82,3 @@ Task.async(fn ->
{:ok, _} = Supervisor.start_link(children, strategy: :one_for_one)
Process.sleep(:infinity)
end)
|> Task.await(:infinity)
10 changes: 9 additions & 1 deletion lib/exq_ui_web/live/busy_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ defmodule ExqUIWeb.BusyLive.Index do
@impl true
def handle_event(
"signal",
%{"signal" => %{"name" => "TSTP", "node_id" => node_id}},
%{
"signal" => %{"name" => "TSTP", "node_id" => node_id},
"_target" => ["signal", "quiet"]
},
socket
) do
:ok = Queue.send_signal(node_id, "TSTP")
Expand All @@ -29,4 +32,9 @@ defmodule ExqUIWeb.BusyLive.Index do

{:noreply, socket}
end

@impl true
def handle_event("signal", _, socket) do
{:noreply, socket}
end
end
5 changes: 5 additions & 0 deletions lib/exq_ui_web/live/components/nav_bar_component.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<li class="nav-item">
<%= nav_link(@socket, "Dead", Routes.dead_index_path(@socket)) %>
</li>
<%= if Application.get_env(:exq_ui, :exq_scheduler_name) do %>
<li class="nav-item">
<%= nav_link(@socket, "Recurring", Routes.recurring_index_path(@socket)) %>
</li>
<% end %>
</ul>
</div>
<form class="d-flex align-items-center" phx-target="stats">
Expand Down
44 changes: 44 additions & 0 deletions lib/exq_ui_web/live/recurring_live/index.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
defmodule ExqUIWeb.RecurringLive.Index do
@moduledoc false
use ExqUIWeb, :live_view
@compile {:no_warn_undefined, [ExqScheduler]}

@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, schedules_details())}
end

@impl true
def handle_event("enqueue_now", %{"name" => name}, socket) do
:ok = ExqScheduler.enqueue_now(scheduler(), String.to_atom(name))
{:noreply, assign(socket, schedules_details())}
end

@impl true
def handle_event(
"active",
%{"active" => %{"name" => name, "enabled" => enabled}, "_target" => ["active", "toggle"]},
socket
) do
case enabled do
"false" -> :ok = ExqScheduler.disable(scheduler(), String.to_atom(name))
"true" -> :ok = ExqScheduler.enable(scheduler(), String.to_atom(name))
end

{:noreply, assign(socket, schedules_details())}
end

@impl true
def handle_event("active", _, socket) do
{:noreply, socket}
end

defp schedules_details() do
schedules = ExqScheduler.schedules(scheduler())
%{items: schedules}
end

defp scheduler do
Application.get_env(:exq_ui, :exq_scheduler_name, ExqScheduler)
end
end
48 changes: 48 additions & 0 deletions lib/exq_ui_web/live/recurring_live/index.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<%= live_component ExqUIWeb.StatsComponent, id: "stats" %>

<div>
<div class="row">
<div class="col-12">
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Active</th>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Cron</th>
<th scope="col">Timezone</th>
<th scope="col">Last Time</th>
<th scope="col">Next Time</th>
<th scope="col">Queue</th>
<th scope="col">Module</th>
<th scope="col">Arguments</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<%= for item <- @items do %>
<tr>
<td>
<%= form_for :active, "#", [id: "schedule-#{item.schedule.name}", phx_change: :active, class: "mb-0 form-check form-switch form-check-inline", style: "margin: auto;display: flex;justify-content: center"], fn f -> %>
<%= hidden_input(f, :name, value: item.schedule.name) %>
<%= hidden_input(f, :enabled, value: !item.schedule.schedule_opts.enabled) %>
<%= checkbox(f, :toggle, value: item.schedule.schedule_opts.enabled, class: "form-check-input", role: "switch", title: "disable") %>
<% end %>
</td>
<td><%= item.schedule.name %></td>
<td class="text-break"><%= item.schedule.description %></td>
<td class="text-nowrap"><%= item.cron %></td>
<td><%= item.schedule.timezone %></td>
<td class="text-nowrap"><%= human_time(item.last_run) %></td>
<td class="text-nowrap" style={!item.schedule.schedule_opts.enabled && "text-decoration: line-through"} ><%= human_time(item.next_run) %></td>
<td><%= item.schedule.job.queue %></td>
<td><%= item.schedule.job.class %></td>
<td class="text-break"><%= inspect(item.schedule.job.args) %></td>
<td class="text-nowrap"><%= link "Enqueue Now", class: "btn btn-danger btn-sm", to: "#", phx_click: "enqueue_now", phx_value_name: item.schedule.name %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
4 changes: 4 additions & 0 deletions lib/exq_ui_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ defmodule ExqUIWeb.Router do
live "/dead/:score/:jid", ExqUIWeb.DeadLive.Show, :index, route_opts
live "/scheduled", ExqUIWeb.ScheduledLive.Index, :index, route_opts
live "/scheduled/:score/:jid", ExqUIWeb.ScheduledLive.Show, :index, route_opts

if Application.get_env(:exq_ui, :exq_scheduler_name) do
live "/recurring", ExqUIWeb.RecurringLive.Index, :index, route_opts
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/exq_ui_web/router/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule ExqUIWeb.Router.Helpers do
def queue_index_path(socket, params \\ %{}), do: build(socket, "/queues", params)
def queue_show_path(socket, queue, params \\ %{}), do: build(socket, "/queues/#{queue}", params)
def retry_index_path(socket, params \\ %{}), do: build(socket, "/retries", params)
def recurring_index_path(socket, params \\ %{}), do: build(socket, "/recurring", params)

def retry_show_path(socket, score, jid, params \\ %{}),
do: build(socket, "/retries/#{score}/#{jid}", params)
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ defmodule ExqUI.MixProject do
defp deps do
[
{:exq, ">= 0.16.2"},
{:exq_scheduler, "~> 1.0", optional: true},
{:phoenix_live_view, "~> 0.16"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:jason, "~> 1.0"},
Expand Down
Loading

0 comments on commit 964cd5c

Please sign in to comment.