Skip to content

Commit 96d50e6

Browse files
committed
wip - add a UI for testing Oban workers
1 parent ef1b77c commit 96d50e6

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
defmodule PhoenixApp.Workers.TestWorker do
2+
use Oban.Worker
3+
4+
@impl Oban.Worker
5+
def perform(%Oban.Job{args: %{"sleep_time" => sleep_time, "should_fail" => should_fail}}) do
6+
# Simulate some work
7+
Process.sleep(sleep_time)
8+
9+
if should_fail do
10+
raise "Simulated failure in test worker"
11+
else
12+
:ok
13+
end
14+
end
15+
16+
def perform(%Oban.Job{args: %{"sleep_time" => sleep_time}}) do
17+
# Simulate some work
18+
Process.sleep(sleep_time)
19+
:ok
20+
end
21+
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
defmodule PhoenixAppWeb.TestWorkerLive do
2+
use PhoenixAppWeb, :live_view
3+
4+
alias PhoenixApp.Workers.TestWorker
5+
6+
@impl true
7+
def mount(_params, _session, socket) do
8+
socket =
9+
assign(socket,
10+
form: to_form(%{"sleep_time" => 1000, "should_fail" => false, "queue" => "default"}),
11+
jobs: list_jobs()
12+
)
13+
14+
if connected?(socket) do
15+
# Poll for job updates every second
16+
:timer.send_interval(1000, self(), :update_jobs)
17+
end
18+
19+
{:ok, socket}
20+
end
21+
22+
@impl true
23+
def handle_event("schedule", %{"test_job" => params}, socket) do
24+
sleep_time = String.to_integer(params["sleep_time"])
25+
should_fail = params["should_fail"] == "true"
26+
queue = params["queue"]
27+
28+
case TestWorker.new(
29+
%{"sleep_time" => sleep_time, "should_fail" => should_fail},
30+
queue: queue
31+
)
32+
|> Oban.insert() do
33+
{:ok, _job} ->
34+
{:noreply,
35+
socket
36+
|> put_flash(:info, "Job scheduled successfully!")
37+
|> assign(jobs: list_jobs())}
38+
39+
{:error, changeset} ->
40+
{:noreply,
41+
socket
42+
|> put_flash(:error, "Error scheduling job: #{inspect(changeset.errors)}")}
43+
end
44+
end
45+
46+
@impl true
47+
def handle_info(:update_jobs, socket) do
48+
{:noreply, assign(socket, jobs: list_jobs())}
49+
end
50+
51+
defp list_jobs do
52+
import Ecto.Query
53+
54+
Oban.Job
55+
|> where([j], j.worker == "PhoenixApp.Workers.TestWorker")
56+
|> order_by([j], desc: j.inserted_at)
57+
|> limit(10)
58+
|> PhoenixApp.Repo.all()
59+
end
60+
end
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<div class="mx-auto max-w-2xl">
2+
<div class="bg-white shadow sm:rounded-lg">
3+
<div class="px-4 py-5 sm:p-6">
4+
<h3 class="text-base font-semibold leading-6 text-gray-900">Schedule Test Worker</h3>
5+
6+
<div class="mt-5">
7+
<.form for={@form} phx-submit="schedule" class="space-y-6">
8+
<div>
9+
<label class="block text-sm font-medium text-gray-700">Sleep Time (ms)</label>
10+
<div class="mt-1">
11+
<input type="number" name="test_job[sleep_time]" value="1000" min="0"
12+
class="block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm" />
13+
</div>
14+
</div>
15+
16+
<div>
17+
<label class="block text-sm font-medium text-gray-700">Queue</label>
18+
<select name="test_job[queue]" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm">
19+
<option value="default">default</option>
20+
<option value="background">background</option>
21+
</select>
22+
</div>
23+
24+
<div class="relative flex items-start">
25+
<div class="flex h-6 items-center">
26+
<input type="checkbox" name="test_job[should_fail]" value="true"
27+
class="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-600" />
28+
</div>
29+
<div class="ml-3 text-sm leading-6">
30+
<label class="font-medium text-gray-900">Should Fail</label>
31+
</div>
32+
</div>
33+
34+
<div>
35+
<button type="submit" class="inline-flex justify-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">
36+
Schedule Job
37+
</button>
38+
</div>
39+
</.form>
40+
</div>
41+
</div>
42+
</div>
43+
44+
<div class="mt-8">
45+
<h3 class="text-base font-semibold leading-6 text-gray-900 mb-4">Recent Jobs</h3>
46+
47+
<div class="overflow-hidden shadow ring-1 ring-black ring-opacity-5 sm:rounded-lg">
48+
<table class="min-w-full divide-y divide-gray-300">
49+
<thead class="bg-gray-50">
50+
<tr>
51+
<th class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">ID</th>
52+
<th class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Queue</th>
53+
<th class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">State</th>
54+
<th class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Attempt</th>
55+
<th class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Args</th>
56+
</tr>
57+
</thead>
58+
<tbody class="divide-y divide-gray-200 bg-white">
59+
<%= for job <- @jobs do %>
60+
<tr>
61+
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"><%= job.id %></td>
62+
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"><%= job.queue %></td>
63+
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"><%= job.state %></td>
64+
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"><%= job.attempt %></td>
65+
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"><%= inspect(job.args) %></td>
66+
</tr>
67+
<% end %>
68+
</tbody>
69+
</table>
70+
</div>
71+
</div>
72+
</div>

test_integrations/phoenix_app/lib/phoenix_app_web/router.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ defmodule PhoenixAppWeb.Router do
2121
get "/exception", PageController, :exception
2222
get "/transaction", PageController, :transaction
2323

24+
live "/test-worker", TestWorkerLive
25+
2426
live "/users", UserLive.Index, :index
2527
live "/users/new", UserLive.Index, :new
2628
live "/users/:id/edit", UserLive.Index, :edit
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defmodule PhoenixApp.Repo.Migrations.AddOban do
2+
use Ecto.Migration
3+
4+
def up do
5+
Oban.Migration.up()
6+
end
7+
8+
def down do
9+
Oban.Migration.down()
10+
end
11+
end

0 commit comments

Comments
 (0)