Question

I has been wanted to do the Delayed::Job which to do the fbLikes ( I post a lot on StackOverFlow but still haven't solve the problem yet) My table n database have name, id, fbLikes, fbId, url. Here is my steps for the program. [Home Page]company list -> Create a Company[Insert A company infos] ->Save fbId, name, id, url BUT NOT FBLIKES -> redirect to HomePage [after_save Update the fbLikes for the previous added company]

I not sure whether my delayed job is working or not because my fbLikes in my model is still blank and not updated with latest fbLikes.I not sure is there a better way to do this. For "rake jobs:work" there is no background work display in the console.

[MODEL company.rb]

require "delayed_job"
require "count_job.rb"
after_save :fb_likes
def fb_likes    
    Delayed::Job.enqueue(CountJob.new(self.id))
end

[lib/count_job.rb]

require 'net/http'
require 'company.rb'

class CountJob < Struct.new(:id)        
  def perform
    @company = Company.find(id)
    uri = URI("http://graph.facebook.com/#{@company.fbId}")
    data = Net::HTTP.get(uri)
    @company.fbLikes= JSON.parse(data)['likes']
    @company.save!
  end
end
Was it helpful?

Solution

[MODEL company.rb]

require "delayed_job"
require "count_job.rb"
after_save :fb_likes
def fb_likes    
    Delayed::Job.enqueue(CountJob.new(self.id))
end

[lib/count_job.rb]

require 'net/http'
require 'company.rb'

class CountJob < Struct.new(:id)        
  def perform
    @company = Company.find(id)
    uri = URI("http://graph.facebook.com/#{@company.fbId}")
    data = Net::HTTP.get(uri)
    @company.fbLikes= JSON.parse(data)['likes']
     @company.save!
   end
 end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top