]> git.openstreetmap.org Git - rails.git/blob - lib/daemons/gpx_import.rb
More tests, found a bug in the data browser, no fix yet. Fix db readme. Don't show...
[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 = 0 and visible = 1", :order => "id").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         Notifier::deliver_gpx_failure(trace, '0 points parsed ok. Do they all have lat,lng,alt,timestamp?')
27         trace.destroy
28       end
29     rescue Exception => ex
30       logger.info ex.to_s
31       ex.backtrace.each {|l| logger.info l }
32       Notifier::deliver_gpx_failure(trace, ex.to_s + "\n" + ex.backtrace.join("\n"))
33       trace.destroy
34     end
35
36     Signal.trap("TERM", "DEFAULT")
37
38     exit if terminated
39   end
40
41   Trace.find(:all, :conditions => "visible = 0", :order => "id").each do |trace|
42     Signal.trap("TERM") do 
43       terminated = true
44     end
45
46     begin
47       trace.destroy
48     rescue Exception => ex
49       logger.info ex.to_s
50       ex.backtrace.each {|l| logger.info l }
51     end
52
53     Signal.trap("TERM", "DEFAULT")
54
55     exit if terminated
56   end
57
58   sleep 5.minutes.value
59 end