开发者

Not able to hit mixpanel in delayed job?

开发者 https://www.devze.com 2023-03-24 14:16 出处:网络
I am using delayed_job to do some background task. In between I want to track some events. I am using mixpanel gem to track the events. In controller its working perfectly fine. But not in Delayed Job

I am using delayed_job to do some background task. In between I want to track some events. I am using mixpanel gem to track the events. In controller its working perfectly fine. But not in Delayed Job.

Code I am using

@original_message = Message.find(message_id)
@mixpanel= Mixpanel::Tracker.new("43242637426346287482", message_id, true)
@mixpanel.track_event("blank_body", {:reset_result => "sucess" })
//message_id is a unique for every request.

I have specified

  gem 'mixpanel' in gemfile

              {undefined method `[]=' for 45:Fixnum

/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/mixpanel-0.9.0/lib/mixpanel/tracker.rb:38:in clear_queue'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/mixpanel-0.9.0/lib/mixpanel/tracker.rb:13:ininitialize'\n/Users/mohit/projects/textadda/lib/message_job.rb:109:in new'\n/Users/mohit/projects/textadda/lib/message_job.rb:109:inperform'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/delayed_job-2.1.4/lib/delayed/backend/base.rb:87:in invoke_job'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/delayed_job-2.1.4/lib/delayed/worker.rb:120:inrun'\n/Users/mohit/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/timeout.rb:67:in timeout'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/delayed_job-2.1.4/lib/delayed/worker.rb:120:inrun'\n/Users/mohit/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/benchmark.rb:308:in realtime'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/delayed_job-2.1.4/lib/delayed/worker.rb:119:inrun'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/delayed_job-2.1.4/lib/delayed/worker.rb:177:in res开发者_开发百科erve_and_run_one_job'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/delayed_job-2.1.4/lib/delayed/worker.rb:104:inwork_off'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/delayed_job-2.1.4/lib/delayed/worker.rb:103:in times'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/delayed_job-2.1.4/lib/delayed/worker.rb:103:inwork_off'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/delayed_job-2.1.4/lib/delayed/worker.rb:78:in start'\n/Users/mohit/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/benchmark.rb:308:inrealtime'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/delayed_job-2.1.4/lib/delayed/worker.rb:77:in start'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/delayed_job-2.1.4/lib/delayed/worker.rb:74:inloop'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/delayed_job-2.1.4/lib/delayed/worker.rb:74:in start'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/delayed_job-2.1.4/lib/delayed/tasks.rb:9\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/lib/rake/task.rb:205:incall'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/lib/rake/task.rb:205:in execute'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/lib/rake/task.rb:200:ineach'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/lib/rake/task.rb:200:in execute'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/lib/rake/task.rb:158:ininvoke_with_call_chain'\n/Users/mohit/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/monitor.rb:242:in synchronize'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/lib/rake/task.rb:151:ininvoke_with_call_chain'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/lib/rake/task.rb:144:in invoke'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/lib/rake/application.rb:112:ininvoke_task'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/lib/rake/application.rb:90:in top_level'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/lib/rake/application.rb:90:ineach'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/lib/rake/application.rb:90:in top_level'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/lib/rake/application.rb:129:instandard_exception_handling'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/lib/rake/application.rb:84:in top_level'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/lib/rake/application.rb:62:inrun'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/lib/rake/application.rb:129:in standard_exception_handling'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/lib/rake/application.rb:59:inrun'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/gems/rake-0.9.2/bin/rake:32\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/bin/rake:19:in `load'\n/Users/mohit/.rvm/gems/ruby-1.8.7-p334/bin/rake:19

EDIT

I have implemented mixpanel in background process using standard get request. But I am still looking for the solution how can I use Mixpanel gem in background process.


You should just be doing something like this:

@mixpanel= Mixpanel::Tracker.new("43242637426346287482", {:REMOTE_ADDR => message_id}, true)
@mixpanel.track_event("blank_body", {:reset_result => "sucess" })

The gem expects the second variable to be the request environment, so will get the ip address that way, and send that to mixpanel.com. But I am not even sure if that is really needed, so I think that even a simple

@mixpanel= Mixpanel::Tracker.new("43242637426346287482", {}, true)
@mixpanel.track_event("blank_body", {:reset_result => "sucess" })

should work.

Hope this helps.


NOTE: THIS ANSWER IS NOW OUT OF DATE AS OF OCT 15 2012 AS THE INITIALIZE METHOD NO LONGER TAKES A ENV PARAMETER

The example on https://github.com/zevarito/mixpanel Mixpanel::Tracker.new gets called like this:

Mixpanel::Tracker.new("YOUR_MIXPANEL_API_TOKEN", request.env, true)

In a controller context, request.env is a hash.

In your code above your passing in message_id as the second argument, which looks like an integer. Sorry, can't help anymore than that, don't know anything about the mixpanel gem, but that's the root of your problem.

If the mixpanel API documentation tells you you can pass an integer as the second parameter, it's incorrect. Here's the code relevant to your error from https://github.com/zevarito/mixpanel/blob/master/lib/mixpanel/tracker.rb

module Mixpanel
  class Tracker
    def initialize(token, env, async = false)
      @token = token
      @env = env
      @async = async
      clear_queue
    end

# snip

    def clear_queue
      @env["mixpanel_events"] = []
    end

# snip

  end
end

Passing an integer as the second argument to the initializer will not work, because the Fixnum class doesn't have a hash assignment ([]=) method, which is exactly the error message you are getting.

If the documentation tells you this can be an integer, you should probably file an issue against mixpanel.

0

精彩评论

暂无评论...
验证码 换一张
取 消