que 0.11.3 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/tests.yml +51 -0
  3. data/.gitignore +2 -0
  4. data/.ruby-version +1 -0
  5. data/CHANGELOG.md +502 -97
  6. data/Dockerfile +20 -0
  7. data/LICENSE.txt +1 -1
  8. data/README.md +205 -59
  9. data/auto/dev +21 -0
  10. data/auto/pre-push-hook +30 -0
  11. data/auto/psql +9 -0
  12. data/auto/test +5 -0
  13. data/auto/test-postgres-14 +17 -0
  14. data/bin/que +8 -81
  15. data/docker-compose.yml +47 -0
  16. data/docs/README.md +881 -0
  17. data/lib/que/active_job/extensions.rb +114 -0
  18. data/lib/que/active_record/connection.rb +51 -0
  19. data/lib/que/active_record/model.rb +48 -0
  20. data/lib/que/command_line_interface.rb +259 -0
  21. data/lib/que/connection.rb +198 -0
  22. data/lib/que/connection_pool.rb +78 -0
  23. data/lib/que/job.rb +210 -103
  24. data/lib/que/job_buffer.rb +255 -0
  25. data/lib/que/job_methods.rb +176 -0
  26. data/lib/que/listener.rb +176 -0
  27. data/lib/que/locker.rb +507 -0
  28. data/lib/que/metajob.rb +47 -0
  29. data/lib/que/migrations/4/down.sql +48 -0
  30. data/lib/que/migrations/4/up.sql +267 -0
  31. data/lib/que/migrations/5/down.sql +73 -0
  32. data/lib/que/migrations/5/up.sql +76 -0
  33. data/lib/que/migrations/6/down.sql +8 -0
  34. data/lib/que/migrations/6/up.sql +8 -0
  35. data/lib/que/migrations/7/down.sql +5 -0
  36. data/lib/que/migrations/7/up.sql +13 -0
  37. data/lib/que/migrations.rb +37 -18
  38. data/lib/que/poller.rb +274 -0
  39. data/lib/que/rails/railtie.rb +12 -0
  40. data/lib/que/result_queue.rb +35 -0
  41. data/lib/que/sequel/model.rb +52 -0
  42. data/lib/que/utils/assertions.rb +62 -0
  43. data/lib/que/utils/constantization.rb +19 -0
  44. data/lib/que/utils/error_notification.rb +68 -0
  45. data/lib/que/utils/freeze.rb +20 -0
  46. data/lib/que/utils/introspection.rb +50 -0
  47. data/lib/que/utils/json_serialization.rb +21 -0
  48. data/lib/que/utils/logging.rb +79 -0
  49. data/lib/que/utils/middleware.rb +46 -0
  50. data/lib/que/utils/queue_management.rb +18 -0
  51. data/lib/que/utils/ruby2_keywords.rb +19 -0
  52. data/lib/que/utils/transactions.rb +34 -0
  53. data/lib/que/version.rb +5 -1
  54. data/lib/que/worker.rb +145 -149
  55. data/lib/que.rb +103 -159
  56. data/que.gemspec +17 -4
  57. data/scripts/docker-entrypoint +14 -0
  58. data/scripts/test +6 -0
  59. metadata +59 -95
  60. data/.rspec +0 -2
  61. data/.travis.yml +0 -17
  62. data/Gemfile +0 -24
  63. data/docs/advanced_setup.md +0 -106
  64. data/docs/customizing_que.md +0 -200
  65. data/docs/error_handling.md +0 -47
  66. data/docs/inspecting_the_queue.md +0 -114
  67. data/docs/logging.md +0 -50
  68. data/docs/managing_workers.md +0 -80
  69. data/docs/migrating.md +0 -30
  70. data/docs/multiple_queues.md +0 -27
  71. data/docs/shutting_down_safely.md +0 -7
  72. data/docs/using_plain_connections.md +0 -41
  73. data/docs/using_sequel.md +0 -31
  74. data/docs/writing_reliable_jobs.md +0 -117
  75. data/lib/generators/que/install_generator.rb +0 -24
  76. data/lib/generators/que/templates/add_que.rb +0 -13
  77. data/lib/que/adapters/active_record.rb +0 -54
  78. data/lib/que/adapters/base.rb +0 -127
  79. data/lib/que/adapters/connection_pool.rb +0 -16
  80. data/lib/que/adapters/pg.rb +0 -21
  81. data/lib/que/adapters/pond.rb +0 -16
  82. data/lib/que/adapters/sequel.rb +0 -20
  83. data/lib/que/railtie.rb +0 -16
  84. data/lib/que/rake_tasks.rb +0 -59
  85. data/lib/que/sql.rb +0 -152
  86. data/spec/adapters/active_record_spec.rb +0 -152
  87. data/spec/adapters/connection_pool_spec.rb +0 -22
  88. data/spec/adapters/pg_spec.rb +0 -41
  89. data/spec/adapters/pond_spec.rb +0 -22
  90. data/spec/adapters/sequel_spec.rb +0 -57
  91. data/spec/gemfiles/Gemfile1 +0 -18
  92. data/spec/gemfiles/Gemfile2 +0 -18
  93. data/spec/spec_helper.rb +0 -118
  94. data/spec/support/helpers.rb +0 -19
  95. data/spec/support/jobs.rb +0 -35
  96. data/spec/support/shared_examples/adapter.rb +0 -37
  97. data/spec/support/shared_examples/multi_threaded_adapter.rb +0 -46
  98. data/spec/travis.rb +0 -23
  99. data/spec/unit/connection_spec.rb +0 -14
  100. data/spec/unit/customization_spec.rb +0 -251
  101. data/spec/unit/enqueue_spec.rb +0 -245
  102. data/spec/unit/helper_spec.rb +0 -12
  103. data/spec/unit/logging_spec.rb +0 -101
  104. data/spec/unit/migrations_spec.rb +0 -84
  105. data/spec/unit/pool_spec.rb +0 -365
  106. data/spec/unit/run_spec.rb +0 -14
  107. data/spec/unit/states_spec.rb +0 -50
  108. data/spec/unit/stats_spec.rb +0 -46
  109. data/spec/unit/transaction_spec.rb +0 -36
  110. data/spec/unit/work_spec.rb +0 -407
  111. data/spec/unit/worker_spec.rb +0 -167
  112. data/tasks/benchmark.rb +0 -3
  113. data/tasks/rspec.rb +0 -14
  114. data/tasks/safe_shutdown.rb +0 -67
