Skip to content

Commit a2df402

Browse files
Merge pull request #1128 from Shopify/api-only
Better support API-only Rails applications
2 parents cd58765 + 49fa2b7 commit a2df402

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ take a look at the [Active Job documentation][active-job-docs].
9090
[async-adapter]: https://api.rubyonrails.org/classes/ActiveJob/QueueAdapters/AsyncAdapter.html
9191
[active-job-docs]: https://guides.rubyonrails.org/active_job_basics.html#setting-the-backend
9292

93+
### Action Controller & Action View Dependency
94+
95+
The Maintenance Tasks framework relies on Action Controller and Action View to
96+
render the UI. If you're using Rails in API-only mode, see [Using Maintenance
97+
Tasks in API-only
98+
applications](#using-maintenance-tasks-in-api-only-applications).
99+
93100
### Autoloading
94101

95102
The Maintenance Tasks framework does not support autoloading in `:classic` mode.
@@ -888,6 +895,42 @@ a Task can be in:
888895
* **succeeded**: A Task that finished successfully.
889896
* **errored**: A Task that encountered an unhandled exception while performing.
890897

898+
### Using Maintenance Tasks in API-only applications
899+
900+
The Maintenance Tasks engine uses Rails sessions for flash messages and storing
901+
the CSRF token. For the engine to work in an API-only Rails application, you
902+
need to add a [session middleware][] and the `ActionDispatch::Flash`
903+
middleware. The engine also defines a strict [Content Security Policy][], make
904+
sure to include `ActionDispatch::ContentSecurityPolicy::Middleware` in your
905+
app's middleware stack to ensure the CSP is delivered to the user's browser.
906+
907+
[session middleware]: https://guides.rubyonrails.org/api_app.html#using-session-middlewares
908+
[Content Security Policy]: https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
909+
910+
Configuring Rails applications is beyond the scope of this documentation, but
911+
one way to do this is to add these lines to your application configuration:
912+
913+
```ruby
914+
# config/application.rb
915+
module YourApplication
916+
class Application < Rails::Application
917+
# ...
918+
config.api_only = true
919+
920+
config.middleware.insert_before ::Rack::Head, ::ActionDispatch::Flash
921+
config.middleware.insert_before ::Rack::Head, ::ActionDispatch::ContentSecurityPolicy::Middleware
922+
config.session_store :cookie_store, key: "_#{railtie_name.chomp("_application")}_session", secure: true
923+
config.middleware.insert_before ::ActionDispatch::Flash, config.session_store, config.session_options
924+
config.middleware.insert_before config.session_store, ActionDispatch::Cookies
925+
end
926+
end
927+
```
928+
929+
You can read more in the [Using Rails for API-only Applications][rails api] Rails
930+
guide.
931+
932+
[rails api]: https://guides.rubyonrails.org/api_app.html
933+
891934
### How Maintenance Tasks runs a Task
892935

893936
Maintenance tasks can be running for a long time, and the purpose of the gem is
@@ -1145,7 +1188,7 @@ The value for `MaintenanceTasks.stuck_task_duration` must be an
11451188
`ActiveSupport::Duration`. If no value is specified, it will default to 5
11461189
minutes.
11471190

1148-
### Metadata
1191+
#### Metadata
11491192

11501193
`MaintenanceTasks.metadata` can be configured to specify a proc from which to
11511194
get extra information about the run. Since this proc will be ran in the context

app/views/maintenance_tasks/runs/_run.html.erb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@
2323

2424
<div class="buttons">
2525
<% if run.paused? %>
26-
<%= button_to 'Resume', resume_task_run_path(@task, run), method: :put, class: 'button is-primary', disabled: @task.deleted? %>
27-
<%= button_to 'Cancel', cancel_task_run_path(@task, run), method: :put, class: 'button is-danger' %>
26+
<%= button_to 'Resume', resume_task_run_path(@task, run), class: 'button is-primary', disabled: @task.deleted? %>
27+
<%= button_to 'Cancel', cancel_task_run_path(@task, run), class: 'button is-danger' %>
2828
<% elsif run.errored? %>
29-
<%= button_to 'Resume', resume_task_run_path(@task, run), method: :put, class: 'button is-primary', disabled: @task.deleted? %>
29+
<%= button_to 'Resume', resume_task_run_path(@task, run), class: 'button is-primary', disabled: @task.deleted? %>
3030
<% elsif run.cancelling? %>
3131
<% if run.stuck? %>
32-
<%= button_to 'Cancel', cancel_task_run_path(@task, run), method: :put, class: 'button is-danger', disabled: @task.deleted? %>
32+
<%= button_to 'Cancel', cancel_task_run_path(@task, run), class: 'button is-danger', disabled: @task.deleted? %>
3333
<% end %>
3434
<% elsif run.pausing? %>
35-
<%= button_to 'Pausing', pause_task_run_path(@task, run), method: :put, class: 'button is-warning', disabled: true %>
36-
<%= button_to 'Cancel', cancel_task_run_path(@task, run), method: :put, class: 'button is-danger' %>
35+
<%= button_to 'Pausing', pause_task_run_path(@task, run), class: 'button is-warning', disabled: true %>
36+
<%= button_to 'Cancel', cancel_task_run_path(@task, run), class: 'button is-danger' %>
3737
<% if run.stuck? %>
38-
<%= button_to 'Force pause', pause_task_run_path(@task, run), method: :put, class: 'button is-danger', disabled: @task.deleted? %>
38+
<%= button_to 'Force pause', pause_task_run_path(@task, run), class: 'button is-danger', disabled: @task.deleted? %>
3939
<% end %>
4040
<% elsif run.active? %>
41-
<%= button_to 'Pause', pause_task_run_path(@task, run), method: :put, class: 'button is-warning', disabled: @task.deleted? %>
42-
<%= button_to 'Cancel', cancel_task_run_path(@task, run), method: :put, class: 'button is-danger' %>
41+
<%= button_to 'Pause', pause_task_run_path(@task, run), class: 'button is-warning', disabled: @task.deleted? %>
42+
<%= button_to 'Cancel', cancel_task_run_path(@task, run), class: 'button is-danger' %>
4343
<% end%>
4444
</div>
4545
</div>

config/routes.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
resources :tasks, only: [:index, :show], format: false do
55
resources :runs, only: [:create], format: false do
66
member do
7-
put "pause"
8-
put "cancel"
9-
put "resume"
7+
post "pause"
8+
post "cancel"
9+
post "resume"
1010
end
1111
end
1212
get :runs, to: redirect("tasks/%{task_id}")

0 commit comments

Comments
 (0)