Skip to content

Commit

Permalink
Adds delayed jobs for long task process
Browse files Browse the repository at this point in the history
  • Loading branch information
harshs08 committed Dec 27, 2015
1 parent bbdcb59 commit c7e4d4c
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ gem 'bcrypt', '~> 3.1.7'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
gem 'kaminari'
gem 'delayed_job_active_record', '~> 4.1'

# Use debugger
gem 'byebug', group: [:development, :test]
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ GEM
compass-rails (1.1.7)
compass (>= 0.12.2)
sprockets (<= 2.11.0)
delayed_job (4.1.1)
activesupport (>= 3.0, < 5.0)
delayed_job_active_record (4.1.0)
activerecord (>= 3.0, < 5)
delayed_job (>= 3.0, < 5)
devise (3.5.2)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
Expand Down Expand Up @@ -189,6 +194,7 @@ DEPENDENCIES
byebug
coffee-rails (~> 4.0.0)
compass-rails (= 1.1.7)
delayed_job_active_record (~> 4.1)
devise
email_spec
factory_girl_rails
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v1/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def create
params[:order][:product_ids_and_quantities])

if order.save
order.reload
OrderMailer.send_confirmation(order).deliver
order.reload #we reload the object so the response displays the product objects
OrderMailer.delay.send_confirmation(order)
render json: order, status: 201, location: [:api, current_user, order]
else
render json: { errors: order.errors }, status: 422
Expand Down
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
7 changes: 6 additions & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
config.action_controller.perform_caching = false

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
config.action_mailer.raise_delivery_errors = true

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
Expand All @@ -26,4 +26,9 @@
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
config.action_mailer.default_url_options = { :host => "localhost",
only_path: false }
end
22 changes: 22 additions & 0 deletions db/migrate/20151227015440_create_delayed_jobs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class CreateDelayedJobs < ActiveRecord::Migration
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
18 changes: 17 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,23 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20151226042740) do
ActiveRecord::Schema.define(version: 20151227015440) do

create_table "delayed_jobs", force: true 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"
end

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

create_table "orders", force: true do |t|
t.integer "user_id"
Expand Down

0 comments on commit c7e4d4c

Please sign in to comment.