]> git.openstreetmap.org Git - rails.git/blob - lib/daemons/gpx_import.rb
ba76d60f0bcb90142ea1df0512d92028ba3b6b50
[rails.git] / lib / daemons / gpx_import.rb
1 #!/usr/bin/env ruby
2
3 #You might want to change this
4 #ENV["RAILS_ENV"] ||= "development"
5
6 require File.dirname(__FILE__) + "/../../config/environment"
7
8 terminated = false
9
10 logger = ActiveRecord::Base.logger
11
12 while(true) do
13   ActiveRecord::Base.logger.info("GPX Import daemon wake @ #{Time.now}.")
14
15   Trace.find(:all, :conditions => ['inserted = ?', false]).each do |trace|
16     Signal.trap("TERM") do 
17       terminated = true
18     end
19
20     begin
21       gpx = trace.import
22
23       if gpx.actual_points > 0
24         Notifier::deliver_gpx_success(trace, gpx.actual_points)
25       else
26         trace.destroy
27         Notifier::deliver_gpx_failure(trace, '0 points parsed ok. Do they all have lat,lng,alt,timestamp?')
28       end
29     rescue Exception => ex
30       logger.info ex.to_s
31       ex.backtrace.each {|l| logger.info l }
32       trace.destroy
33       Notifier::deliver_gpx_failure(trace, ex.to_s + "\n" + ex.backtrace.join("\n"))
34     end
35
36     Signal.trap("TERM", "DEFAULT")
37
38     exit if terminated
39   end
40
41   sleep 5.minutes
42 end