Commit b42009a6

mo k <mo@mokhan.ca>
2012-10-07 14:20:45
add delayed_job
1 parent 2cfcf1e
app/controllers/creations_controller.rb
@@ -23,20 +23,19 @@ class CreationsController < ApplicationController
 
   # POST /creations
   def create
-    #@creation = current_user.creations.create(params[:creation])
-    #@creation.category_ids = params[:creation][:category_ids] ||= []
+    @creation = current_user.creations.create(params[:creation])
+    @creation.category_ids = params[:creation][:category_ids] ||= []
 
-    #if @creation.save
-      ##redirect_to( '/creations/crop/' + @creation.id.to_s ) 
-      #Rails.logger.info "end create"
-      #redirect_to(@creation, :notice => 'Creation was successfully cropped.') 
-    #else
-      #flash[:error] = @creation.errors.full_messages
-      #render :action => "new" 
-    #end
+    if @creation.save
+      #redirect_to( '/creations/crop/' + @creation.id.to_s ) 
+      redirect_to(@creation, :notice => 'Creation was successfully cropped.') 
+    else
+      flash[:error] = @creation.errors.full_messages
+      render :action => "new" 
+    end
 
-    @creation = current_user.add_creation(params[:creation])
-    redirect_to(@creation, :notice => 'Creation was successfully cropped.') 
+    #current_user.add_creation(params[:creation])
+    #redirect_to(creations_url) 
   end
 
   # PUT /creations/1
app/models/user.rb
@@ -32,12 +32,12 @@ class User < ActiveRecord::Base
     Comment.create_for(self, creation, comment)
   end
 
-  def add_creation(params)
-    @creation = creations.create(params)
-    @creation.category_ids = params[:category_ids] ||= []
-    @creation.save!
-    @creation
-  end
+  #def add_creation(params)
+    #@creation = creations.create(params)
+    #@creation.category_ids = params[:category_ids] ||= []
+    #@creation.save!
+    #@creation
+  #end
 
   def to_param
     "#{id}-#{name.gsub(/[^a-z0-9]+/i, '-')}"
db/migrate/20121006225021_create_delayed_jobs.rb
@@ -0,0 +1,22 @@
+class CreateDelayedJobs < ActiveRecord::Migration
+  def self.up
+    create_table :delayed_jobs, :force => true do |table|
+      table.integer  :priority, :default => 0      # Allows some jobs to jump to the front of the queue
+      table.integer  :attempts, :default => 0      # Provides for retries, but still fail eventually.
+      table.text     :handler                      # 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
+    end
+
+    add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority'
+  end
+
+  def self.down
+    drop_table :delayed_jobs
+  end
+end
db/schema.rb
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20121002125009) do
+ActiveRecord::Schema.define(:version => 20121006225021) do
 
   create_table "active_admin_comments", :force => true do |t|
     t.integer  "resource_id",   :null => false
@@ -93,6 +93,22 @@ ActiveRecord::Schema.define(:version => 20121002125009) do
     t.integer "category_id"
   end
 
+  create_table "delayed_jobs", :force => true do |t|
+    t.integer  "priority",   :default => 0
+    t.integer  "attempts",   :default => 0
+    t.text     "handler"
+    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",                :null => false
+    t.datetime "updated_at",                :null => false
+  end
+
+  add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority"
+
   create_table "favorites", :force => true do |t|
     t.integer  "user_id"
     t.integer  "creation_id"
script/delayed_job
@@ -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
Gemfile
@@ -15,6 +15,8 @@ gem 'bootstrap-sass'
 gem 'acts_as_commentable_with_threading'
 gem 'acts-as-taggable-on'
 gem 'newrelic_rpm'
+gem 'delayed_job_active_record'
+gem 'daemons'
 
 group :development, :test do
   gem 'webrat'
Gemfile.lock
@@ -87,6 +87,12 @@ GEM
       coffee-script-source
       execjs
     coffee-script-source (1.3.3)
+    daemons (1.1.9)
+    delayed_job (3.0.3)
+      activesupport (~> 3.0)
+    delayed_job_active_record (0.3.3)
+      activerecord (>= 2.1.0, < 4)
+      delayed_job (~> 3.0)
     developwithpassion_arrays (0.0.1)
     devise (2.1.2)
       bcrypt-ruby (~> 3.0)
@@ -287,6 +293,8 @@ DEPENDENCIES
   capybara
   carrierwave
   coffee-rails
+  daemons
+  delayed_job_active_record
   devise
   exception_notification
   factory_girl_rails