Browse Source

Begin work on asynchronous jobs

Maarten van den Berg 7 years ago
parent
commit
551451eb79
6 changed files with 59 additions and 1 deletions
  1. 5 0
      Gemfile
  2. 9 0
      Gemfile.lock
  3. 5 0
      bin/delayed_job
  4. 2 0
      config/application.rb
  5. 22 0
      db/migrate/20170916212858_create_delayed_jobs.rb
  6. 16 1
      db/schema.rb

+ 5 - 0
Gemfile

59
 gem 'will_paginate', '~> 3.1.0'
59
 gem 'will_paginate', '~> 3.1.0'
60
 gem 'bootstrap-will_paginate'
60
 gem 'bootstrap-will_paginate'
61
 
61
 
62
+# Delayed jobs
63
+gem 'delayed_job'
64
+gem 'delayed_job_active_record'
65
+gem 'daemons'
66
+
62
 group :development, :test do
67
 group :development, :test do
63
   # Call 'byebug' anywhere in the code to stop execution and get a debugger console
68
   # Call 'byebug' anywhere in the code to stop execution and get a debugger console
64
   gem 'byebug', platform: :mri
69
   gem 'byebug', platform: :mri

+ 9 - 0
Gemfile.lock

63
       execjs
63
       execjs
64
     coffee-script-source (1.12.2)
64
     coffee-script-source (1.12.2)
65
     concurrent-ruby (1.0.5)
65
     concurrent-ruby (1.0.5)
66
+    daemons (1.2.4)
67
+    delayed_job (4.1.3)
68
+      activesupport (>= 3.0, < 5.2)
69
+    delayed_job_active_record (4.1.2)
70
+      activerecord (>= 3.0, < 5.2)
71
+      delayed_job (>= 3.0, < 5)
66
     domain_name (0.5.20170404)
72
     domain_name (0.5.20170404)
67
       unf (>= 0.0.5, < 1.0.0)
73
       unf (>= 0.0.5, < 1.0.0)
68
     erubis (2.7.0)
74
     erubis (2.7.0)
208
   byebug
214
   byebug
209
   clipboard-rails
215
   clipboard-rails
210
   coffee-rails (~> 4.2)
216
   coffee-rails (~> 4.2)
217
+  daemons
218
+  delayed_job
219
+  delayed_job_active_record
211
   faker
220
   faker
212
   font-awesome-sass
221
   font-awesome-sass
213
   haml
222
   haml

+ 5 - 0
bin/delayed_job

1
+#!/usr/bin/env ruby
2
+
3
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
4
+require 'delayed/command'
5
+Delayed::Command.new(ARGV).daemonize

+ 2 - 0
config/application.rb

17
 
17
 
18
     config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
18
     config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
19
     config.i18n.default_locale = :nl
19
     config.i18n.default_locale = :nl
20
+
21
+    config.active_job.queue_adapter = :delayed_job
20
   end
22
   end
21
 end
23
 end

+ 22 - 0
db/migrate/20170916212858_create_delayed_jobs.rb

1
+class CreateDelayedJobs < ActiveRecord::Migration[5.0]
2
+  def self.up
3
+    create_table :delayed_jobs, force: true do |table|
4
+      table.integer :priority, default: 0, null: false # Allows some jobs to jump to the front of the queue
5
+      table.integer :attempts, default: 0, null: false # Provides for retries, but still fail eventually.
6
+      table.text :handler,                 null: false # YAML-encoded string of the object that will do work
7
+      table.text :last_error                           # reason for last failure (See Note below)
8
+      table.datetime :run_at                           # When to run. Could be Time.zone.now for immediately, or sometime in the future.
9
+      table.datetime :locked_at                        # Set when a client is working on this object
10
+      table.datetime :failed_at                        # Set when all retries have failed (actually, by default, the record is deleted instead)
11
+      table.string :locked_by                          # Who is working on this object (if locked)
12
+      table.string :queue                              # The name of the queue this job is in
13
+      table.timestamps null: true
14
+    end
15
+
16
+    add_index :delayed_jobs, [:priority, :run_at], name: "delayed_jobs_priority"
17
+  end
18
+
19
+  def self.down
20
+    drop_table :delayed_jobs
21
+  end
22
+end

+ 16 - 1
db/schema.rb

10
 #
10
 #
11
 # It's strongly recommended that you check this file into your version control system.
11
 # It's strongly recommended that you check this file into your version control system.
12
 
12
 
13
-ActiveRecord::Schema.define(version: 20170815155155) do
13
+ActiveRecord::Schema.define(version: 20170916212858) do
14
 
14
 
15
   create_table "activities", force: :cascade do |t|
15
   create_table "activities", force: :cascade do |t|
16
     t.string   "name"
16
     t.string   "name"
25
     t.index ["group_id"], name: "index_activities_on_group_id"
25
     t.index ["group_id"], name: "index_activities_on_group_id"
26
   end
26
   end
27
 
27
 
28
+  create_table "delayed_jobs", force: :cascade do |t|
29
+    t.integer  "priority",   default: 0, null: false
30
+    t.integer  "attempts",   default: 0, null: false
31
+    t.text     "handler",                null: false
32
+    t.text     "last_error"
33
+    t.datetime "run_at"
34
+    t.datetime "locked_at"
35
+    t.datetime "failed_at"
36
+    t.string   "locked_by"
37
+    t.string   "queue"
38
+    t.datetime "created_at"
39
+    t.datetime "updated_at"
40
+    t.index ["priority", "run_at"], name: "delayed_jobs_priority"
41
+  end
42
+
28
   create_table "groups", force: :cascade do |t|
43
   create_table "groups", force: :cascade do |t|
29
     t.string   "name"
44
     t.string   "name"
30
     t.datetime "created_at", null: false
45
     t.datetime "created_at", null: false