]> git.openstreetmap.org Git - rails.git/blob - lib/daemons/gpx_import.rb
Primary keys can't have names so don't pretend otherwise.
[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
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