Guest User

Untitled

a guest
Jun 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. #!/usr/bin/ruby
  2. # MapServer manages jobs and provides a service with DRB.
  3.  
  4. require 'rubygems'
  5. require 'drb'
  6. require 'fileutils'
  7. require 'coordinates'
  8. require 'methodchain'
  9.  
  10. ($: << File.expand_path(File.dirname __FILE__)).uniq!
  11. require 'map_request.rb'
  12. require 'wms.rb'
  13.  
  14. class DrMap
  15. attr_reader :config, :servers
  16.  
  17. def initialize
  18. config_file = File.join(File.dirname(__FILE__), '..', 'config', 'service.yaml')
  19. raise 'config/service.yaml not found' unless File.exists? config_file
  20. @config = YAML.load_file(config_file)
  21. @servers = @config[:servers].collect { |x| WMS.new x }
  22. end
  23.  
  24. def pretend params; exception_filter {
  25. req = MapRequest.new(params).yaml
  26. unobjectify(req)
  27. } end
  28.  
  29. def create params; exception_filter {
  30. req = MapRequest.new(params)
  31. req.yaml[:wms] = wms(req.yaml[:layers])
  32. req.save
  33.  
  34. puts "created job #{req.job_id} from #{params.inspect}"
  35. return req.job_id
  36. } end
  37.  
  38. def submit params; exception_filter {
  39. job_id = self.create(params)
  40. self.run(job_id)
  41. return job_id
  42. } end
  43.  
  44. def run job_id, phase=nil; exception_filter {
  45. MapRequest.new(job_id).run
  46. puts "running job #{job_id}"
  47. } end
  48.  
  49. def stats job_id; exception_filter {
  50. unobjectify(MapRequest.new(job_id).yaml)
  51. } end
  52.  
  53. def jobs; exception_filter {
  54. # all of the subdirectories in the job directory
  55. Dir.entries(@config[:job][:path]).select do |dir|
  56. File.directory? dir and not %w{ . .. }.member? dir
  57. end
  58. } end
  59.  
  60. # Return the uri of the final image for a job id
  61. def uri job_id; exception_filter {
  62. req = MapRequest.new(job_id)
  63. file = "output.#{req.yaml[:ext]}"
  64. [ @config[:job][:uri], job_id, file ].join('/')
  65. } end
  66.  
  67. # Get a list of layers which are available on the attached web map services
  68. def layers
  69. @servers . collect { |x| x.layers } . flatten . uniq
  70. end
  71.  
  72. def finished? job_id; exception_filter {
  73. MapRequest.new(job_id).finished?
  74. } end
  75.  
  76. # pass through the Process::Control methods
  77.  
  78. def continue job_id; exception_filter {
  79. MapRequest.new(job_id).continue
  80. } end
  81.  
  82. def stop job_id; exception_filter {
  83. MapRequest.new(job_id).stop
  84. } end
  85.  
  86. def stopped? job_id; exception_filter {
  87. MapRequest.new(job_id).stopped?
  88. } end
  89.  
  90. def alive? job_id; exception_filter {
  91. MapRequest.new(job_id).alive?
  92. } end
  93.  
  94. # kill the process if it's alive and remove its directory
  95. def remove job_id; exception_filter {
  96. MapRequest.new(job_id).remove
  97. } end
  98.  
  99. private
  100.  
  101. # Pick out the first wms server that supports all of the requested layers.
  102. def wms layers
  103. server = @servers.detect { |server| server.supports? layers }
  104. raise 'No WMS has all the layers requested' if server.nil?
  105. return server
  106. end
  107.  
  108. def exception_filter
  109. begin
  110. Dir.chdir(@config[:job][:path]) { yield }
  111. rescue
  112. msg = $!.message + "\n" + $!.backtrace.join("\n") + "\n"
  113. puts msg # print the messages so they show up in the log
  114. raise msg # and re-raise as a string for the remote client
  115. end
  116. end
  117.  
  118. def unobjectify object
  119. req = object.clone
  120. unless req[:wms].nil?
  121. req[:wms] = {
  122. :name => req[:wms].name,
  123. :layers => req[:wms].layers,
  124. :uri => req[:wms].uri.to_s,
  125. }
  126. end
  127. req
  128. end
  129. end
  130.  
  131. if $0 == __FILE__ then
  132. STDOUT.sync = true
  133. STDERR.sync = true
  134. drmap = DrMap.new
  135. DRb.start_service drmap.config[:resource], drmap
  136. DRb.thread.join
  137. end
Add Comment
Please, Sign In to add comment