mission_control-jobs 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +244 -0
  4. data/Rakefile +8 -0
  5. data/app/assets/config/mission_control_jobs_manifest.js +4 -0
  6. data/app/assets/stylesheets/mission_control/jobs/application.css +16 -0
  7. data/app/assets/stylesheets/mission_control/jobs/forms.css +8 -0
  8. data/app/assets/stylesheets/mission_control/jobs/jobs.css +7 -0
  9. data/app/controllers/concerns/mission_control/jobs/adapter_features.rb +20 -0
  10. data/app/controllers/concerns/mission_control/jobs/application_scoped.rb +38 -0
  11. data/app/controllers/concerns/mission_control/jobs/failed_jobs_bulk_operations.rb +17 -0
  12. data/app/controllers/concerns/mission_control/jobs/job_filters.rb +18 -0
  13. data/app/controllers/concerns/mission_control/jobs/job_scoped.rb +16 -0
  14. data/app/controllers/concerns/mission_control/jobs/not_found_redirections.rb +25 -0
  15. data/app/controllers/concerns/mission_control/jobs/queue_scoped.rb +12 -0
  16. data/app/controllers/mission_control/jobs/application_controller.rb +11 -0
  17. data/app/controllers/mission_control/jobs/bulk_discards_controller.rb +20 -0
  18. data/app/controllers/mission_control/jobs/bulk_retries_controller.rb +10 -0
  19. data/app/controllers/mission_control/jobs/discards_controller.rb +13 -0
  20. data/app/controllers/mission_control/jobs/jobs_controller.rb +37 -0
  21. data/app/controllers/mission_control/jobs/queues/pauses_controller.rb +15 -0
  22. data/app/controllers/mission_control/jobs/queues_controller.rb +24 -0
  23. data/app/controllers/mission_control/jobs/retries_controller.rb +13 -0
  24. data/app/controllers/mission_control/jobs/workers_controller.rb +18 -0
  25. data/app/helpers/mission_control/jobs/application_helper.rb +8 -0
  26. data/app/helpers/mission_control/jobs/dates_helper.rb +19 -0
  27. data/app/helpers/mission_control/jobs/jobs_helper.rb +63 -0
  28. data/app/helpers/mission_control/jobs/navigation_helper.rb +51 -0
  29. data/app/helpers/mission_control/jobs/ui_helper.rb +23 -0
  30. data/app/javascript/mission_control/jobs/application.js +4 -0
  31. data/app/javascript/mission_control/jobs/controllers/application.js +9 -0
  32. data/app/javascript/mission_control/jobs/controllers/form_controller.js +21 -0
  33. data/app/javascript/mission_control/jobs/controllers/index.js +11 -0
  34. data/app/javascript/mission_control/jobs/helpers/debounce_helpers.js +9 -0
  35. data/app/javascript/mission_control/jobs/helpers/index.js +1 -0
  36. data/app/jobs/mission_control/jobs/application_job.rb +6 -0
  37. data/app/mailers/mission_control/jobs/application_mailer.rb +8 -0
  38. data/app/models/mission_control/jobs/application_record.rb +7 -0
  39. data/app/models/mission_control/jobs/current.rb +3 -0
  40. data/app/models/mission_control/jobs/page.rb +48 -0
  41. data/app/models/mission_control/jobs/worker.rb +17 -0
  42. data/app/views/layouts/mission_control/jobs/_application_selection.html.erb +11 -0
  43. data/app/views/layouts/mission_control/jobs/_flash.html.erb +9 -0
  44. data/app/views/layouts/mission_control/jobs/_navigation.html.erb +9 -0
  45. data/app/views/layouts/mission_control/jobs/application.html.erb +25 -0
  46. data/app/views/layouts/mission_control/jobs/application_selection/_applications.html.erb +13 -0
  47. data/app/views/layouts/mission_control/jobs/application_selection/_servers.html.erb +15 -0
  48. data/app/views/mission_control/jobs/jobs/_error_information.html.erb +19 -0
  49. data/app/views/mission_control/jobs/jobs/_filters.html.erb +35 -0
  50. data/app/views/mission_control/jobs/jobs/_general_information.html.erb +54 -0
  51. data/app/views/mission_control/jobs/jobs/_job.html.erb +13 -0
  52. data/app/views/mission_control/jobs/jobs/_jobs_page.html.erb +15 -0
  53. data/app/views/mission_control/jobs/jobs/_raw_data.html.erb +4 -0
  54. data/app/views/mission_control/jobs/jobs/_title.html.erb +13 -0
  55. data/app/views/mission_control/jobs/jobs/_toolbar.html.erb +18 -0
  56. data/app/views/mission_control/jobs/jobs/blocked/_job.html.erb +3 -0
  57. data/app/views/mission_control/jobs/jobs/failed/_actions.html.erb +5 -0
  58. data/app/views/mission_control/jobs/jobs/failed/_job.html.erb +7 -0
  59. data/app/views/mission_control/jobs/jobs/finished/_job.html.erb +2 -0
  60. data/app/views/mission_control/jobs/jobs/in_progress/_job.html.erb +9 -0
  61. data/app/views/mission_control/jobs/jobs/index.html.erb +19 -0
  62. data/app/views/mission_control/jobs/jobs/scheduled/_job.html.erb +7 -0
  63. data/app/views/mission_control/jobs/jobs/show.html.erb +6 -0
  64. data/app/views/mission_control/jobs/queues/_actions.html.erb +7 -0
  65. data/app/views/mission_control/jobs/queues/_job.html.erb +15 -0
  66. data/app/views/mission_control/jobs/queues/_queue.html.erb +16 -0
  67. data/app/views/mission_control/jobs/queues/_queue_title.html.erb +17 -0
  68. data/app/views/mission_control/jobs/queues/index.html.erb +16 -0
  69. data/app/views/mission_control/jobs/queues/show.html.erb +25 -0
  70. data/app/views/mission_control/jobs/shared/_pagination_toolbar.html.erb +5 -0
  71. data/app/views/mission_control/jobs/workers/_configuration.html.erb +6 -0
  72. data/app/views/mission_control/jobs/workers/_job.html.erb +19 -0
  73. data/app/views/mission_control/jobs/workers/_jobs.html.erb +20 -0
  74. data/app/views/mission_control/jobs/workers/_raw_data.html.erb +6 -0
  75. data/app/views/mission_control/jobs/workers/_title.html.erb +11 -0
  76. data/app/views/mission_control/jobs/workers/_worker.html.erb +21 -0
  77. data/app/views/mission_control/jobs/workers/index.html.erb +17 -0
  78. data/app/views/mission_control/jobs/workers/show.html.erb +7 -0
  79. data/config/importmap.rb +6 -0
  80. data/config/routes.rb +33 -0
  81. data/lib/active_job/errors/invalid_operation.rb +5 -0
  82. data/lib/active_job/errors/job_not_found_error.rb +14 -0
  83. data/lib/active_job/errors/query_error.rb +5 -0
  84. data/lib/active_job/executing.rb +43 -0
  85. data/lib/active_job/execution_error.rb +8 -0
  86. data/lib/active_job/failed.rb +11 -0
  87. data/lib/active_job/job_proxy.rb +26 -0
  88. data/lib/active_job/jobs_relation.rb +300 -0
  89. data/lib/active_job/querying.rb +44 -0
  90. data/lib/active_job/queue.rb +62 -0
  91. data/lib/active_job/queue_adapters/resque_ext.rb +300 -0
  92. data/lib/active_job/queue_adapters/solid_queue_ext.rb +294 -0
  93. data/lib/active_job/queues.rb +29 -0
  94. data/lib/mission_control/jobs/adapter.rb +108 -0
  95. data/lib/mission_control/jobs/application.rb +17 -0
  96. data/lib/mission_control/jobs/applications.rb +8 -0
  97. data/lib/mission_control/jobs/console/context.rb +11 -0
  98. data/lib/mission_control/jobs/console/helpers.rb +26 -0
  99. data/lib/mission_control/jobs/engine.rb +88 -0
  100. data/lib/mission_control/jobs/errors/incompatible_adapter.rb +2 -0
  101. data/lib/mission_control/jobs/errors/resource_not_found.rb +2 -0
  102. data/lib/mission_control/jobs/identified_by_name.rb +18 -0
  103. data/lib/mission_control/jobs/identified_elements.rb +23 -0
  104. data/lib/mission_control/jobs/server/serializable.rb +24 -0
  105. data/lib/mission_control/jobs/server/workers.rb +15 -0
  106. data/lib/mission_control/jobs/server.rb +26 -0
  107. data/lib/mission_control/jobs/version.rb +5 -0
  108. data/lib/mission_control/jobs.rb +19 -0
  109. data/lib/resque/thread_safe_redis.rb +34 -0
  110. data/lib/tasks/mission_control/jobs_tasks.rake +4 -0
  111. metadata +364 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 63e12a3c6650d55dd75e269ca604320316a5400c6e7b9429b16a05de167717a7
