Orbit/app/jobs/delayed_impressionist.rb

51 lines
1.2 KiB
Ruby
Raw Permalink Normal View History

2013-07-18 04:03:18 +00:00
class DelayedImpressionist
def self.before_filter(*args)
return
end
include ImpressionistController::InstanceMethods
@queue = :low
def initialize(request,controller_name,action_name,user,obj)
@controller_name = controller_name
@action_name = action_name
@user = User.find user unless user.nil?
@request = request
end
def self.perform(*args)
arg = args[0]
@request= DelayImpressionistRequest.new
@request.restore(arg["request"])
obj = eval("#{arg["class"]}.find '#{arg['obj']['_id']}'")
2013-07-18 04:03:18 +00:00
# imp = new(@request,arg["controller_name"],arg["action_name"],arg["user"],obj)
new_impression = obj.impressions.build(:user_id=>arg["user"],:controller_name=>arg["controller_name"],:action_name=>arg["action_name"],:ip_address=>@request.remote_ip,:referrer=>@request.referer)
2013-08-28 09:56:03 +00:00
@try = 1
loop do
result = new_impression.save rescue false
break if (result || @try >= 10)
@try = @try + 1
puts "trying:#{@try}"
end
2013-07-18 04:03:18 +00:00
new_impression.save
obj.update_attribute(:view_count,obj.impression_count)
end
private
def user
@user
end
def action_name
@action_name
end
def controller_name
@controller_name
end
def request
@request
end
end