Skip to content

Commit

Permalink
Delayed Job
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonah Hirsch committed Jul 21, 2017
1 parent 3a52970 commit e7c140e
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 5 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ gem 'trueskill', github: 'saulabs/trueskill', require: 'saulabs/trueskill'
gem 'slack-ruby-client'
gem 'terminal-table'
gem 'rails-assets-spectrum', source: 'https://rails-assets.org'
gem 'delayed_job_active_record'

group :production do
gem 'unicorn'
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ GEM
coolline (0.5.0)
unicode_utils (~> 1.4)
debug_inspector (0.0.2)
delayed_job (4.1.3)
activesupport (>= 3.0, < 5.2)
delayed_job_active_record (4.1.2)
activerecord (>= 3.0, < 5.2)
delayed_job (>= 3.0, < 5)
diff-lcs (1.3)
docile (1.1.5)
dotenv (2.2.1)
Expand Down Expand Up @@ -253,6 +258,7 @@ DEPENDENCIES
chartkick
codeclimate-test-reporter
coffee-rails
delayed_job_active_record
dotenv-rails
dynamic_form
elo
Expand Down
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
worker: rake jobs:work
8 changes: 4 additions & 4 deletions app/controllers/slack/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ def action
payload = JSON.parse(params[:payload]).with_indifferent_access
case payload[:callback_id]
when 'record_result'
render text: 'Recording Result...'
Thread.new { Slack::Result.create(payload) }
RecordResultJob.perform_later payload
render plain: 'Recording Result...'
when 'leaderboard'
render text: 'Loading Leaderboard...'
Thread.new { Slack::Leaderboard.show(payload) }
LeaderboardJob.perform_later(payload)
render plain: 'Loading Leaderboard...'
end
end

Expand Down
7 changes: 7 additions & 0 deletions app/jobs/application_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ApplicationJob < ActiveJob::Base
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked

# Most jobs are safe to ignore if the underlying records are no longer available
# discard_on ActiveJob::DeserializationError
end
7 changes: 7 additions & 0 deletions app/jobs/leaderboard_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class LeaderboardJob < ApplicationJob
queue_as :default

def perform(payload)
Slack::Leaderboard.show(payload)
end
end
7 changes: 7 additions & 0 deletions app/jobs/record_result_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class RecordResultJob < ApplicationJob
queue_as :default

def perform(payload)
Slack::Result.create(payload)
end
end
5 changes: 5 additions & 0 deletions bin/delayed_job
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
require 'delayed/command'
Delayed::Command.new(ARGV).daemonize
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ class Application < Rails::Application
[u, p] == [ENV['BASIC_AUTH_USER'], ENV['BASIC_AUTH_PASSWORD']]
end
end

config.active_job.queue_adapter = :delayed_job
end
end
22 changes: 22 additions & 0 deletions db/migrate/20170721213838_create_delayed_jobs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class CreateDelayedJobs < ActiveRecord::Migration[5.1]
def self.up
create_table :delayed_jobs, force: true do |table|
table.integer :priority, default: 0, null: false # Allows some jobs to jump to the front of the queue
table.integer :attempts, default: 0, null: false # Provides for retries, but still fail eventually.
table.text :handler, null: false # YAML-encoded string of the object that will do work
table.text :last_error # reason for last failure (See Note below)
table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
table.datetime :locked_at # Set when a client is working on this object
table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
table.string :locked_by # Who is working on this object (if locked)
table.string :queue # The name of the queue this job is in
table.timestamps null: true
end

add_index :delayed_jobs, [:priority, :run_at], name: "delayed_jobs_priority"
end

def self.down
drop_table :delayed_jobs
end
end
17 changes: 16 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,26 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170504163956) do
ActiveRecord::Schema.define(version: 20170721213838) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "delayed_jobs", force: :cascade do |t|
t.integer "priority", default: 0, null: false
t.integer "attempts", default: 0, null: false
t.text "handler", null: false
t.text "last_error"
t.datetime "run_at"
t.datetime "locked_at"
t.datetime "failed_at"
t.string "locked_by"
t.string "queue"
t.datetime "created_at"
t.datetime "updated_at"
t.index ["priority", "run_at"], name: "delayed_jobs_priority"
end

create_table "games", id: :serial, force: :cascade do |t|
t.string "name", limit: 255, null: false
t.datetime "created_at"
Expand Down

0 comments on commit e7c140e

Please sign in to comment.