data/docs/README.md ADDED
@@ -0,0 +1,881 @@
1
+ # Que documentation
2
+
3
+ <!-- MarkdownTOC autolink=true -->
4
+
5
+ - [Command Line Interface](#command-line-interface)
6
+ - [`worker-priorities` and `worker-count`](#worker-priorities-and-worker-count)
7
+ - [`poll-interval`](#poll-interval)
8
+ - [`maximum-buffer-size`](#maximum-buffer-size)
9
+ - [`connection-url`](#connection-url)
10
+ - [`wait-period`](#wait-period)
11
+ - [`log-internals`](#log-internals)
12
+ - [Advanced Setup](#advanced-setup)
13
+ - [Using ActiveRecord Without Rails](#using-activerecord-without-rails)
14
+ - [Managing the Jobs Table](#managing-the-jobs-table)
15
+ - [Other Setup](#other-setup)
16
+ - [Error Handling](#error-handling)
17
+ - [Error Notifications](#error-notifications)
18
+ - [Error-Specific Handling](#error-specific-handling)
19
+ - [Inspecting the Queue](#inspecting-the-queue)
20
+ - [Job Stats](#job-stats)
21
+ - [Custom Queries](#custom-queries)
22
+ - [ActiveRecord Example](#activerecord-example)
23
+ - [Sequel Example](#sequel-example)
24
+ - [Managing Workers](#managing-workers)
25
+ - [Working Jobs Via Executable](#working-jobs-via-executable)
26
+ - [Thread-Unsafe Application Code](#thread-unsafe-application-code)
27
+ - [Logging](#logging)
28
+ - [Logging Job Completion](#logging-job-completion)
29
+ - [Migrating](#migrating)
30
+ - [Multiple Queues](#multiple-queues)
31
+ - [Shutting Down Safely](#shutting-down-safely)
32
+ - [Using Plain Postgres Connections](#using-plain-postgres-connections)
33
+ - [Using ConnectionPool or Pond](#using-connectionpool-or-pond)
34
+ - [Using Any Other Connection Pool](#using-any-other-connection-pool)
35
+ - [Using Sequel](#using-sequel)
36
+ - [Using Que With ActiveJob](#using-que-with-activejob)
37
+ - [Job Helper Methods](#job-helper-methods)
38
+ - [`destroy`](#destroy)
39
+ - [`finish`](#finish)
40
+ - [`expire`](#expire)
41
+ - [`retry_in`](#retry_in)
42
+ - [`error_count`](#error_count)
43
+ - [`default_resolve_action`](#default_resolve_action)
44
+ - [Writing Reliable Jobs](#writing-reliable-jobs)
45
+ - [Timeouts](#timeouts)
46
+ - [Job Options](#job-options)
47
+ - [`queue`](#queue)
48
+ - [`priority`](#priority)
49
+ - [`run_at`](#run_at)
50
+ - [`job_class`](#job_class)
51
+ - [`tags`](#tags)
52
+ - [Middleware](#middleware)
53
+ - [Defining Middleware For Jobs](#defining-middleware-for-jobs)
54
+ - [Defining Middleware For SQL statements](#defining-middleware-for-sql-statements)
55
+ - [Vacuuming](#vacuuming)
56
+ - [Enqueueing jobs in bulk](#enqueueing-jobs-in-bulk)
57
+ - [Expired jobs](#expired-jobs)
58
+ - [Finished jobs](#finished-jobs)
59
+
60
+ <!-- /MarkdownTOC -->
61
+
62
+ ## Command Line Interface
63
+
64
+ ```
65
+ usage: que [options] [file/to/require] ...
66
+ -h, --help Show this help text.
67
+ -i, --poll-interval [INTERVAL] Set maximum interval between polls for available jobs, in seconds (default: 5)
68
+ -l, --log-level [LEVEL] Set level at which to log to STDOUT (debug, info, warn, error, fatal) (default: info)
69
+ -p, --worker-priorities [LIST] List of priorities to assign to workers (default: 10,30,50,any,any,any)
70
+ -q, --queue-name [NAME] Set a queue name to work jobs from. Can be passed multiple times. (default: the default queue only)
71
+ -w, --worker-count [COUNT] Set number of workers in process (default: 6)
72
+ -v, --version Print Que version and exit.
73
+ --connection-url [URL] Set a custom database url to connect to for locking purposes.
74
+ --log-internals Log verbosely about Que's internal state. Only recommended for debugging issues
75
+ --maximum-buffer-size [SIZE] Set maximum number of jobs to be locked and held in this process awaiting a worker (default: 8)
76
+ --wait-period [PERIOD] Set maximum interval between checks of the in-memory job queue, in milliseconds (default: 50)
77
+ ```
78
+
79
+ Some explanation of the more unusual options:
80
+
81
+ ### `worker-priorities` and `worker-count`
82
+
83
+ These options dictate the size and priority distribution of the worker pool. The default worker-priorities is `10,30,50,any,any,any`. This means that the default worker pool will reserve one worker to only works jobs with priorities under 10, one for priorities under 30, and one for priorities under 50. Three more workers will work any job.
84
+
85
+ For example, with these defaults, you could have a large backlog of jobs of priority 100. When a more important job (priority 40) comes in, there's guaranteed to be a free worker. If the process then becomes saturated with jobs of priority 40, and then a priority 20 job comes in, there's guaranteed to be a free worker for it, and so on. You can pass a priority more than once to have multiple workers at that level (for example: `--worker-priorities=100,100,any,any`). This gives you a lot of freedom to manage your worker capacity at different priority levels.
86
+
87
+ Instead of passing worker-priorities, you can pass a `worker-count` - this is a shorthand for creating the given number of workers at the `any` priority level. So, `--worker-count=3` is just like passing equivalent to `worker-priorities=any,any,any`.
88
+
89
+ If you pass both worker-count and worker-priorities, the count will trim or pad the priorities list with `any` workers. So, `--worker-priorities=20,30,40 --worker-count=6` would be the same as passing `--worker-priorities=20,30,40,any,any,any`.
90
+
91
+ ### `poll-interval`
92
+
93
+ This option sets the number of seconds the process will wait between polls of the job queue. Jobs that are ready to be worked immediately will be broadcast via the LISTEN/NOTIFY system, so polling is unnecessary for them - polling is only necessary for jobs that are scheduled in the future or which are being delayed due to errors. The default is 5 seconds.
94
+
95
+ ### `maximum-buffer-size`
96
+
97
+ This option sets the size of the internal buffer that Que uses to hold jobs until they're ready for workers. The default maximum is 8, meaning that the process won't buffer more than 8 jobs that aren't yet ready to be worked. If you don't want jobs to be buffered at all, you can set this value to zero.
98
+
99
+ ### `connection-url`
100
+
101
+ This option sets the URL to be used to open a connection to the database for locking purposes. By default, Que will simply use a connection from the connection pool for locking - this option is only useful if your application connections can't use advisory locks - for example, if they're passed through an external connection pool like PgBouncer. In that case, you'll need to use this option to specify your actual database URL so that Que can establish a direct connection.
102
+
103
+ ### `wait-period`
104
+
105
+ This option specifies (in milliseconds) how often the locking thread wakes up to check whether the workers have finished jobs, whether it's time to poll, etc. You shouldn't generally need to tweak this, but it may come in handy for some workloads. The default is 50 milliseconds.
106
+
107
+ ### `log-internals`
108
+
109
+ This option instructs Que to output a lot of information about its internal state to the logger. It should only be used if it becomes necessary to debug issues.
110
+
111
+ ## Advanced Setup
112
+
113
+ ### Using ActiveRecord Without Rails
114
+
115
+ If you're using both Rails and ActiveRecord, the README describes how to get started with Que (which is pretty straightforward, since it includes a Railtie that handles a lot of setup for you). Otherwise, you'll need to do some manual setup.
116
+
117
+ If you're using ActiveRecord outside of Rails, you'll need to tell Que to piggyback on its connection pool after you've connected to the database:
118
+
119
+ ```ruby
120
+ ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])
121
+
122
+ require 'que'
123
+ Que.connection = ActiveRecord
124
+ ```
125
+
126
+ Then you can queue jobs just as you would in Rails:
127
+
128
+ ```ruby
129
+ ActiveRecord::Base.transaction do
130
+ @user = User.create(params[:user])
131
+ SendRegistrationEmail.enqueue user_id: @user.id
132
+ end
133
+ ```
134
+
135
+ There are other docs to read if you're using [Sequel](#using-sequel) or [plain Postgres connections](#using-plain-postgres-connections) (with no ORM at all) instead of ActiveRecord.
136
+
137
+ ### Managing the Jobs Table
138
+
139
+ After you've connected Que to the database, you can manage the jobs table. You'll want to migrate to a specific version in a migration file, to ensure that they work the same way even when you upgrade Que in the future:
140
+
141
+ ```ruby
142
+ # Update the schema to version #6.
143
+ Que.migrate!(version: 6)
144
+
145
+ # Remove Que's jobs table entirely.
146
+ Que.migrate!(version: 0)
147
+ ```
148
+
149
+ There's also a helper method to clear all jobs from the jobs table:
150
+
151
+ ```ruby
152
+ Que.clear!
153
+ ```
154
+
155
+ ### Other Setup
156
+
157
+ Be sure to read the docs on [managing workers](#managing-workers) for more information on using the worker pool.
158
+
159
+ You'll also want to set up [logging](#logging) and an [error handler](#error-handling) to track errors raised by jobs.
160
+
161
+ ## Error Handling
162
+
163
+ If an error is raised and left uncaught by your job, Que will save the error message and backtrace to the database and schedule the job to be retried later.
164
+
165
+ If a given job fails repeatedly, Que will retry it at exponentially-increasing intervals equal to (failure_count^4 + 3) seconds. This means that a job will be retried 4 seconds after its first failure, 19 seconds after its second, 84 seconds after its third, 259 seconds after its fourth, and so on until it succeeds. This pattern is very similar to DelayedJob's. Alternately, you can define your own retry logic by setting an interval to delay each time, or a callable that accepts the number of failures and returns an interval:
166
+
167
+ ```ruby
168
+ class MyJob < Que::Job
169
+ # Just retry a failed job every 5 seconds:
170
+ self.retry_interval = 5
171
+
172
+ # Always retry this job immediately (not recommended, or transient
173
+ # errors will spam your error reporting):
174
+ self.retry_interval = 0
175
+
176
+ # Increase the delay by 30 seconds every time this job fails:
177
+ self.retry_interval = proc { |count| count * 30 }
178
+ end
179
+ ```
180
+
181
+ There is a maximum_retry_count option for jobs. It defaults to 15 retries, which with the default retry interval means that a job will stop retrying after a little more than two days.
182
+
183
+ ### Error Notifications
184
+
185
+ If you're using an error notification system (highly recommended, of course), you can hook Que into it by setting a callable as the error notifier:
186
+
187
+ ```ruby
188
+ Que.error_notifier = proc do |error, job|
189
+ # Do whatever you want with the error object or job row here. Note that the
190
+ # job passed is not the actual job object, but the hash representing the job
191
+ # row in the database, which looks like:
192
+
193
+ # {
194
+ # :priority => 100,
195
+ # :run_at => "2017-09-15T20:18:52.018101Z",
196
+ # :id => 172340879,
197
+ # :job_class => "TestJob",
198
+ # :error_count => 0,
199
+ # :last_error_message => nil,
200
+ # :queue => "default",
201
+ # :last_error_backtrace => nil,
202
+ # :finished_at => nil,
203
+ # :expired_at => nil,
204
+ # :args => [],
205
+ # :data => {}
206
+ # }
207
+
208
+ # This is done because the job may not have been able to be deserialized
209
+ # properly, if the name of the job class was changed or the job class isn't
210
+ # loaded for some reason. The job argument may also be nil, if there was a
211
+ # connection failure or something similar.
212
+ end
213
+ ```
214
+
215
+ ### Error-Specific Handling
216
+
217
+ You can also define a handle_error method in your job, like so:
218
+
219
+ ```ruby
220
+ class MyJob < Que::Job
221
+ def run(*args)
222
+ # Your code goes here.
223
+ end
224
+
225
+ def handle_error(error)
226
+ case error
227
+ when TemporaryError then retry_in 10.seconds
228
+ when PermanentError then expire
229
+ else super # Default (exponential backoff) behavior.
230
+ end
231
+ end
232
+ end
233
+ ```
234
+
235
+ The return value of handle_error determines whether the error object is passed to the error notifier. The helper methods like expire and retry_in return true, so these errors will be notified. You can explicitly return false to skip notification.
236
+
237
+ ```ruby
238
+ class MyJob < Que::Job
239
+ def handle_error(error)
240
+ case error
241
+ when AnnoyingError
242
+ retry_in 10.seconds
243
+ false
244
+ when TransientError
245
+ super
246
+ error_count > 3
247
+ else
248
+ super # Default (exponential backoff) behavior.
249
+ end
250
+ end
251
+ end
252
+ ```
253
+
254
+ In this example, AnnoyingError will never be notified, while TransientError will only be notified once it has affected a given job at least three times.
255
+
256
+ ## Inspecting the Queue
257
+
258
+ In order to remain simple and compatible with any ORM (or no ORM at all), Que is really just a very thin wrapper around some raw SQL. There are two methods available that query the jobs table and Postgres' system catalogs to retrieve information on the current state of the queue:
259
+
260
+ ### Job Stats
261
+
262
+ You can call `Que.job_stats` to return some aggregate data on the types of jobs currently in the queue. Example output:
263
+
264
+ ```ruby
265
+ [
266
+ {
267
+ :job_class=>"ChargeCreditCard",
268
+ :count=>10,
269
+ :count_working=>4,
270
+ :count_errored=>2,
271
+ :highest_error_count=>5,
272
+ :oldest_run_at=>2017-09-08 16:13:18 -0400
273
+ },
274
+ {
275
+ :job_class=>"SendRegistrationEmail",
276
+ :count=>1,
277
+ :count_working=>0,
278
+ :count_errored=>0,
279
+ :highest_error_count=>0,
280
+ :oldest_run_at=>2017-09-08 17:13:18 -0400
281
+ }
282
+ ]
283
+ ```
284
+
285
+ This tells you that, for instance, there are ten ChargeCreditCard jobs in the queue, four of which are currently being worked, and two of which have experienced errors. One of them has started to process but experienced an error five times. The oldest_run_at is helpful for determining how long jobs have been sitting around, if you have a large backlog.
286
+
287
+ ### Custom Queries
288
+
289
+ If you're using ActiveRecord or Sequel, Que ships with models that wrap the job queue so you can write your own logic to inspect it. They include some helpful scopes to write your queries - see the gem source for a complete accounting.
290
+
291
+ #### ActiveRecord Example
292
+
293
+ ``` ruby
294
+ # app/models/que_job.rb
295
+
296
+ require 'que/active_record/model'
297
+
298
+ class QueJob < Que::ActiveRecord::Model
299
+ end
300
+
301
+ QueJob.finished.to_sql # => "SELECT \"que_jobs\".* FROM \"que_jobs\" WHERE (\"que_jobs\".\"finished_at\" IS NOT NULL)"
302
+
303
+ # You could also name the model whatever you like, or just query from
304
+ # Que::ActiveRecord::Model directly if you don't need to write your own model
305
+ # logic.
306
+ ```
307
+
308
+ #### Sequel Example
309
+
310
+ ``` ruby
311
+ # app/models/que_job.rb
312
+
313
+ require 'que/sequel/model'
314
+
315
+ class QueJob < Que::Sequel::Model
316
+ end
317
+
318
+ QueJob.finished # => #<Sequel::Postgres::Dataset: "SELECT * FROM \"public\".\"que_jobs\" WHERE (\"public\".\"que_jobs\".\"finished_at\" IS NOT NULL)">
319
+ ```
320
+
321
+ ## Managing Workers
322
+
323
+ Que uses a multithreaded pool of workers to run jobs in parallel - this allows you to save memory by working many jobs simultaneously in the same process. The `que` executable starts up a pool of 6 workers by default. This is fine for most use cases, but the ideal number for your app will depend on your interpreter and what types of jobs you're running.
324
+
325
+ Ruby MRI has a global interpreter lock (GIL), which prevents it from using more than one CPU core at a time. Having multiple workers running makes sense if your jobs tend to spend a lot of time in I/O (waiting on complex database queries, sending emails, making HTTP requests, etc.), as most jobs do. However, if your jobs are doing a lot of work in Ruby, they'll be spending a lot of time blocking each other, and having too many workers running will cause you to lose efficiency to context-switching. So, you'll want to choose the appropriate number of workers for your use case.
326
+
327
+ ### Working Jobs Via Executable
328
+
329
+ ```shell
330
+ # Run a pool of 6 workers:
331
+ que
332
+
333
+ # Or configure the number of workers:
334
+ que --worker-count 10
335
+ ```
336
+
337
+ See `que -h` for a complete list of command-line options.
338
+
339
+ ### Thread-Unsafe Application Code
340
+
341
+ If your application code is not thread-safe, you won't want any workers to be processing jobs while anything else is happening in the Ruby process. So, you'll want to run a single worker at a time, like so:
342
+
343
+ ```shell
344
+ que --worker-count 1
345
+ ```
346
+
347
+ ## Logging
348
+
349
+ By default, Que logs important information in JSON to either Rails' logger (when running in a Rails web process) or STDOUT (when running via the `que` executable). So, your logs will look something like:
350
+
351
+ ```
352
+ I, [2017-08-12T05:07:31.094201 #4687] INFO -- : {"lib":"que","hostname":"lovelace","pid":21626,"thread":21471100,"event":"job_worked","job_id":6157665,"elapsed":0.531411}
353
+ ```
354
+
355
+ Of course you can have it log wherever you like:
356
+
357
+ ```ruby
358
+ Que.logger = Logger.new(...)
359
+ ```
360
+
361
+ If you don't like logging in JSON, you can also customize the format of the logging output by passing a callable object (such as a proc) to Que.log_formatter=. The proc should take a hash (the keys are symbols) and return a string. The keys and values are just as you would expect from the JSON output:
362
+
363
+ ```ruby
364
+ Que.log_formatter = proc do |data|
365
+ "Thread number #{data[:thread]} experienced a #{data[:event]}"
366
+ end
367
+ ```
368
+
369
+ If the log formatter returns nil or false, nothing will be logged at all. You could use this to narrow down what you want to emit, for example:
370
+
371
+ ```ruby
372
+ Que.log_formatter = proc do |data|
373
+ if [:job_worked, :job_unavailable].include?(data[:event])
374
+ JSON.dump(data)
375
+ end
376
+ end
377
+ ```
378
+
379
+ ### Logging Job Completion
380
+
381
+ Que logs a `job_worked` event whenever a job completes, though by default this event is logged at the `DEBUG` level. Since people often run their applications at the `INFO` level or above, this can make the logs too silent for some use cases. Similarly, you may want to log at a higher level if a time-sensitive job begins taking too long to run.
382
+
383
+ You can solve these problems by configuring the level at which a job is logged on a per-job basis. Simply define a `log_level` method in your job class - it will be called with a float representing the number of seconds it took for the job to run, and it should return a symbol indicating what level to log the job at:
384
+
385
+ ```ruby
386
+ class TimeSensitiveJob < Que::Job
387
+ def run(*args)
388
+ RemoteAPI.execute_important_request
389
+ end
390
+
391
+ def log_level(elapsed)
392
+ if elapsed > 60
393
+ # This job took over a minute! We should complain about it!
394
+ :warn
395
+ elsif elapsed > 30
396
+ # A little long, but no big deal!
397
+ :info
398
+ else
399
+ # This is fine, don't bother logging at all.
400
+ false
401
+ end
402
+ end
403
+ end
404
+ ```
405
+
406
+ This method should return a symbol that is a valid logging level (one of `[:debug, :info, :warn, :error, :fatal, :unknown]`). If the method returns anything other than one of these symbols, the job won't be logged.
407
+
408
+ If a job errors, a `job_errored` event will be emitted at the `ERROR` log level. This is not currently configurable.
409
+
410
+ ## Migrating
411
+
412
+ Some new releases of Que may require updates to the database schema. It's recommended that you integrate these updates alongside your other database migrations. For example, when Que released version 0.6.0, the schema version was updated from 2 to 3. If you're running ActiveRecord, you could make a migration to perform this upgrade like so:
413
+
414
+ ```ruby
415
+ class UpdateQue < ActiveRecord::Migration[5.0]
416
+ def self.up
417
+ Que.migrate!(version: 3)
418
+ end
419
+
420
+ def self.down
421
+ Que.migrate!(version: 2)
422
+ end
423
+ end
424
+ ```
425
+
426
+ This will make sure that your database schema stays consistent with your codebase. If you're looking for something quicker and dirtier, you can always manually migrate in a console session:
427
+
428
+ ```ruby
429
+ # Change schema to version 3.
430
+ Que.migrate!(version: 3)
431
+
432
+ # Check your current schema version.
433
+ Que.db_version #=> 3
434
+ ```
435
+
436
+ Note that you can remove Que from your database completely by migrating to version 0.
437
+
438
+ ## Multiple Queues
439
+
440
+ Que supports the use of multiple queues in a single job table. Please note that this feature is intended to support the case where multiple codebases are sharing the same job queue - if you want to support jobs of differing priorities, the numeric priority system offers better flexibility and performance.
441
+
442
+ For instance, you might have a separate Ruby application that handles only processing credit cards. In that case, you can run that application's workers against a specific queue:
443
+
444
+ ```shell
445
+ que --queue-name credit_cards
446
+ # The -q flag is equivalent, and either can be passed multiple times.
447
+ que -q default -q credit_cards
448
+ ```
449
+
450
+ Then you can set jobs to be enqueued in that queue specifically:
451
+
452
+ ```ruby
453
+ ProcessCreditCard.enqueue(current_user.id, job_options: { queue: 'credit_cards' })
454
+
455
+ # Or:
456
+
457
+ class ProcessCreditCard < Que::Job
458
+ # Set a default queue for this job class; this can be overridden by
459
+ # passing the :queue parameter to enqueue like above.
460
+ self.queue = 'credit_cards'
461
+ end
462
+ ```
463
+
464
+ In some cases, the `ProcessCreditCard` class may not be defined in the application that is enqueueing the job. In that case, you can [specify the job class as a string](#job_class).
465
+
466
+ ## Shutting Down Safely
467
+
468
+ To ensure safe operation, Que needs to be very careful in how it shuts down. When a Ruby process ends normally, it calls Thread#kill on any threads that are still running - unfortunately, if a thread is in the middle of a transaction when this happens, there is a risk that it will be prematurely commited, resulting in data corruption. See [here](http://blog.headius.com/2008/02/ruby-threadraise-threadkill-timeoutrb.html) and [here](http://coderrr.wordpress.com/2011/05/03/beware-of-threadkill-or-your-activerecord-transactions-are-in-danger-of-being-partially-committed/) for more detail on this.
469
+
470
+ To prevent this, Que will block the worker process from exiting until all jobs it is working have completed normally. Unfortunately, if you have long-running jobs, this may take a very long time (and if something goes wrong with a job's logic, it may never happen). The solution in this case is SIGKILL - luckily, Ruby processes that are killed via SIGKILL will end without using Thread#kill on its running threads. This is safer than exiting normally - when PostgreSQL loses the connection it will simply roll back the open transaction, if any, and unlock the job so it can be retried later by another worker. Be sure to read [Writing Reliable Jobs](#writing-reliable-jobs) for information on how to design your jobs to fail safely.
471
+
472
+ So, be prepared to use SIGKILL on your Ruby processes if they run for too long. For example, Heroku takes a good approach to this - when Heroku's platform is shutting down a process, it sends SIGTERM, waits ten seconds, then sends SIGKILL if the process still hasn't exited. This is a nice compromise - it will give each of your currently running jobs ten seconds to complete, and any jobs that haven't finished by then will be interrupted and retried later.
473
+
474
+ ## Using Plain Postgres Connections
475
+
476
+ If you're not using an ORM like ActiveRecord or Sequel, you can use a distinct connection pool to manage your Postgres connections. Please be aware that if you **are** using ActiveRecord or Sequel, there's no reason for you to be using any of these methods - it's less efficient (unnecessary connections will waste memory on your database server) and you lose the reliability benefits of wrapping jobs in the same transactions as the rest of your data.
477
+
478
+ ### Using ConnectionPool or Pond
479
+
480
+ Support for two connection pool gems is included in Que. The first is the ConnectionPool gem (be sure to add `gem 'connection_pool'` to your Gemfile):
481
+
482
+ ```ruby
483
+ require 'uri'
484
+ require 'pg'
485
+ require 'connection_pool'
486
+
487
+ uri = URI.parse(ENV['DATABASE_URL'])
488
+
489
+ Que.connection = ConnectionPool.new(size: 10) do
490
+ PG::Connection.open(
491
+ host: uri.host,
492
+ user: uri.user,
493
+ password: uri.password,
494
+ port: uri.port || 5432,
495
+ dbname: uri.path[1..-1]
496
+ )end
497
+ ```
498
+
499
+ Be sure to pick your pool size carefully - if you use 10 for the size, you'll incur the overhead of having 10 connections open to Postgres even if you never use more than a couple of them.
500
+
501
+ The Pond gem doesn't have this drawback - it is very similar to ConnectionPool, but establishes connections lazily (add `gem 'pond'` to your Gemfile):
502
+
503
+ ```ruby
504
+ require 'uri'
505
+ require 'pg'
506
+ require 'pond'
507
+
508
+ uri = URI.parse(ENV['DATABASE_URL'])
509
+
510
+ Que.connection = Pond.new(maximum_size: 10) do
511
+ PG::Connection.open(
512
+ host: uri.host,
513
+ user: uri.user,
514
+ password: uri.password,
515
+ port: uri.port || 5432,
516
+ dbname: uri.path[1..-1]
517
+ )
518
+ end
519
+ ```
520
+
521
+ ### Using Any Other Connection Pool
522
+
523
+ You can use any other in-process connection pool by defining access to it in a proc that's passed to `Que.connection_proc = proc`. The proc you pass should accept a block and call it with a connection object. For instance, Que's built-in interface to Sequel's connection pool is basically implemented like:
524
+
525
+ ```ruby
526
+ Que.connection_proc = proc do |&block|
527
+ DB.synchronize do |connection|
528
+ block.call(connection)
529
+ end
530
+ end
531
+ ```
532
+
533
+ This proc must meet a few requirements:
534
+ - The yielded object must be an instance of `PG::Connection`.
535
+ - It must be reentrant - if it is called with a block, and then called again inside that block, it must return the same object. For example, in `proc.call{|conn1| proc.call{|conn2| conn1.object_id == conn2.object_id}}` the innermost condition must be true.
536
+ - It must lock the connection object and prevent any other thread from accessing it for the duration of the block.
537
+
538
+ If any of these conditions aren't met, Que will raise an error.
539
+
540
+ ## Using Sequel
541
+
542
+ If you're using Sequel, with or without Rails, you'll need to give Que a specific database instance to use:
543
+
544
+ ```ruby
545
+ DB = Sequel.connect(ENV['DATABASE_URL'])
546
+ Que.connection = DB
547
+ ```
548
+
549
+ If you are using Sequel's migrator, your app initialization won't happen, so you may need to tweak your migrations to `require 'que'` and set its connection:
550
+
551
+ ```ruby
552
+ require 'que'
553
+ Sequel.migration do
554
+ up do
555
+ Que.connection = self
556
+ Que.migrate!(version: 6)
557
+ end
558
+ down do
559
+ Que.connection = self
560
+ Que.migrate!(version: 0)
561
+ end
562
+ end
563
+ ```
564
+
565
+ Then you can safely use the same database object to transactionally protect your jobs:
566
+
567
+ ```ruby
568
+ class MyJob < Que::Job
569
+ def run(user_id:)
570
+ # Do stuff.
571
+
572
+ DB.transaction do
573
+ # Make changes to the database.
574
+
575
+ # Destroying this job will be protected by the same transaction.
576
+ destroy
577
+ end
578
+ end
579
+ end
580
+
581
+ # Or, in your controller action:
582
+ DB.transaction do
583
+ @user = User.create(params[:user])
584
+ MyJob.enqueue user_id: @user.id
585
+ end
586
+ ```
587
+
588
+ Sequel automatically wraps model persistance actions (create, update, destroy) in transactions, so you can simply call #enqueue methods from your models' callbacks, if you wish.
589
+
590
+ ## Using Que With ActiveJob
591
+
592
+ You can include `Que::ActiveJob::JobExtensions` into your `ApplicationJob` subclass to get support for all of Que's
593
+ [helper methods](#job-helper-methods). These methods will become no-ops if you use a queue adapter that isn't Que, so if you like to use a different adapter in development they shouldn't interfere.
594
+
595
+ Additionally, including `Que::ActiveJob::JobExtensions` lets you define a run() method that supports keyword arguments.
596
+
597
+ ## Job Helper Methods
598
+
599
+ There are a number of instance methods on Que::Job that you can use in your jobs, preferably in transactions. See [Writing Reliable Jobs](#writing-reliable-jobs) for more information on where to use these methods.
600
+
601
+ ### `destroy`
602
+
603
+ This method deletes the job from the queue table, ensuring that it won't be worked a second time.
604
+
605
+ ### `finish`
606
+
607
+ This method marks the current job as finished, ensuring that it won't be worked a second time. This is like destroy, in that it finalizes a job, but this method leaves the job in the table, in case you want to query it later.
608
+
609
+ ### `expire`
610
+
611
+ This method marks the current job as expired. It will be left in the table and won't be retried, but it will be easy to query for expired jobs. This method is called if the job exceeds its maximum_retry_count.
612
+
613
+ ### `retry_in`
614
+
615
+ This method marks the current job to be retried later. You can pass a numeric to this method, in which case that is the number of seconds after which it can be retried (`retry_in(10)`, `retry_in(0.5)`), or, if you're using ActiveSupport, you can pass in a duration object (`retry_in(10.minutes)`). This automatically happens, with an exponentially-increasing interval, when the job encounters an error.
616
+
617
+ Note that `retry_in` increments the job's `error_count`.
618
+
619
+ ### `error_count`
620
+
621
+ This method returns the total number of times the job has errored, in case you want to modify the job's behavior after it has failed a given number of times.
622
+
623
+ ### `default_resolve_action`
624
+
625
+ If you don't perform a resolve action (destroy, finish, expire, retry_in) while the job is worked, Que will call this method for you. By default it simply calls `destroy`, but you can override it in your Job subclasses if you wish - for example, to call `finish`, or to invoke some more complicated logic.
626
+
627
+ ## Writing Reliable Jobs
628
+
629
+ Que does everything it can to ensure that jobs are worked exactly once, but if something bad happens when a job is halfway completed, there's no way around it - the job will need be repeated over again from the beginning, probably by a different worker. When you're writing jobs, you need to be prepared for this to happen.
630
+
631
+ The safest type of job is one that reads in data, either from the database or from external APIs, then does some number crunching and writes the results to the database. These jobs are easy to make safe - simply write the results to the database inside a transaction, and also destroy the job inside that transaction, like so:
632
+
633
+ ```ruby
634
+ class UpdateWidgetPrice < Que::Job
635
+ def run(widget_id)
636
+ widget = Widget[widget_id]
637
+ price = ExternalService.get_widget_price(widget_id)
638
+
639
+ ActiveRecord::Base.transaction do
640
+ # Make changes to the database.
641
+ widget.update price: price
642
+
643
+ # Mark the job as destroyed, so it doesn't run again.
644
+ destroy
645
+ end
646
+ end
647
+ end
648
+ ```
649
+
650
+ Here, you're taking advantage of the guarantees of an [ACID](https://en.wikipedia.org/wiki/ACID) database. The job is destroyed along with the other changes, so either the write will succeed and the job will be run only once, or it will fail and the database will be left untouched. But even if it fails, the job can simply be retried, and there are no lingering effects from the first attempt, so no big deal.
651
+
652
+ The more difficult type of job is one that makes changes that can't be controlled transactionally. For example, writing to an external service:
653
+
654
+ ```ruby
655
+ class ChargeCreditCard < Que::Job
656
+ def run(user_id, credit_card_id)
657
+ CreditCardService.charge(credit_card_id, amount: "$10.00")
658
+
659
+ ActiveRecord::Base.transaction do
660
+ User.where(id: user_id).update_all charged_at: Time.now
661
+ destroy
662
+ end
663
+ end
664
+ end
665
+ ```
666
+
667
+ What if the process abruptly dies after we tell the provider to charge the credit card, but before we finish the transaction? Que will retry the job, but there's no way to tell where (or even if) it failed the first time. The credit card will be charged a second time, and then you've got an angry customer. The ideal solution in this case is to make the job [idempotent](https://en.wikipedia.org/wiki/Idempotence), meaning that it will have the same effect no matter how many times it is run:
668
+
669
+ ```ruby
670
+ class ChargeCreditCard < Que::Job
671
+ def run(user_id, credit_card_id)
672
+ unless CreditCardService.check_for_previous_charge(credit_card_id)
673
+ CreditCardService.charge(credit_card_id, amount: "$10.00")
674
+ end
675
+
676
+ ActiveRecord::Base.transaction do
677
+ User.where(id: user_id).update_all charged_at: Time.now
678
+ destroy
679
+ end
680
+ end
681
+ end
682
+ ```
683
+
684
+ This makes the job slightly more complex, but reliable (or, at least, as reliable as your credit card service).
685
+
686
+ Finally, there are some jobs where you won't want to write to the database at all:
687
+
688
+ ```ruby
689
+ class SendVerificationEmail < Que::Job
690
+ def run(email_address)
691
+ Mailer.verification_email(email_address).deliver
692
+ end
693
+ end
694
+ ```
695
+
696
+ In this case, we don't have a way to prevent the occasional double-sending of an email. But, for ease of use, you can leave out the transaction and the `destroy` call entirely - Que will recognize that the job wasn't destroyed and will clean it up for you.
697
+
698
+ ### Timeouts
699
+
700
+ Long-running jobs aren't necessarily a problem for the database, since the overhead of an individual job is very small (just an advisory lock held in memory). But jobs that hang indefinitely can tie up a worker and [block the Ruby process from exiting gracefully](#shutting-down-safely), which is a pain.
701
+
702
+ If there's part of your job that is prone to hang (due to an API call or other HTTP request that never returns, for example), you can (and should) timeout those parts of your job. For example, consider a job that needs to make an HTTP request and then write to the database:
703
+
704
+ ```ruby
705
+ class ScrapeStuff < Que::Job
706
+ def run(url_to_scrape)
707
+ result = YourHTTPLibrary.get(url_to_scrape)
708
+
709
+ ActiveRecord::Base.transaction do
710
+ # Insert result...
711
+
712
+ destroy
713
+ end
714
+ end
715
+ end
716
+ ```
717
+
718
+ That request could take a very long time, or never return at all. Let's use the timeout feature that almost all HTTP libraries offer some version of:
719
+
720
+ ```ruby
721
+ class ScrapeStuff < Que::Job
722
+ def run(url_to_scrape)
723
+ result = YourHTTPLibrary.get(url_to_scrape, timeout: 5)
724
+
725
+ ActiveRecord::Base.transaction do
726
+ # Insert result...
727
+
728
+ destroy
729
+ end
730
+ end
731
+ end
732
+ ```
733
+
734
+ Now, if the request takes more than five seconds, an error will be raised (probably - check your library's documentation) and Que will just retry the job later.
735
+
736
+ ## Job Options
737
+
738
+ When enqueueing a job, you can specify particular options for it in a `job_options` hash, e.g.:
739
+
740
+ ```ruby
741
+ ChargeCreditCard.enqueue(card.id, user_id: current_user.id, job_options: { run_at: 1.day.from_now, priority: 5 })
742
+ ```
743
+
744
+ ### `queue`
745
+
746
+ See [Multiple Queues](#multiple-queues).
747
+
748
+ ### `priority`
749
+
750
+ Provide an integer to customise the priority level of the job.
751
+
752
+ We use the Linux priority scale - a lower number is more important.
753
+
754
+ ### `run_at`
755
+
756
+ Provide a `Time` as the `run_at` to make a job run at a later time (well, at some point after it, depending on how busy the workers are).
757
+
758
+ It's best not to use `Time.now` here, as the current time in the Ruby process and the database won't be perfectly aligned. When the database considers the `run_at` to be in the past, the job will not be broadcast via the LISTEN/NOTIFY system, and it will need to wait for a poll. This introduces an unnecessary delay of probably a few seconds (depending on your configured [poll interval](#poll-interval)). So if you want the job to run ASAP, just omit the `run_at` option.
759
+
760
+ ### `job_class`
761
+
762
+ Specifying `job_class` allows you to enqueue a job using `Que.enqueue`:
763
+
764
+ ```ruby
765
+ Que.enqueue(current_user.id, job_options: { job_class: 'ProcessCreditCard' })
766
+ ```
767
+
768
+ Rather than needing to use the job class (nor even have it defined in the enqueueing process):
769
+
770
+ ```ruby
771
+ ProcessCreditCard.enqueue(current_user.id)
772
+ ```
773
+
774
+ ### `tags`
775
+
776
+ You can provide an array of strings to give a job some tags. These are not used by Que and are completely custom.
777
+
778
+ A job can have up to five tags, each one up to 100 characters long.
779
+
780
+ Note that unlike the other job options, tags are stored within the `que_jobs.data` column, rather than a correspondingly-named column.
781
+
782
+ ## Middleware
783
+
784
+ A new feature in 1.0 is support for custom middleware around various actions.
785
+
786
+ This API is experimental for the 1.0 beta and may change.
787
+
788
+ ### Defining Middleware For Jobs
789
+
790
+ You can define middleware to wrap worked jobs. You can use this to add custom instrumentation around jobs, log how long they take to complete, etc.
791
+
792
+ ``` ruby
793
+ Que.job_middleware.push(
794
+ -> (job, &block) {
795
+ # Do stuff with the job object - report on it, count time elapsed, etc.
796
+ block.call
797
+ nil # Doesn't matter what's returned.
798
+ }
799
+ )
800
+ ```
801
+
802
+ ### Defining Middleware For SQL statements
803
+
804
+ SQL middleware wraps queries that Que executes, or which you might decide to execute via Que.execute(). You can use hook this into NewRelic or a similar service to instrument how long SQL queries take, for example.
805
+
806
+ ``` ruby
807
+ Que.sql_middleware.push(
808
+ -> (sql, params, &block) {
809
+ Service.instrument(sql: sql, params: params) do
810
+ block.call
811
+ end
812
+ nil # Still doesn't matter what's returned.
813
+ }
814
+ )
815
+ ```
816
+
817
+ Please be careful with what you do inside an SQL middleware - this code will execute inside Que's locking thread, which runs in a fairly tight loop that is optimized for performance. If you do something inside this block that incurs blocking I/O (like synchronously touching an external service) you may find Que being less able to pick up jobs quickly.
818
+
819
+ ## Vacuuming
820
+
821
+ Because the que_jobs table is "high churn" (lots of rows are being created and deleted), it needs to be vacuumed fairly frequently to keep the dead tuple count down otherwise [acquring a job to work will start taking longer and longer](https://brandur.org/postgres-queues).
822
+
823
+ In many cases postgres will vacuum these dead tuples automatically using autovacuum, so no intervention is required. However, if your database has a lot of other large tables that take hours for autovacuum to run on, it is possible that there won't be any autovacuum processes available within a reasonable amount of time. If that happens the dead tuple count on the que_jobs table will reach a point where it starts taking so long to acquire a job to work that the jobs are being added faster than they can be worked.
824
+
825
+ In order to avoid this situation you can kick off a manual vacuum against the que_jobs table on a regular basis. This manual vacuum will be more aggressive than an autovacuum since by default it does not back-off and sleep, so you will want to make sure your server has enough disk I/O available to handle the vacuum + any autovacuums + your workload + some overhead. However, by keeping the interval between vacuums small you will also be limiting the amount of work to be done which will aleviate some of the afforementiond risk of I/O usage.
826
+
827
+ Here is an example recurring manual vacuum job that assumes you are using Sequel:
828
+
829
+ ```
830
+ class ManualVacuumJob < CronJob
831
+ self.priority = 1 # set this to the highest priority since it keeps the table healthy for other jobs
832
+ INTERVAL = 300
833
+
834
+ def run(args)
835
+ DB.run "VACUUM VERBOSE ANALYZE que_jobs"
836
+ end
837
+ end
838
+ ```
839
+
840
+ ## Enqueueing jobs in bulk
841
+
842
+ If you need to enqueue a large number of jobs at once, enqueueing each one separately (and running the notify trigger for each) can become a performance bottleneck. To mitigate this, there is a bulk enqueue interface:
843
+
844
+ ```ruby
845
+ Que.bulk_enqueue do
846
+ MyJob.enqueue(user_id: 1)
847
+ MyJob.enqueue(user_id: 2)
848
+ # ...
849
+ end
850
+ ```
851
+
852
+ The jobs are only actually enqueued at the end of the block, at which point they are inserted into the database in one big query.
853
+
854
+ Limitations:
855
+
856
+ - ActiveJob is not supported
857
+ - All jobs must use the same job class
858
+ - All jobs must use the same `job_options` (`job_options` must be provided to `.bulk_enqueue` instead of `.enqueue`)
859
+ - The `que_attrs` of a job instance returned from `.enqueue` is empty (`{}`)
860
+ - The notify trigger is not run by default, so jobs will only be picked up by a worker upon its next poll
861
+
862
+ If you still want the notify trigger to run for each job, use `Que.bulk_enqueue(notify: true) { ... }`.
863
+
864
+ ## Expired jobs
865
+
866
+ Expired jobs hang around in the `que_jobs` table. If necessary, you can get an expired job to run again by clearing the `error_count` and `expired_at` columns, e.g.:
867
+
868
+ ```sql
869
+ UPDATE que_jobs SET error_count = 0, expired_at = NULL WHERE id = 172340879;
870
+ ```
871
+
872
+ ## Finished jobs
873
+
874
+ If you prefer to leave finished jobs in the database for a while, to performantly remove them periodically, you can use something like:
875
+
876
+ ```sql
877
+ BEGIN;
878
+ SET LOCAL que.skip_notify TO true;
879
+ DELETE FROM que_jobs WHERE finished_at < (select now() - interval '7 days');
880
+ COMMIT;
881
+ ```