4
+ data.tar.gz: 617b2ea6236f481ed4ee6b6c22e5b72570b75d9e2bcf69e0cf60ba18eec69e06
5
+ SHA512:
6
+ metadata.gz: 91a3206ed4772d01f441d7fe6ed10b0e8dc27beb6828333b3805ad65cc314d13157c9a6e2d07f0bed46f09f911549d28250244f320a9dc637883135e5c10f627
7
+ data.tar.gz: c6f173db8d4d4be438b91ad8925d381f5eb44aabda18fcd9f4b174f91bf80922c779ca7eb71132046c952b496c379209c29c7da1aec94f667f01365a51b79c3a
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 37signals, LLC
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,244 @@
1
+ # Mission Control — Jobs
2
+
3
+ This gem provides a Rails-based frontend to Active Job adapters. It currently supports [Resque](https://github.com/resque/resque/) and [Solid Queue](https://github.com/basecamp/solid_queue). Its features depend on those offered by the adapter itself, but at a minimum, it allows you to inspect job queues, jobs currently waiting in those queues, and inspecting and retrying or discarding failed jobs.
4
+
5
+ ## Installation
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem "mission_control-jobs"
10
+ ```
11
+
12
+ And then execute:
13
+ ```bash
14
+ $ bundle install
15
+ ```
16
+
17
+ ## Basic configuration
18
+
19
+ Mount Mission Control Job's engine where you wish to have it accessible from your app, in your `routes.rb` file:
20
+
21
+ ```ruby
22
+ Rails.application.routes.draw do
23
+ # ...
24
+ mount MissionControl::Jobs::Engine, at: "/jobs"
25
+ ```
26
+
27
+ And that's it. With this alone, you should be able to access Mission Control Job's UI, where you can browse the existing queues, jobs pending in these queues, jobs in different statuses, and discard and retry failed jobs:
28
+
29
+ ![Queues tab in a simple app](docs/images/queues-simple.png)
30
+
31
+ ![Failed jobs tab in a simple app](docs/images/failed-jobs-simple.png)
32
+
33
+
34
+ ### Authentication and base controller class
35
+
36
+ By default, Mission Control's controllers will extend the host app's `ApplicationController`. If no authentication is enforced, `/jobs` will be available to everyone. You might want to implement some kind of authentication for this in your app. To make this easier, you can specify a different controller as the base class for Mission Control's controllers:
37
+
38
+ ```ruby
39
+ Rails.application.configure do
40
+ MissionControl::Jobs.base_controller_class = "AdminController"
41
+ end
42
+ ```
43
+
44
+ Or, in your environment config or `application.rb`:
45
+ ```ruby
46
+ config.mission_control.jobs.base_controller_class = "AdminController"
47
+
48
+ ```
49
+
50
+ ### Other configuration settings
51
+
52
+ Besides `base_controller_class`, you can also set the following for `MissionControl::Jobs` or `config.mission_control.jobs`:
53
+
54
+ - `logger`: the logger you want Mission Control Jobs to use. Defaults to `ActiveSupport::Logger.new(nil)` (no logging). Notice that this is different from Active Job's logger or Active Job's backend's configured logger.
55
+ - `delay_between_bulk_operation_batches`: how long to wait between batches when performing bulk operations, such as _discard all_ or _retry all_ jobs—defaults to `0`
56
+ - `adapters`: a list of adapters that you want Mission Control to use and extend. By default this will be the adapter you have set for `active_job.queue_adapter`.
57
+
58
+ This library extends Active Job with a querying interface, and the following setting:
59
+ - `config.active_job.default_page_size`: the internal batch size that Active Job will use when sending queries to the underlying adapter and the batch size for the bulk operations defined above—defaults to `1000`.
60
+
61
+ ## Advanced configuration
62
+
63
+ When we built Mission Control Jobs, we did it with the idea of managing multiple apps' job backends from a single, centralized app that we used for monitoring, alerts and other tools that related to all our apps. Some of our apps run in more than one datacenter and we run different Resque instances with different Redis configurations in each. Because of this, we added support for multiple apps and multiple adapters per app. Even when running Mission Control Job within the app it manages and a single DC, as we migrated from Resque to Solid Queue, we needed to manage both adapters from Mission Control.
64
+
65
+ Without adding any additional configuration to [the one described before](#basic-configuration), Mission Control will be configured with one single app and a single server for your configured `active_job.queue_adapter`.
66
+
67
+ If you want to support multiple adapters, you need to add them to Mission Control configuration via the `adapters` setting mentioned above. For example:
68
+
69
+ ```ruby
70
+ config.mission_control.jobs.adapters = [ :resque, :solid_queue ]
71
+ ```
72
+
73
+ Then, to configure the different apps and/or different servers, you can do so in an initializer like this (taken from our dummy app for testing purposes):
74
+
75
+ ```ruby
76
+ require "resque"
77
+ require "resque_pause_helper"
78
+
79
+ require "solid_queue"
80
+
81
+ Resque.redis = Redis::Namespace.new "#{Rails.env}", redis: Redis.new(host: "localhost", port: 6379, thread_safe: true)
82
+
83
+ SERVERS_BY_APP = {
84
+ BC4: %w[ resque_ashburn resque_chicago ],
85
+ HEY: %w[ resque solid_queue ]
86
+ }
87
+
88
+ def redis_connection_for(app, server)
89
+ redis_namespace = Redis::Namespace.new "#{app}:#{server}", redis: Resque.redis.instance_variable_get("@redis")
90
+ Resque::DataStore.new redis_namespace
91
+ end
92
+
93
+ SERVERS_BY_APP.each do |app, servers|
94
+ queue_adapters_by_name = servers.collect do |server|
95
+ queue_adapter = if server.start_with?("resque")
96
+ ActiveJob::QueueAdapters::ResqueAdapter.new(redis_connection_for(app, server))
97
+ else
98
+ ActiveJob::QueueAdapters::SolidQueueAdapter.new
99
+ end
100
+
101
+ [ server, queue_adapter ]
102
+ end.to_h
103
+
104
+ MissionControl::Jobs.applications.add(app, queue_adapters_by_name)
105
+ end
106
+ ```
107
+
108
+ This is an example for two different apps, BC4 and HEY, each one with two servers. BC4 has two Resque servers with two different configurations and HEY has one Resque server and one Solid Queue server.
109
+
110
+ Currently only one Solid Queue configuration is supported, but support for several Solid Queue backends (with different databases) [is planned](https://github.com/basecamp/mission_control-jobs/issues/35).
111
+
112
+ This is how we set Resque and Solid Queue together when we migrated from one to the other:
113
+
114
+ ```ruby
115
+ queue_adapters_by_name = {
116
+ resque: ActiveJob::QueueAdapters.lookup(:resque).new, # This will use Resque.redis as the redis client
117
+ solid_queue: ActiveJob::QueueAdapters.lookup(:solid_queue).new
118
+ }
119
+
120
+ MissionControl::Jobs.applications.add("hey", queue_adapters_by_name)
121
+ ```
122
+
123
+ When you have multiple apps and servers configured, you can choose between them with select and toggle menus:
124
+
125
+ ![Queues tab with multiple apps and servers](docs/images/queues-multiple.png)
126
+
127
+ ## Basic UI usage
128
+
129
+ As mentioned, the features available in Mission Control depend on the adapter you're using, as each adapter supports different features. Besides inspecting the queues and the jobs in them, and discarding and retrying failed jobs, you can inspect jobs in different statuses supported by each adapter, filter them by _queue name_ and _job class name_ (with the idea of [adding more filters in the future](https://github.com/basecamp/mission_control-jobs/issues/30)), pause and un-pause queues (if the adapter allows that), inspect workers, know which jobs are being run by what worker, checking a specific job or a specific worker...
130
+
131
+ ![Default queue tab](docs/images/default-queue.png)
132
+
133
+ ![In-progress jobs tab](docs/images/in-progress-jobs.png)
134
+
135
+ ![Workers tab](docs/images/workers.png)
136
+
137
+ ![Single job](docs/images/single-job.png)
138
+
139
+ ![Single worker](docs/images/single-worker.png)
140
+
141
+
142
+ ## Console helpers, scripting and dealing with big sets of jobs
143
+
144
+ Besides the UI, Mission Control provides a light console helper to switch between applications and adapters. Some potentially destructive actions aren't exposed via the UI (for example, discarding jobs that aren't failed, although this might change in the future) but you can always perform these from the console if you know very well what you're doing.
145
+
146
+ It's also possible that you need to deal with very big sets of jobs that are unmanageable via the UI, or that you wish to write a script to deal with an incident, some cleanup or some data migration. The console helpers and the querying API with which we've extended Active Job come handy here.
147
+
148
+ First, when connecting to the Rails console, you'll see this new message:
149
+
150
+ ```
151
+ bin/rails c
152
+
153
+
154
+ Type 'jobs_help' to see how to connect to the available job servers to manage jobs
155
+ ```
156
+
157
+ Typing `jobs_help`, you'll get clear instructions about how to switch between applications and adapters:
158
+
159
+ ```
160
+ >> jobs_help
161
+ You can connect to a job server with
162
+ connect_to <app_id>:<server_id>
163
+
164
+ Available job servers:
165
+ * bc4:resque_ashburn
166
+ * bc4:resque_chicago
167
+ * hey:resque
168
+ * hey:solid_queue
169
+ ```
170
+
171
+ And then:
172
+ ```
173
+ >> connect_to "hey:solid_queue"
174
+ Connected to hey:solid_queue
175
+ ```
176
+
177
+ Now you're ready to query and operate over jobs for this adapter via the API. Some examples of queries:
178
+
179
+ ```ruby
180
+
181
+ # All jobs
182
+ ActiveJob.jobs
183
+
184
+ # All failed jobs
185
+ ActiveJob.jobs.failed
186
+
187
+ # All pending jobs in some queue
188
+ ActiveJob.jobs.pending.where(queue: "some_queue")
189
+
190
+ # All failed jobs of a given class
191
+ ActiveJob.jobs.failed.where(job_class: "SomeJob")
192
+
193
+ # All pending jobs of a given class with limit and offset
194
+ ActiveJob.jobs.pending.where(job_class: "SomeJob").limit(10).offset(5)
195
+
196
+ # For adpaters that support these statuses:
197
+ # All scheduled/in-progress/finished jobs of a given class
198
+ ActiveJob.jobs.scheudled.where(job_class: "SomeJob")
199
+ ActiveJob.jobs.in_progress.where(job_class: "SomeJob")
200
+ ActiveJob.jobs.finished.where(job_class: "SomeJob")
201
+
202
+ # For adapters that support filtering by worker:
203
+ # All jobs in progress being run by a given worker
204
+ ActiveJob.jobs.in_progress.where(worker_id: 42)
205
+ ```
206
+
207
+ Some examples of bulk operations:
208
+
209
+ ```ruby
210
+ # Retry all the jobs (only possible for failed jobs)
211
+ ActiveJob.jobs.failed.retry_all
212
+
213
+ # Retry all the jobs of a given class (only possible for failed jobs)
214
+ ActiveJob.jobs.failed.where(job_class: "SomeJob").retry_all
215
+
216
+ # Discard all failed jobs
217
+ ActiveJob.jobs.failed.discard_all
218
+
219
+ # Discard all pending jobs of a given class
220
+ ActiveJob.jobs.pending.where(job_class: "SomeJob").discard_all
221
+ # Or all pending jobs in a given queue:
222
+ ActiveJob.jobs.pending.where(queue_name: "some-queue").discard_all
223
+ ```
224
+
225
+ When performing these bulk operations in the console, a delay of 2 seconds between batches processed will be introduced, set via [`delay_between_bulk_operation_batches`](#other-configuration-settings). You can modify it as
226
+ ```ruby
227
+ MissionControl::Jobs.delay_between_bulk_operation_batches = 5.seconds
228
+ ```
229
+
230
+ ## Contributing
231
+
232
+ Thanks for your interest in contributing! To get the app running locally just run:
233
+ ```
234
+ bin/setup
235
+ ```
236
+ This will load a bunch of jobs as seeds.
237
+
238
+ We have both unit and functional tests and system tests. If you want to run system tests, you'd need to install [ChromeDriver](https://chromedriver.chromium.org/). Then, you'll be able to run the tests as:
239
+ ```
240
+ bin/rails test test/system
241
+ ```
242
+
243
+ ## License
244
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
@@ -0,0 +1,4 @@
1
+ //= link_directory ../stylesheets/mission_control/jobs .css
2
+ //= link_directory ../../javascript/mission_control/jobs .js
3
+ //= link_directory ../../javascript/mission_control/jobs/controllers .js
4
+ //= link_directory ../../javascript/mission_control/jobs/helpers .js
@@ -0,0 +1,16 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
16
+
@@ -0,0 +1,8 @@
1
+ /* Hide the HTML datalist wonky arrow in Safari and Chrome, since we add it via bulma. */
2
+ [list]::-webkit-calendar-picker-indicator {
3
+ opacity: 0;
4
+ }
5
+
6
+ a[disabled] {
7
+ pointer-events: none;
8
+ }
@@ -0,0 +1,7 @@
1
+ .filter input {
2
+ width: 15rem;
3
+ }
4
+
5
+ .jobs td {
6
+ word-break: break-all;
7
+ }
@@ -0,0 +1,20 @@
1
+ module MissionControl::Jobs::AdapterFeatures
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ helper_method :supported_job_statuses, :queue_pausing_supported?, :workers_exposed?
6
+ end
7
+
8
+ private
9
+ def workers_exposed?
10
+ MissionControl::Jobs::Current.server.queue_adapter.exposes_workers?
11
+ end
12
+
13
+ def supported_job_statuses
14
+ MissionControl::Jobs::Current.server.queue_adapter.supported_statuses & ActiveJob::JobsRelation::STATUSES
15
+ end
16
+
17
+ def queue_pausing_supported?
18
+ MissionControl::Jobs::Current.server.queue_adapter.supports_queue_pausing?
19
+ end
20
+ end
@@ -0,0 +1,38 @@
1
+ module MissionControl::Jobs::ApplicationScoped
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ before_action :set_application
6
+ around_action :activating_job_server
7
+
8
+ delegate :applications, to: MissionControl::Jobs
9
+ end
10
+
11
+ private
12
+ def set_application
13
+ @application = find_application or raise MissionControl::Jobs::Errors::ResourceNotFound, "Application not found"
14
+ MissionControl::Jobs::Current.application = @application
15
+ end
16
+
17
+ def find_application
18
+ if params[:application_id]
19
+ applications[params[:application_id]]
20
+ else
21
+ applications.first
22
+ end
23
+ end
24
+
25
+ def activating_job_server(&block)
26
+ @server = find_server or raise MissionControl::Jobs::Errors::ResourceNotFound, "Server not found"
27
+ MissionControl::Jobs::Current.server = @server
28
+ @server.activating(&block)
29
+ end
30
+
31
+ def find_server
32
+ if params[:server_id]
33
+ MissionControl::Jobs::Current.application.servers[params[:server_id]]
34
+ else
35
+ @application.servers.first
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,17 @@
1
+ module MissionControl::Jobs::FailedJobsBulkOperations
2
+ extend ActiveSupport::Concern
3
+
4
+ MAX_NUMBER_OF_JOBS_FOR_BULK_OPERATIONS = 3000
5
+
6
+ included do
7
+ include MissionControl::Jobs::JobFilters
8
+ end
9
+
10
+ private
11
+ # We set a hard limit to prevent problems with the data store (for example, overloading Redis
12
+ # or causing replication lag in MySQL). This should be enough for most scenarios. For
13
+ # cases where we need to retry a huge sets of jobs, we offer a runbook that uses the API.
14
+ def bulk_limited_filtered_failed_jobs
15
+ ApplicationJob.jobs.failed.where(**@job_filters).limit(MAX_NUMBER_OF_JOBS_FOR_BULK_OPERATIONS)
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ module MissionControl::Jobs::JobFilters
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ before_action :set_filters
6
+
7
+ helper_method :active_filters?
8
+ end
9
+
10
+ private
11
+ def set_filters
12
+ @job_filters = { job_class_name: params.dig(:filter, :job_class_name).presence, queue_name: params.dig(:filter, :queue_name).presence }.compact
13
+ end
14
+
15
+ def active_filters?
16
+ @job_filters.any?
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ module MissionControl::Jobs::JobScoped
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ before_action :set_job, except: :index
6
+ end
7
+
8
+ private
9
+ def set_job
10
+ @job = jobs_relation.find_by_id!(params[:job_id] || params[:id])
11
+ end
12
+
13
+ def jobs_relation
14
+ raise NotImplementedError
15
+ end
16
+ end
@@ -0,0 +1,25 @@
1
+ module MissionControl::Jobs::NotFoundRedirections
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ rescue_from(ActiveJob::Errors::JobNotFoundError) do |error|
6
+ redirect_to best_location_for_job_relation(error.job_relation), alert: error.message
7
+ end
8
+
9
+ rescue_from(MissionControl::Jobs::Errors::ResourceNotFound) do |error|
10
+ redirect_to root_url, alert: error.message
11
+ end
12
+ end
13
+
14
+ private
15
+ def best_location_for_job_relation(job_relation)
16
+ case
17
+ when job_relation.failed?
18
+ application_jobs_path(@application, :failed)
19
+ when job_relation.queue_name.present?
20
+ application_queue_path(@application, job_relation.queue_name)
21
+ else
22
+ root_path
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,12 @@
1
+ module MissionControl::Jobs::QueueScoped
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ before_action :set_queue
6
+ end
7
+
8
+ private
9
+ def set_queue
10
+ @queue = ActiveJob::Base.queues[params[:queue_id]] or raise MissionControl::Jobs::Errors::ResourceNotFound, "Queue '#{params[:queue_id]}' not found"
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ class MissionControl::Jobs::ApplicationController < MissionControl::Jobs.base_controller_class.constantize
2
+ layout "mission_control/jobs/application"
3
+
4
+ include MissionControl::Jobs::ApplicationScoped, MissionControl::Jobs::NotFoundRedirections
5
+ include MissionControl::Jobs::AdapterFeatures
6
+
7
+ private
8
+ def default_url_options
9
+ { server_id: MissionControl::Jobs::Current.server }
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ class MissionControl::Jobs::BulkDiscardsController < MissionControl::Jobs::ApplicationController
2
+ include MissionControl::Jobs::FailedJobsBulkOperations
3
+
4
+ def create
5
+ jobs_to_discard_count = jobs_to_discard.count
6
+ jobs_to_discard.discard_all
7
+
8
+ redirect_to application_jobs_url(@application, :failed), notice: "Discarded #{jobs_to_discard_count} jobs"
9
+ end
10
+
11
+ private
12
+ def jobs_to_discard
13
+ if active_filters?
14
+ bulk_limited_filtered_failed_jobs
15
+ else
16
+ # we don't want to apply any limit since "discarding all" without parameters can be optimized in the adapter as a much faster operation
17
+ ApplicationJob.jobs.failed
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ class MissionControl::Jobs::BulkRetriesController < MissionControl::Jobs::ApplicationController
2
+ include MissionControl::Jobs::FailedJobsBulkOperations
3
+
4
+ def create
5
+ jobs_to_retry_count = bulk_limited_filtered_failed_jobs.count
6
+ bulk_limited_filtered_failed_jobs.retry_all
7
+
8
+ redirect_to application_jobs_url(@application, :failed), notice: "Retried #{jobs_to_retry_count} jobs"
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ class MissionControl::Jobs::DiscardsController < MissionControl::Jobs::ApplicationController
2
+ include MissionControl::Jobs::JobScoped
3
+
4
+ def create
5
+ @job.discard
6
+ redirect_to application_jobs_url(@application, :failed), notice: "Discarded job with id #{@job.job_id}"
7
+ end
8
+
9
+ private
10
+ def jobs_relation
11
+ ApplicationJob.jobs.failed
12
+ end
13
+ end
@@ -0,0 +1,37 @@
1
+ class MissionControl::Jobs::JobsController < MissionControl::Jobs::ApplicationController
2
+ include MissionControl::Jobs::JobScoped, MissionControl::Jobs::JobFilters
3
+
4
+ def index
5
+ @job_class_names = jobs_with_status.job_class_names
6
+ @queue_names = ApplicationJob.queues.map(&:name)
7
+
8
+ @jobs_page = MissionControl::Jobs::Page.new(filtered_jobs_with_status, page: params[:page].to_i)
9
+ @jobs_count = @jobs_page.total_count
10
+ end
11
+
12
+ def show
13
+ end
14
+
15
+ private
16
+ def jobs_relation
17
+ filtered_jobs
18
+ end
19
+
20
+ def filtered_jobs_with_status
21
+ filtered_jobs.with_status(jobs_status)
22
+ end
23
+
24
+ def jobs_with_status
25
+ ApplicationJob.jobs.with_status(jobs_status)
26
+ end
27
+
28
+ def filtered_jobs
29
+ ApplicationJob.jobs.where(**@job_filters)
30
+ end
31
+
32
+ helper_method :jobs_status
33
+
34
+ def jobs_status
35
+ params[:status].presence&.inquiry
36
+ end
37
+ end
@@ -0,0 +1,15 @@
1
+ class MissionControl::Jobs::Queues::PausesController < MissionControl::Jobs::ApplicationController
2
+ include MissionControl::Jobs::QueueScoped
3
+
4
+ def create
5
+ @queue.pause
6
+
7
+ redirect_back fallback_location: application_queues_url(@application)
8
+ end
9
+
10
+ def destroy
11
+ @queue.resume
12
+
13
+ redirect_back fallback_location: application_queues_url(@application)
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ class MissionControl::Jobs::QueuesController < MissionControl::Jobs::ApplicationController
2
+ before_action :set_queue, only: :show
3
+
4
+ def index
5
+ @queues = filtered_queues.sort_by(&:name)
6
+ end
7
+
8
+ def show
9
+ @jobs_page = MissionControl::Jobs::Page.new(@queue.jobs, page: params[:page].to_i)
10
+ end
11
+
12
+ private
13
+ def set_queue
14
+ @queue = ApplicationJob.queues[params[:id]]
15
+ end
16
+
17
+ def filtered_queues
18
+ if prefix = ApplicationJob.queue_name_prefix
19
+ ApplicationJob.queues.select { |queue| queue.name.start_with?(prefix) }
20
+ else
21
+ ApplicationJob.queues
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ class MissionControl::Jobs::RetriesController < MissionControl::Jobs::ApplicationController
2
+ include MissionControl::Jobs::JobScoped
3
+
4
+ def create
5
+ @job.retry
6
+ redirect_to application_jobs_url(@application, :failed), notice: "Retried job with id #{@job.job_id}"
7
+ end
8
+
9
+ private
10
+ def jobs_relation
11
+ ApplicationJob.jobs.failed
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ class MissionControl::Jobs::WorkersController < MissionControl::Jobs::ApplicationController
2
+ before_action :ensure_exposed_workers
3
+
4
+ def index
5
+ @workers = MissionControl::Jobs::Current.server.workers.sort_by { |worker| -worker.jobs.count }
6
+ end
7
+
8
+ def show
9
+ @worker = MissionControl::Jobs::Current.server.find_worker(params[:id])
10
+ end
11
+
12
+ private
13
+ def ensure_exposed_workers
14
+ unless workers_exposed?
15
+ redirect_to root_url, alert: "This server doesn't expose workers"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ module MissionControl::Jobs
2
+ module ApplicationHelper
3
+ # Explicit helper inclusion because ApplicationController inherits from the host app.
4
+ #
5
+ # We can't rely on +config.action_controller.include_all_helpers = true+ in the host app.
6
+ include DatesHelper, JobsHelper, NavigationHelper, UiHelper
7
+ end
8
+ end