|
1 | 1 | # initializers/okcomputer.rb |
2 | 2 | # Health checks configuration |
| 3 | +require_relative '../../lib/http_client' |
| 4 | +require_relative '../../lib/http_head_check' |
| 5 | +require_relative '../../lib/server_check' |
3 | 6 |
|
4 | 7 | OkComputer.logger = Rails.logger |
5 | 8 | OkComputer.check_in_parallel = true |
|
12 | 15 | core_baseurl = Blacklight.default_index.connection.uri.to_s.chomp('/') |
13 | 16 | OkComputer::Registry.register 'solr', OkComputer::SolrCheck.new(core_baseurl) |
14 | 17 |
|
15 | | -def check_geoserver(server_name) |
16 | | - url = host_url(server_name) |
17 | | - # geoserver_url = url && "#{url.chomp('/')}/wms?service=WMS&request=GetCapabilities" |
18 | | - geoserver_url = 'https://geoservices.lib.berkeley.edu/geoserver/UCB/wms?service=WMS&version=1.1.0&request=GetMap&layers=UCB%3Af7b68n&bbox=-122.836485%2C37.808713%2C-122.409565%2C38.190532&width=768&height=686&srs=EPSG%3A4326&styles=&format=format=image/png' |
19 | | - OkComputer::Registry.register clr_name(server_name), HttpHeadCheck.new(geoserver_url) |
20 | | -end |
| 18 | +SERVER_NAME_GEOSERVER = 'geoserver'.freeze |
| 19 | +SERVER_NAME_SECUREGEOSERVER = 'secure_geoserver'.freeze |
| 20 | +SERVER_NAME_SPATIAL = 'spatial_server'.freeze |
21 | 21 |
|
22 | | -def check_spatial_server(server_name, type) |
23 | | - message = "#{type} #{clr_name(server_name)}" |
24 | | - url = host_url(server_name) |
25 | | - spatial_url = url && "#{url.chomp('/')}/#{type}/berkeley-status/data.zip" |
26 | | - OkComputer::Registry.register message, HttpHeadCheck.new(spatial_url) |
27 | | -end |
28 | | - |
29 | | -def host_url(server_name) |
30 | | - Rails.configuration.x.servers[server_name.to_sym] |
31 | | -end |
32 | | - |
33 | | -def clr_name(server_name) |
34 | | - server_name.gsub('_', ' ') |
35 | | -end |
36 | | -# geoserver_url = geoserver_capabilty_url(Rails.application.config.geoserver_url) |
37 | | -# OkComputer::Registry.register 'Check public geoserver', HttpHeadCheck.new(geoserver_url) |
38 | | -# pubilc_download_url = spatial_download_url(Rails.application.config.spatialserver_url) |
39 | | -# OkComputer::Registry.register 'Check public download', HttpHeadCheck.new(pubilc_download_url) |
40 | | -# secure_download_url = spatial_download_url(Rails.application.config.spatialserver_url, 'UCB') |
41 | | -# OkComputer::Registry.register 'Check public download', HttpHeadCheck.new(secure_download_url) |
42 | | - |
43 | | -class HttpHeadCheck < OkComputer::Check |
44 | | - ConnectionFailed = Class.new(StandardError) |
45 | | - attr_accessor :url |
46 | | - |
47 | | - def initialize(url) |
48 | | - self.url = url |
49 | | - end |
50 | | - |
51 | | - def check |
52 | | - return skip_check unless url |
53 | | - |
54 | | - response = perform_http_head_request |
55 | | - if response.is_a?(Net::HTTPOK) |
56 | | - mark_message 'Check succeeded.' |
57 | | - else |
58 | | - mark_failure |
59 | | - mark_message "Error: Http head check endpoint responded, but returned unexpeced HTTP status: #{response.code} #{response.class}. Expected 200 Net::HTTPOK." |
60 | | - end |
61 | | - rescue StandardError => e |
62 | | - mark_message "Error: '#{e}'" |
63 | | - mark_failure |
64 | | - end |
65 | | - |
66 | | - private |
67 | | - |
68 | | - def perform_http_head_request |
69 | | - uri = URI(url) |
70 | | - Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https', verify_mode: OpenSSL::SSL::VERIFY_PEER) do |http| |
71 | | - http.head(uri.request_uri) |
72 | | - end |
73 | | - rescue StandardError => e |
74 | | - raise ConnectionFailed, e |
75 | | - end |
76 | | - |
77 | | - def skip_check |
78 | | - mark_failure |
79 | | - mark_message 'No URL configured; health check was skipped...' |
80 | | - end |
81 | | -end |
82 | | - |
83 | | -check_geoserver 'geoserver' |
84 | | -# check_geoserver 'secure_geoserver' |
85 | | -# check_spatial_server 'spatial_server', 'public' |
86 | | -# check_spatial_server 'spatial_server', 'UCB' |
| 22 | +ServerCheck.geoserver SERVER_NAME_GEOSERVER |
| 23 | +ServerCheck.geoserver SERVER_NAME_SECUREGEOSERVER |
| 24 | +ServerCheck.spatial_server SERVER_NAME_SPATIAL, 'public' |
| 25 | +ServerCheck.spatial_server SERVER_NAME_SPATIAL, 'UCB' |
0 commit comments