0

I am having the following code

def test
  t1 = Time.now
  @lat = params[:lat]
  @lng = params[:lng]
  @vehicleid = params[:vehicle_id]
  @address = GoogleGeocoder.reverse_geocode([@lat, @lng]).full_address

  speed = params[:speed]
  speed ||= 0
  Tracking.create(:latitude => @lat, :longitude => @lng, :address => @address, :vehicle_id => @vehicleid, :speed => speed)


  uid = Vehicle.select('user_id').where('vehicle_id=?', @vehicleid)
  gid = VehicleGeozone.where('vehicle_id=?', @vehicleid)
  email = User.select('email').where('user_id=?', uid[0].user_id)
  gzone = Geozone.where('geozone_id=?', gid[0].geozone_id)

  @geofence = Geofence.where('geozone_id = ?', gzone[0].geozone_id)
  if gzone[0].zonetype == 'polygon'
    infence = false
    res = isPointInPoly(@lat.to_f, @lng.to_f, @geofence)
  else
    res = isPointInCircle(@lat.to_f, @lng.to_f, @geofence[0].latitude, @geofence[0].longitude, @geofence[0].radius)
  end

  if res == true
    AlertMailer.geofence(email[0].email).deliver
  end
  t2 = Time.now
  render :text => '{"message":"success"}'
end

This test function is called every 5 seconds.

From t1 and t2 found that processing this function requires 7 seconds.

Is there a way to use threading so that I can move some intensive code of isPointInCircle and isPointInPoly into separate threads?

Andrew Hubbs
  • 9,338
  • 9
  • 48
  • 71
Mohammad Sadiq Shaikh
  • 3,160
  • 9
  • 35
  • 54
  • don't do this. You will have to handle the threads and if you get a lot request, you will get in real trouble. Try to move some of the tasks in a background job. – Fa11enAngel Jan 06 '13 at 20:32
  • Fa11enAngel Can you suggest me some some libraries or gems for background process suitable to above code.... – Mohammad Sadiq Shaikh Jan 07 '13 at 06:34
  • Is it a some kind of service you call every 5 seconds, to send someone an email? Try to find out, what is slow about it. If you move the job in background, it won't run faster. It just works in background. Do you need to call it every 5 seconds. Maybe calling it every 10 or 20 seconds is enough? – Fa11enAngel Jan 07 '13 at 10:16
  • I may run this in background.but I want to to check condition for each call to test function to check whether the point is out of fence or not... slow is the functions – Mohammad Sadiq Shaikh Jan 07 '13 at 11:05
  • Please suggest some method to accomplish this task..... – Mohammad Sadiq Shaikh Jan 07 '13 at 11:05
  • The functions should be really fast as described here: http://stackoverflow.com/questions/481144/equation-for-testing-if-a-point-is-inside-a-circle – Fa11enAngel Jan 07 '13 at 14:57
  • What is exactly slow. Do you have so much data to check. If this is the case, you have to find a way to reduce the data you have to process on every cache. Maybe separate the data - which you really need to process. – Fa11enAngel Jan 09 '13 at 15:35

0 Answers0