1
0
Fork 0

Added delayed jobs

This commit is contained in:
magnolia-fan 2011-06-20 01:23:22 +04:00
parent ec90fe57f2
commit a7b36e6d0e
10 changed files with 81 additions and 33 deletions

26
Gemfile
View File

@ -2,32 +2,8 @@ source 'http://rubygems.org'
gem 'rails', '3.0.8' gem 'rails', '3.0.8'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3' gem 'sqlite3'
gem 'pg' gem 'pg'
gem 'awesome_print', :require => 'ap' gem 'awesome_print', :require => 'ap'
# Use unicorn as the web server gem 'delayed_job'
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
# gem 'ruby-debug'
# gem 'ruby-debug19', :require => 'ruby-debug'
# Bundle the extra gems:
# gem 'bj'
gem 'nokogiri'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3'
# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
# group :development, :test do
# gem 'webrat'
# end

View File

@ -31,6 +31,10 @@ GEM
arel (2.0.10) arel (2.0.10)
awesome_print (0.4.0) awesome_print (0.4.0)
builder (2.1.2) builder (2.1.2)
daemons (1.1.4)
delayed_job (2.1.4)
activesupport (~> 3.0)
daemons
erubis (2.6.6) erubis (2.6.6)
abstract (>= 1.0.0) abstract (>= 1.0.0)
i18n (0.5.0) i18n (0.5.0)
@ -40,7 +44,6 @@ GEM
mime-types (~> 1.16) mime-types (~> 1.16)
treetop (~> 1.4.8) treetop (~> 1.4.8)
mime-types (1.16) mime-types (1.16)
nokogiri (1.4.5)
pg (0.11.0) pg (0.11.0)
polyglot (0.3.1) polyglot (0.3.1)
rack (1.2.3) rack (1.2.3)
@ -73,7 +76,7 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
awesome_print awesome_print
nokogiri delayed_job
pg pg
rails (= 3.0.8) rails (= 3.0.8)
sqlite3 sqlite3

View File

@ -5,14 +5,23 @@ class ArtistController < ApplicationController
data = {} data = {}
name = params[:name].gsub('%20', ' ').gsub('+', ' ') name = params[:name].gsub('%20', ' ').gsub('+', ' ')
artist = Artist.find_by_name(name) artist = Artist.find_by_name(name)
if artist and artist.status == 0
render :json => {status: 'loading'}
return
end
unless artist unless artist
results = ArtistController.musicBrainzExactSearch(name) results = ArtistController.musicBrainzExactSearch(name)
if results.empty? if results.empty?
render :json => {status: 'not founds'} render :json => {status: 'not found'}
return return
elsif results[0][:name] == name elsif results[0][:name] == name
ImportController.importArtist(name) # Saving artist and queueing job
render :json => {status: 'loaded'} artist = Artist.new
artist.name = name
artist.status = 0
artist.save
Delayed::Job.enqueue LoadArtistJob.new(name)
render :json => {status: 'loading_started'}
return return
elsif results[0][:name].downcase == name.downcase or results[0][:name].downcase == 'the '+ name.downcase elsif results[0][:name].downcase == name.downcase or results[0][:name].downcase == 'the '+ name.downcase
render :json => {status: 'corrected', page: results[0][:name]} render :json => {status: 'corrected', page: results[0][:name]}

View File

@ -19,8 +19,8 @@ class ImportController < ApplicationController
end end
# Save artist # Save artist
artist = Artist.new artist = Artist.find_by_name(name)
artist.name = artist_mb_data[:name] #artist.name = artist_mb_data[:name]
artist.desc = artist_desc artist.desc = artist_desc
artist.pic_url = artist_pic artist.pic_url = artist_pic
artist.artist_type = artist_mb_data[:type] artist.artist_type = artist_mb_data[:type]
@ -133,6 +133,9 @@ class ImportController < ApplicationController
end # mb_albums.each do |mb_album| end # mb_albums.each do |mb_album|
artist.status = 1
artist.save
end # def self.importArtist name end # def self.importArtist name
end end

View File

@ -0,0 +1 @@
require 'load_artist_job'

View File

@ -0,0 +1,21 @@
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.timestamps
end
add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority'
end
def self.down
drop_table :delayed_jobs
end
end

View File

@ -0,0 +1,9 @@
class AddStatusToArtists < ActiveRecord::Migration
def self.up
add_column :artists, :status, :integer
end
def self.down
remove_column :artists, :status
end
end

View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20110617213912) do ActiveRecord::Schema.define(:version => 20110618191349) do
create_table "albums", :force => true do |t| create_table "albums", :force => true do |t|
t.string "name" t.string "name"
@ -31,8 +31,24 @@ ActiveRecord::Schema.define(:version => 20110617213912) do
t.string "pic_url" t.string "pic_url"
t.string "artist_type" t.string "artist_type"
t.string "mbid" t.string "mbid"
t.integer "status"
end 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.datetime "created_at"
t.datetime "updated_at"
end
add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority"
create_table "tracks", :force => true do |t| create_table "tracks", :force => true do |t|
t.string "name" t.string "name"
t.integer "album_id" t.integer "album_id"

5
lib/load_artist_job.rb Normal file
View File

@ -0,0 +1,5 @@
class LoadArtistJob < Struct.new(:name)
def perform
ImportController.importArtist(name)
end
end

5
script/delayed_job Executable file
View 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