Skip to content

Commit caf4b06

Browse files
authored
Merge pull request #192 from timujinne/dev
Fix Oban configuration and status checks
2 parents 54fc9be + ad87f7a commit caf4b06

4 files changed

Lines changed: 106 additions & 80 deletions

File tree

lib/mix/tasks/phoenix_kit.install.ex

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,40 @@ if Code.ensure_loaded?(Igniter.Mix.Task) do
135135
# CRITICAL: Check if required configuration exists BEFORE starting app
136136
# This prevents configuration timing issues where config is added via Igniter
137137
# but the app has already started with cached (missing) configuration
138+
139+
# Check if this is a retry pass (automatic restart after adding config)
140+
is_retry = Process.get(:phoenix_kit_retry_pass, false)
138141
config_status = check_required_configuration()
139142

140-
case config_status do
141-
:missing ->
143+
case {config_status, is_retry} do
144+
{:missing, false} ->
142145
# First pass: Add configuration via Igniter without starting app
146+
# Store status in Process dictionary for tracking
147+
Process.put(:phoenix_kit_config_status, :missing)
148+
143149
show_missing_config_message(argv)
144-
result = super(argv)
145-
show_config_added_message(argv)
146-
result
150+
super(argv)
151+
152+
# AUTOMATIC RESTART instead of asking user to run again manually
153+
Mix.shell().info("""
154+
155+
✅ Configuration added successfully!
156+
🔄 Automatically restarting to complete the installation...
157+
""")
147158

148-
:ok ->
159+
# Clean Process dictionary to ensure fresh state for retry
160+
Process.delete(:phoenix_kit_config_status)
161+
162+
# Mark this as a retry pass to prevent infinite loops
163+
Process.put(:phoenix_kit_retry_pass, true)
164+
165+
# Recursive call with same arguments - automatic restart
166+
run(argv)
167+
168+
{:ok, _} ->
149169
# Second pass: Configuration exists, safe to start app and complete installation
170+
Process.put(:phoenix_kit_config_status, :ok)
171+
150172
# Run standard igniter process
151173
result = super(argv)
152174

@@ -158,7 +180,27 @@ if Code.ensure_loaded?(Igniter.Mix.Task) do
158180
AssetRebuild.check_and_rebuild(verbose: true)
159181
end
160182

