]> git.openstreetmap.org Git - rails.git/blob - lib/daemons/gpx_import.rb
1fc60858a3cebb423fa0aca1f2dc5ebd7e7576f4
[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 $running = true;
9 Signal.trap("TERM") do 
10   $running = false
11 end
12
13 logger = ActiveRecord::Base.logger
14
15 while($running) do
16   
17   ActiveRecord::Base.logger.info("GPX Import daemon wake @ #{Time.now}.")
18
19   traces = Trace.find(:all, :conditions => ['inserted = ?', false])
20
21   if traces and traces.length > 0
22     traces.each do |trace|
23       begin
24
25         logger.info("GPX Import importing #{trace.name} from #{trace.user.email}")
26
27         gzipped = `file -b /tmp/#{trace.id}.gpx`.chomp =~/^gzip/
28
29         if gzipped
30           logger.info("gzipped")
31         else
32           logger.info("not gzipped")
33         end
34         gpx = OSM::GPXImporter.new("/tmp/#{trace.id}.gpx")
35
36         gpx.points do |point|
37           tp = Tracepoint.new
38           tp.latitude = point['latitude']
39           tp.latitude = point['longitude']
40           tp.altitude = point['altitude']
41           tp.user_id = trace.user.id
42           tp.gpx_id = trace.id
43           tp.trackid = point['segment']
44         end 
45         trace.size = gpx.actual_points
46         trace.inserted = true
47         trace.save
48         Notifier::deliver_gpx_success(trace, gpx.possible_points)
49       rescue Exception => ex
50         trace.destroy
51         Notifier::deliver_gpx_failure(trace, ex.to_s)
52       end
53     end
54   end
55   sleep 15.minutes
56 end