Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- # MapServer manages jobs and provides a service with DRB.
- require 'rubygems'
- require 'drb'
- require 'fileutils'
- require 'coordinates'
- require 'methodchain'
- ($: << File.expand_path(File.dirname __FILE__)).uniq!
- require 'map_request.rb'
- require 'wms.rb'
- class DrMap
- attr_reader :config, :servers
- def initialize
- config_file = File.join(File.dirname(__FILE__), '..', 'config', 'service.yaml')
- raise 'config/service.yaml not found' unless File.exists? config_file
- @config = YAML.load_file(config_file)
- @servers = @config[:servers].collect { |x| WMS.new x }
- end
- def pretend params; exception_filter {
- req = MapRequest.new(params).yaml
- unobjectify(req)
- } end
- def create params; exception_filter {
- req = MapRequest.new(params)
- req.yaml[:wms] = wms(req.yaml[:layers])
- req.save
- puts "created job #{req.job_id} from #{params.inspect}"
- return req.job_id
- } end
- def submit params; exception_filter {
- job_id = self.create(params)
- self.run(job_id)
- return job_id
- } end
- def run job_id, phase=nil; exception_filter {
- MapRequest.new(job_id).run
- puts "running job #{job_id}"
- } end
- def stats job_id; exception_filter {
- unobjectify(MapRequest.new(job_id).yaml)
- } end
- def jobs; exception_filter {
- # all of the subdirectories in the job directory
- Dir.entries(@config[:job][:path]).select do |dir|
- File.directory? dir and not %w{ . .. }.member? dir
- end
- } end
- # Return the uri of the final image for a job id
- def uri job_id; exception_filter {
- req = MapRequest.new(job_id)
- file = "output.#{req.yaml[:ext]}"
- [ @config[:job][:uri], job_id, file ].join('/')
- } end
- # Get a list of layers which are available on the attached web map services
- def layers
- @servers . collect { |x| x.layers } . flatten . uniq
- end
- def finished? job_id; exception_filter {
- MapRequest.new(job_id).finished?
- } end
- # pass through the Process::Control methods
- def continue job_id; exception_filter {
- MapRequest.new(job_id).continue
- } end
- def stop job_id; exception_filter {
- MapRequest.new(job_id).stop
- } end
- def stopped? job_id; exception_filter {
- MapRequest.new(job_id).stopped?
- } end
- def alive? job_id; exception_filter {
- MapRequest.new(job_id).alive?
- } end
- # kill the process if it's alive and remove its directory
- def remove job_id; exception_filter {
- MapRequest.new(job_id).remove
- } end
- private
- # Pick out the first wms server that supports all of the requested layers.
- def wms layers
- server = @servers.detect { |server| server.supports? layers }
- raise 'No WMS has all the layers requested' if server.nil?
- return server
- end
- def exception_filter
- begin
- Dir.chdir(@config[:job][:path]) { yield }
- rescue
- msg = $!.message + "\n" + $!.backtrace.join("\n") + "\n"
- puts msg # print the messages so they show up in the log
- raise msg # and re-raise as a string for the remote client
- end
- end
- def unobjectify object
- req = object.clone
- unless req[:wms].nil?
- req[:wms] = {
- :name => req[:wms].name,
- :layers => req[:wms].layers,
- :uri => req[:wms].uri.to_s,
- }
- end
- req
- end
- end
- if $0 == __FILE__ then
- STDOUT.sync = true
- STDERR.sync = true
- drmap = DrMap.new
- DRb.start_service drmap.config[:resource], drmap
- DRb.thread.join
- end
Add Comment
Please, Sign In to add comment