@@ -8,6 +8,7 @@ defmodule PhoenixAppWeb.TestWorkerLive do
88 socket =
99 assign ( socket ,
1010 form: to_form ( % { "sleep_time" => 1000 , "should_fail" => false , "queue" => "default" } ) ,
11+ auto_form: to_form ( % { "job_count" => 5 } ) ,
1112 jobs: list_jobs ( )
1213 )
1314
@@ -25,11 +26,7 @@ defmodule PhoenixAppWeb.TestWorkerLive do
2526 should_fail = params [ "should_fail" ] == "true"
2627 queue = params [ "queue" ]
2728
28- case TestWorker . new (
29- % { "sleep_time" => sleep_time , "should_fail" => should_fail } ,
30- queue: queue
31- )
32- |> Oban . insert ( ) do
29+ case schedule_job ( sleep_time , should_fail , queue ) do
3330 { :ok , _job } ->
3431 { :noreply ,
3532 socket
@@ -43,11 +40,48 @@ defmodule PhoenixAppWeb.TestWorkerLive do
4340 end
4441 end
4542
43+ @ impl true
44+ def handle_event ( "auto_schedule" , % { "auto" => % { "job_count" => count } } , socket ) do
45+ job_count = String . to_integer ( count )
46+
47+ results =
48+ Enum . map ( 1 .. job_count , fn _ ->
49+ sleep_time = Enum . random ( 500 .. 5000 )
50+ should_fail = Enum . random ( [ true , false ] )
51+ queue = Enum . random ( [ "default" , "background" ] )
52+
53+ schedule_job ( sleep_time , should_fail , queue )
54+ end )
55+
56+ failed_count = Enum . count ( results , & match? ( { :error , _ } , & 1 ) )
57+ success_count = job_count - failed_count
58+
59+ socket =
60+ socket
61+ |> put_flash ( :info , "Scheduled #{ success_count } jobs successfully!" )
62+ |> assign ( jobs: list_jobs ( ) )
63+
64+ if failed_count > 0 do
65+ socket = put_flash ( socket , :error , "Failed to schedule #{ failed_count } jobs" )
66+ { :noreply , socket }
67+ else
68+ { :noreply , socket }
69+ end
70+ end
71+
4672 @ impl true
4773 def handle_info ( :update_jobs , socket ) do
4874 { :noreply , assign ( socket , jobs: list_jobs ( ) ) }
4975 end
5076
77+ defp schedule_job ( sleep_time , should_fail , queue ) do
78+ TestWorker . new (
79+ % { "sleep_time" => sleep_time , "should_fail" => should_fail } ,
80+ queue: queue
81+ )
82+ |> Oban . insert ( )
83+ end
84+
5185 defp list_jobs do
5286 import Ecto.Query
5387
0 commit comments