diff options
| -rw-r--r-- | Gemfile | 2 | ||||
| -rw-r--r-- | Gemfile.lock | 7 | ||||
| -rw-r--r-- | app/models/import.rb | 39 | ||||
| -rw-r--r-- | config/initializers/apartment.rb | 2 | ||||
| -rw-r--r-- | db/migrate/20120516172252_create_delayed_jobs.rb | 22 | ||||
| -rwxr-xr-x | script/delayed_job | 5 |
6 files changed, 69 insertions, 8 deletions
@@ -35,6 +35,8 @@ gem 'ninoxe', :git => 'git://chouette.dryade.priv/ninoxe' #, :path => '~/Project gem 'acts_as_list', '0.1.6' gem 'composite_primary_keys', '4.1.2' +gem 'delayed_job_active_record' + # Gems used only for assets and not required # in production environments by default. group :assets do diff --git a/Gemfile.lock b/Gemfile.lock index b6b129246..9c79ce4e0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -121,6 +121,11 @@ GEM composite_primary_keys (4.1.2) activerecord (~> 3.1) database_cleaner (0.7.2) + delayed_job (3.0.2) + activesupport (~> 3.0) + delayed_job_active_record (0.3.2) + activerecord (> 2.1.0) + delayed_job (~> 3.0.0) devise (2.0.4) bcrypt-ruby (~> 3.0) orm_adapter (~> 0.0.3) @@ -161,6 +166,7 @@ GEM jruby-openssl (0.7.6.1) bouncy-castle-java (>= 1.5.0146.1) jruby-rack (1.1.5) + json (1.7.3) json (1.7.3-java) launchy (2.1.0) addressable (~> 2.2.6) @@ -311,6 +317,7 @@ DEPENDENCIES coffee-script-source composite_primary_keys (= 4.1.2) database_cleaner + delayed_job_active_record devise factory_girl_rails (= 1.7) formtastic (= 2.0.2) diff --git a/app/models/import.rb b/app/models/import.rb index a2a7e9afa..ec6caa660 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -26,19 +26,44 @@ class Import < ActiveRecord::Base self.status ||= "pending" end - after_create :import + after_create :delayed_import + def delayed_import + save_resources + delay.import + end + + @@root = "#{Rails.root}/tmp/imports" + cattr_accessor :root + + def save_resources + FileUtils.mkdir_p root + FileUtils.cp resources.path, saved_resources + end + + after_destroy :destroy_resources + def destroy_resources + FileUtils.rm saved_resources if File.exists? saved_resources + end + + def saved_resources + "#{root}/#{id}.zip" + end + def import begin - with_original_filename do |file| - # chouette-command checks the file extension (and requires .zip) :( - loader.import file + if resources + with_original_filename do |file| + # chouette-command checks the file extension (and requires .zip) :( + loader.import file + end + else + loader.import saved_resources end update_attribute :status, "completed" - rescue + rescue => e + Rails.logger.error "Import #{id} failed : #{e}, #{e.backtrace}" update_attribute :status, "failed" end end - - end diff --git a/config/initializers/apartment.rb b/config/initializers/apartment.rb index eaecaeac5..be6b7e933 100644 --- a/config/initializers/apartment.rb +++ b/config/initializers/apartment.rb @@ -1,6 +1,6 @@ Apartment.configure do |config| # set your options (described below) here - config.excluded_models = ["Referential", "User", "Import"] # these models will not be multi-tenanted, but remain in the global (public) namespace + config.excluded_models = ["Referential", "User", "Import", "Delayed::Backend::ActiveRecord::Job"] # these models will not be multi-tenanted, but remain in the global (public) namespace # Dynamically get database names to migrate config.database_names = lambda{ Referential.select(:slug).map(&:slug) } diff --git a/db/migrate/20120516172252_create_delayed_jobs.rb b/db/migrate/20120516172252_create_delayed_jobs.rb new file mode 100644 index 000000000..e7841608c --- /dev/null +++ b/db/migrate/20120516172252_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 diff --git a/script/delayed_job b/script/delayed_job new file mode 100755 index 000000000..edf195985 --- /dev/null +++ b/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 |
