0

I need to do a delayed job to count fbLikes in Model but I have the error report of "undefined send_later() method". Is there any way to do delayed job to my fb_likes function in model?

==============================Latest===================================================

This is my latest code in my project. Things still the same, fb_likes does not display likes count.

[Company.rb]-MODEL

require "delayed_job"
require "count_job.rb"

class Company < ActiveRecord::Base

before_save :fb_likes

    def fb_likes            
        Delayed::Job.enqueue(CountJob.new(self.fbId))
    end     
 end

[config/lib/count_job.rb]

class CountJob<Struct.new(:fbId)
def perform 
    uri = URI("http://graph.facebook.com/#{fbId}")
    data = Net::HTTP.get(uri)
    self.fbLikes = JSON.parse(data)['likes']
end
end

[controller]

 def create 
    @company = Company.new(params[:company])

    if @company.save!
        flash[:success] = "New company successfully registered."
   ----and other more code----
shoujo_sm
  • 3,173
  • 4
  • 37
  • 60
  • You're now describing a different error than you were before. Please add a stack trace for the undefined send_later (nothing should be calling this method) – betamatt Apr 09 '12 at 15:08
  • Your job's perform is also assigning to `self.fbLikes`, which doesn't exist. You can't save any data on the job class and should be storing the like data somewhere useful. – betamatt Apr 09 '12 at 15:09

2 Answers2

3

Library files are not required by default.

  1. Rename the job file to count_job.rb. Using camelCase for filenames is insane and will burn you in unpredictable ways.
  2. Create an initializer and add require 'count_job.rb'
betamatt
  • 500
  • 2
  • 8
  • I rename already but the problem still the same. I had added a latest code in my post. – shoujo_sm Apr 08 '12 at 09:38
  • I spent hours breaking my head over this. I found that delayed job was silently deleting the job from the DB when it wasn't finding the worker class. Adding a require statement in the initializer fixed the issue. Thanks! – Santosh Dec 03 '12 at 17:47
0

One way is to create a separate worker that will get queued, the run to fetch the updated Model and call its fb_likes method on it, but the method will need to be public. Or take the logic into the worker itself.

Bashar Abdullah
  • 1,545
  • 1
  • 16
  • 27
  • can you show me a more solid example? I think I will misunderstood something... thanks.. – shoujo_sm Apr 03 '12 at 23:50
  • Your code seems fine to me... Is the lib directory in your autoload path? config.autoload_paths << File.join(config.root, "lib") – Bashar Abdullah Apr 04 '12 at 06:21
  • I had put the require 'countJob' and it will not output error but my fb_likes does not output the facebook likes(seems the function cannot functions) – shoujo_sm Apr 05 '12 at 02:18
  • Sorry for late reply. You need to tell Rails to include the classes in your lib directory fot autoloading, otherwise Rails will not load these classes: http://stackoverflow.com/questions/3356742/best-way-to-load-module-class-from-lib-folder-in-rails-3 – Bashar Abdullah Apr 05 '12 at 02:58
  • copy config.autoload_paths += Dir["#{config.root}/lib/**/"] into config/application.rb? It still cant work... I also copy config.autoload_paths += %W(#{config.root}/extras) Do I need to bundle install again? – shoujo_sm Apr 05 '12 at 16:50
  • I don't get why it's trying to load Company::CountJob Sorry if this sounds stupid, but are you sure you didn't declare CountJob somewhere else as well? Can you post full classes? – Bashar Abdullah Apr 06 '12 at 19:58
  • I added my latest code... Do my code have mistake somewhere? Thanks for helping me. =) – shoujo_sm Apr 08 '12 at 09:43
  • can you try something like Delayed::Job.enqueue(CountJob.new(self.fbId)) from the console? just to isolate the problem. – Bashar Abdullah Apr 09 '12 at 19:14
  • it report "syntax error near unexpected token `CountJob.new' " – shoujo_sm Apr 11 '12 at 23:54