Add delayed job to post requests to tab
This commit is contained in:
parent
28774b3175
commit
825dee8e02
8 changed files with 80 additions and 3 deletions
7
Gemfile
7
Gemfile
|
@ -94,3 +94,10 @@ gem 'identicon'
|
||||||
|
|
||||||
# Slack
|
# Slack
|
||||||
gem 'tarumi'
|
gem 'tarumi'
|
||||||
|
|
||||||
|
# Run stuff in the background
|
||||||
|
gem 'daemons'
|
||||||
|
gem 'delayed_job', '~> 4.0'
|
||||||
|
gem 'delayed_job_active_record'
|
||||||
|
|
||||||
|
gem 'httparty'
|
||||||
|
|
10
Gemfile.lock
10
Gemfile.lock
|
@ -104,7 +104,13 @@ GEM
|
||||||
simplecov (~> 0.9.1)
|
simplecov (~> 0.9.1)
|
||||||
term-ansicolor (~> 1.3)
|
term-ansicolor (~> 1.3)
|
||||||
thor (~> 0.19.1)
|
thor (~> 0.19.1)
|
||||||
|
daemons (1.2.3)
|
||||||
debugger-linecache (1.2.0)
|
debugger-linecache (1.2.0)
|
||||||
|
delayed_job (4.0.6)
|
||||||
|
activesupport (>= 3.0, < 5.0)
|
||||||
|
delayed_job_active_record (4.0.3)
|
||||||
|
activerecord (>= 3.0, < 5.0)
|
||||||
|
delayed_job (>= 3.0, < 4.1)
|
||||||
devise (3.4.1)
|
devise (3.4.1)
|
||||||
bcrypt (~> 3.0)
|
bcrypt (~> 3.0)
|
||||||
orm_adapter (~> 0.1)
|
orm_adapter (~> 0.1)
|
||||||
|
@ -320,9 +326,13 @@ DEPENDENCIES
|
||||||
codeclimate-test-reporter
|
codeclimate-test-reporter
|
||||||
coffee-rails (~> 4.0.0)
|
coffee-rails (~> 4.0.0)
|
||||||
coveralls
|
coveralls
|
||||||
|
daemons
|
||||||
|
delayed_job (~> 4.0)
|
||||||
|
delayed_job_active_record
|
||||||
devise
|
devise
|
||||||
factory_girl_rails
|
factory_girl_rails
|
||||||
faker (= 1.4.2)
|
faker (= 1.4.2)
|
||||||
|
httparty
|
||||||
identicon
|
identicon
|
||||||
jbuilder (~> 2.0)
|
jbuilder (~> 2.0)
|
||||||
jquery-rails
|
jquery-rails
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
# cancelled :boolean default("f")
|
# cancelled :boolean default("f")
|
||||||
#
|
#
|
||||||
|
|
||||||
|
require 'httparty'
|
||||||
class Order < ActiveRecord::Base
|
class Order < ActiveRecord::Base
|
||||||
include ActionView::Helpers::TextHelper
|
include ActionView::Helpers::TextHelper
|
||||||
|
|
||||||
|
@ -17,6 +18,8 @@ class Order < ActiveRecord::Base
|
||||||
has_many :order_items, dependent: :destroy
|
has_many :order_items, dependent: :destroy
|
||||||
has_many :products, through: :order_items
|
has_many :products, through: :order_items
|
||||||
|
|
||||||
|
after_create :tab_api_created
|
||||||
|
|
||||||
default_scope -> { where(cancelled: false) }
|
default_scope -> { where(cancelled: false) }
|
||||||
|
|
||||||
validates :user, presence: true
|
validates :user, presence: true
|
||||||
|
@ -36,12 +39,25 @@ class Order < ActiveRecord::Base
|
||||||
write_attribute(:price_cents, price_cents)
|
write_attribute(:price_cents, price_cents)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def tab_api_created
|
||||||
|
body = { transaction: { debtor: user.uid, cents: price_cents, message: to_sentence } }
|
||||||
|
headers = { "Authorization" => "Token token=LNJxGqkM39O21gcJJq6BLQ==" }
|
||||||
|
HTTParty.post("https://zeus.ugent.be/tab/transactions", body: body, headers: headers )
|
||||||
|
end
|
||||||
|
handle_asynchronously :tab_api_created
|
||||||
|
|
||||||
|
def tab_api_cancelled
|
||||||
|
puts "api cancelled"
|
||||||
|
end
|
||||||
|
handle_asynchronously :tab_api_cancelled
|
||||||
|
|
||||||
def cancel
|
def cancel
|
||||||
return false if cancelled || created_at < 5.minutes.ago
|
return false if cancelled || created_at < 5.minutes.ago
|
||||||
|
|
||||||
User.decrement_counter(:orders_count, user.id)
|
User.decrement_counter(:orders_count, user.id)
|
||||||
update_attribute(:cancelled, true)
|
update_attribute(:cancelled, true)
|
||||||
self.order_items.each(&:cancel)
|
self.order_items.each(&:cancel)
|
||||||
|
tab_api_cancelled
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -22,9 +22,9 @@
|
||||||
<% if @user.dagschotel %>
|
<% if @user.dagschotel %>
|
||||||
Huidige dagschotel
|
Huidige dagschotel
|
||||||
<%= image_tag @user.dagschotel.avatar %>
|
<%= image_tag @user.dagschotel.avatar %>
|
||||||
<%= link_to "Change dagschotel", user_edit_dagschotel_path(@user), class: "btn btn-default btn-block" %>
|
<%= link_to "Change dagschotel", dit_dagschotel_user_path(@user), class: "btn btn-default btn-block" %>
|
||||||
<% else %>
|
<% else %>
|
||||||
<%= link_to "Set dagschotel", user_edit_dagschotel_path(@user), class: "btn btn-default btn-block" %>
|
<%= link_to "Set dagschotel", edit_dagschotel_user_path(@user), class: "btn btn-default btn-block" %>
|
||||||
<% end %>
|
<% end %>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
5
bin/delayed_job
Executable file
5
bin/delayed_job
Executable file
|
@ -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
|
|
@ -20,5 +20,6 @@ module Tab002
|
||||||
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
||||||
# config.i18n.default_locale = :de
|
# config.i18n.default_locale = :de
|
||||||
config.active_record.raise_in_transactional_callbacks = true
|
config.active_record.raise_in_transactional_callbacks = true
|
||||||
|
config.active_job.queue_adapter = :delayed_job
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
22
db/migrate/20150911195029_create_delayed_jobs.rb
Normal file
22
db/migrate/20150911195029_create_delayed_jobs.rb
Normal file
|
@ -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
db/schema.rb
18
db/schema.rb
|
@ -11,7 +11,23 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 20150827155036) do
|
ActiveRecord::Schema.define(version: 20150911195029) do
|
||||||
|
|
||||||
|
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"
|
||||||
|
end
|
||||||
|
|
||||||
|
add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority"
|
||||||
|
|
||||||
create_table "order_items", force: :cascade do |t|
|
create_table "order_items", force: :cascade do |t|
|
||||||
t.integer "order_id"
|
t.integer "order_id"
|
||||||
|
|
Loading…
Reference in a new issue