183+
# Clean up retry flag on successful completion
184+
Process.delete(:phoenix_kit_retry_pass)
161185
result
186+
187+
{:missing, true} ->
188+
# Safety check: Configuration still missing after automatic retry
189+
# This prevents infinite loops if configuration addition fails
190+
Mix.shell().error("""
191+
192+
❌ Configuration was not added successfully after automatic retry.
193+
194+
Please check config/config.exs manually and ensure it contains:
195+
- config :ueberauth, Ueberauth (with providers: %{})
196+
- config :hammer (with backend and expiry_ms)
197+
- config :phoenix_kit, Oban (with queues configuration)
198+
199+
Then run: mix phoenix_kit.install #{Enum.join(argv, " ")}
200+
""")
201+
202+
Process.delete(:phoenix_kit_retry_pass)
203+
:error
162204
end
163205
end
164206
end
@@ -286,17 +328,6 @@ if Code.ensure_loaded?(Igniter.Mix.Task) do
286328
""")
287329
end
288330

289-
# Display message after configuration is added
290-
defp show_config_added_message(argv) do
291-
Mix.shell().info("""
292-
293-
✅ Configuration added successfully!
294-
295-
Next step: Run the install command again to complete the installation:
296-
mix phoenix_kit.install #{Enum.join(argv, " ")}
297-
""")
298-
end
299-
300331
# Check if all required configuration exists
301332
# Returns :ok if all config present, :missing if any config is missing
302333
defp check_required_configuration do
@@ -361,9 +392,9 @@ if Code.ensure_loaded?(Igniter.Mix.Task) do
361392
has_oban_config =
362393
Enum.any?(lines, fn line ->
363394
trimmed = String.trim(line)
364-
# Not a comment and contains config :phoenix_kit, Oban
395+
# Not a comment and contains config :any_app, Oban
365396
!String.starts_with?(trimmed, "#") and
366-
String.contains?(line, "config :phoenix_kit, Oban")
397+
String.contains?(line, ", Oban")
367398
end)
368399

369400
has_queues =

lib/mix/tasks/phoenix_kit.status.ex

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,15 @@ defmodule Mix.Tasks.PhoenixKit.Status do
6060
]
6161

6262
def run(argv) do
63-
# Conditionally start the application if repositories aren't available
64-
app_started_by_us = ensure_app_started()
65-
66-
try do
67-
{opts, _argv, _errors} = OptionParser.parse(argv, switches: @switches, aliases: @aliases)
68-
69-
prefix = opts[:prefix] || "public"
70-
verbose = opts[:verbose] || false
71-
72-
show_comprehensive_status(prefix, verbose)
73-
after
74-
# Stop the application if we started it
75-
if app_started_by_us do
76-
# Note: Mix doesn't have app.stop task, and typically apps stay running
77-
# This is normal behavior for Mix tasks
78-
:ok
79-
end
80-
end
63+
# Start the application to ensure repo is available
64+
Mix.Task.run("app.start")
65+
66+
{opts, _argv, _errors} = OptionParser.parse(argv, switches: @switches, aliases: @aliases)
67+
68+
prefix = opts[:prefix] || "public"
69+
verbose = opts[:verbose] || false
70+
71+
show_comprehensive_status(prefix, verbose)
8172
end
8273

8374
# Main status display function
@@ -526,29 +517,6 @@ defmodule Mix.Tasks.PhoenixKit.Status do
526517
_ -> false
527518
end
528519

529-
# Ensure application is started only if needed
530-
# Returns true if we started the app, false if it was already running
531-
defp ensure_app_started do
532-
# Check if we can get repo configuration
533-
case get_repo_with_fallback() do
534-
nil ->
535-
# No repo found, start app and try again
536-
Mix.Task.run("app.start")
537-
true
538-
539-
repo ->
540-
# Repo found, check if it's actually available
541-
if repo_available?(repo) do
542-
# Repo is available, no need to start app
543-
false
544-
else
545-
# Repo not available, start app
546-
Mix.Task.run("app.start")
547-
true
548-
end
549-
end
550-
end
551-
552520
# Pad version number for consistent display
553521
defp pad_version(version) when version < 10, do: "0#{version}"
554522
defp pad_version(version), do: to_string(version)

lib/mix/tasks/phoenix_kit.update.ex

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -167,26 +167,60 @@ if Code.ensure_loaded?(Igniter.Mix.Task) do
167167
# CRITICAL: Check if required configuration exists BEFORE starting app
168168
# This prevents configuration timing issues where config is added via Igniter
169169
# but the app has already started with cached (missing) configuration
170+
171+
# Check if this is a retry pass (automatic restart after adding config)
172+
is_retry = Process.get(:phoenix_kit_retry_pass, false)
170173
config_status = check_required_configuration()
171174

172-
case config_status do
173-
:missing ->
175+
case {config_status, is_retry} do
176+
{:missing, false} ->
174177
# First pass: Add configuration via Igniter without starting app
175178
# Store config status in Process dictionary for igniter/1 to read
176179
Process.put(:phoenix_kit_config_status, :missing)
177180
show_missing_config_message(argv)
178-
result = super(argv)
179-
show_config_added_message(argv)
180-
result
181+
super(argv)
182+
183+
# Automatic restart instead of manual prompt
184+
Mix.shell().info("""
185+
186+
✅ Configuration added successfully!
187+
🔄 Automatically restarting to complete the update...
188+
""")
181189

182-
:ok ->
183-
# Second pass: Configuration exists, safe to start app and update
190+
# Clean Process dictionary for fresh state
191+
Process.delete(:phoenix_kit_config_status)
192+
Process.put(:phoenix_kit_retry_pass, true)
193+
194+
# Recursive call with same arguments
195+
run(argv)
196+
197+
{:ok, _} ->
198+
# Second pass (automatic or manual): Configuration exists, safe to start app
184199
# Store config status in Process dictionary for igniter/1 to read
185200
Process.put(:phoenix_kit_config_status, :ok)
186201
Mix.Task.run("app.start")
187202
result = super(argv)
188203
post_igniter_tasks(elem(opts, 0))
204+
205+
# Clean retry flag
206+
Process.delete(:phoenix_kit_retry_pass)
189207
result
208+
209+
{:missing, true} ->
210+
# Safety: Configuration still missing after retry
211+
Mix.shell().error("""
212+
213+
❌ Configuration was not added successfully after automatic retry.
214+
215+
This may indicate a problem with your config/config.exs file.
216+
Please check the file manually and ensure it's writable.
217+
218+
Then run manually:
219+
mix phoenix_kit.update #{Enum.join(argv, " ")}
220+
""")
221+
222+
Process.delete(:phoenix_kit_retry_pass)
223+
:error
190224
end
191225
end
192226
end
@@ -201,6 +235,7 @@ if Code.ensure_loaded?(Igniter.Mix.Task) do
201235
PhoenixKit requires configuration for:
202236
- Ueberauth (OAuth authentication)
203237
- Hammer (rate limiting)
238+
- Oban (background jobs for file processing)
204239
205240
This configuration will be added now.
206241
@@ -209,17 +244,6 @@ if Code.ensure_loaded?(Igniter.Mix.Task) do
209244
""")
210245
end
211246

212-
# Display message after configuration is added
213-
defp show_config_added_message(argv) do
214-
Mix.shell().info("""
215-
216-
✅ Configuration added successfully!
217-
218-
Next step: Run the update command again to complete the upgrade:
219-
mix phoenix_kit.update #{Enum.join(argv, " ")}
220-
""")
221-
end
222-
223247
# Check if all required configuration exists
224248
# Returns :ok if all config present, :missing if any config is missing
225249
defp check_required_configuration do
@@ -284,9 +308,10 @@ if Code.ensure_loaded?(Igniter.Mix.Task) do
284308
has_oban_config =
285309
Enum.any?(lines, fn line ->
286310
trimmed = String.trim(line)
287-
# Not a comment and contains config :phoenix_kit, Oban
311+
# Not a comment and contains config for any app with Oban
312+
# Matches: "config :any_app, Oban" or "config :any_app, Oban,"
288313
!String.starts_with?(trimmed, "#") and
289-
String.contains?(line, "config :phoenix_kit, Oban")
314+
String.contains?(line, ", Oban")
290315
end)
291316

292317
has_queues =

mix.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ defmodule PhoenixKit.MixProject do
128128
{:finch, "~> 0.18"},
129129

130130
# Code generation and project patching
131+
# Note: Available in all environments for library code, but typically
132+
# only needed in :dev when used as a dependency in parent projects
131133
{:igniter, "~> 0.7"}
132134
]
133135
end

0 commit comments

Comments
 (0)