procon_bypass_man 0.2.0 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +19 -6
  3. data/.github/workflows/gitleacks.yml +2 -1
  4. data/.github/workflows/release.yml +1 -0
  5. data/.github/workflows/ruby.yml +1 -1
  6. data/.rubocop.yml +4 -0
  7. data/.ruby-version +1 -1
  8. data/CHANGELOG.md +16 -1
  9. data/Gemfile +6 -3
  10. data/Gemfile.lock +3 -3
  11. data/README.md +17 -8
  12. data/Steepfile +0 -1
  13. data/bin/generate_default_app +10 -0
  14. data/docs/getting_started.md +98 -25
  15. data/docs/setup_raspi_by_mitamae.md +44 -2
  16. data/lib/procon_bypass_man/background/job_performer.rb +1 -2
  17. data/lib/procon_bypass_man/background/job_queue.rb +50 -0
  18. data/lib/procon_bypass_man/background/jobs/base_job.rb +1 -1
  19. data/lib/procon_bypass_man/background/jobs/concerns/has_external_api_setting.rb +2 -2
  20. data/lib/procon_bypass_man/background/jobs/concerns/job_performable.rb +16 -0
  21. data/lib/procon_bypass_man/background/jobs/post_completed_remote_macro_job.rb +1 -1
  22. data/lib/procon_bypass_man/background/jobs/report_boot_job.rb +1 -1
  23. data/lib/procon_bypass_man/background/jobs/report_completed_upgrade_pbm_job.rb +1 -1
  24. data/lib/procon_bypass_man/background/jobs/report_error_job.rb +1 -1
  25. data/lib/procon_bypass_man/background/jobs/report_error_reload_config_job.rb +1 -1
  26. data/lib/procon_bypass_man/background/jobs/report_load_config_job.rb +1 -1
  27. data/lib/procon_bypass_man/background/jobs/report_procon_performance_measurements_job.rb +43 -0
  28. data/lib/procon_bypass_man/background/jobs/report_reload_config_job.rb +1 -1
  29. data/lib/procon_bypass_man/background/jobs/report_start_reboot_job.rb +1 -1
  30. data/lib/procon_bypass_man/background/jobs/sync_device_stats_job.rb +1 -1
  31. data/lib/procon_bypass_man/background.rb +3 -4
  32. data/lib/procon_bypass_man/bypass/bypass_command.rb +6 -15
  33. data/lib/procon_bypass_man/bypass/bypass_value.rb +6 -0
  34. data/lib/procon_bypass_man/bypass/procon_to_switch.rb +107 -0
  35. data/lib/procon_bypass_man/bypass/switch_to_procon.rb +64 -0
  36. data/lib/procon_bypass_man/bypass.rb +5 -112
  37. data/lib/procon_bypass_man/configuration.rb +16 -40
  38. data/lib/procon_bypass_man/device_connection/output_report_sub_command_table.rb +1 -1
  39. data/lib/procon_bypass_man/device_connection/procon_setting_overrider.rb +12 -3
  40. data/lib/procon_bypass_man/procon/macro.rb +1 -1
  41. data/lib/procon_bypass_man/procon/performance_measurement/last_bypass_at.rb +17 -0
  42. data/lib/procon_bypass_man/procon/performance_measurement/measurement_collection.rb +9 -0
  43. data/lib/procon_bypass_man/procon/performance_measurement/measurements_summarizer.rb +84 -0
  44. data/lib/procon_bypass_man/procon/performance_measurement/procon_performance_span_transfer_job.rb +8 -0
  45. data/lib/procon_bypass_man/procon/performance_measurement/queue_over_process.rb +38 -0
  46. data/lib/procon_bypass_man/procon/performance_measurement/span_queue.rb +42 -0
  47. data/lib/procon_bypass_man/procon/performance_measurement/span_transfer_buffer.rb +39 -0
  48. data/lib/procon_bypass_man/procon/performance_measurement.rb +103 -0
  49. data/lib/procon_bypass_man/procon.rb +2 -1
  50. data/lib/procon_bypass_man/procon_display/bypass_hook.rb +12 -0
  51. data/lib/procon_bypass_man/procon_display/http_request.rb +31 -0
  52. data/lib/procon_bypass_man/procon_display/http_response.rb +23 -0
  53. data/lib/procon_bypass_man/procon_display/server.rb +33 -0
  54. data/lib/procon_bypass_man/procon_display/server_app.rb +17 -0
  55. data/lib/procon_bypass_man/procon_display/status.rb +20 -0
  56. data/lib/procon_bypass_man/procon_display.rb +11 -0
  57. data/lib/procon_bypass_man/remote_macro/queue_over_process.rb +26 -44
  58. data/lib/procon_bypass_man/remote_macro/remote_macro_object.rb +22 -24
  59. data/lib/procon_bypass_man/remote_macro/remote_macro_receiver.rb +1 -1
  60. data/lib/procon_bypass_man/remote_macro/remote_macro_sender.rb +1 -1
  61. data/lib/procon_bypass_man/remote_macro/task.rb +1 -5
  62. data/lib/procon_bypass_man/remote_macro/task_queue.rb +6 -10
  63. data/lib/procon_bypass_man/remote_pbm_action/commands/update_remote_pbm_action_status_command.rb +1 -1
  64. data/lib/procon_bypass_man/runner.rb +4 -9
  65. data/lib/procon_bypass_man/scheduler.rb +15 -6
  66. data/lib/procon_bypass_man/support/callbacks.rb +72 -34
  67. data/lib/procon_bypass_man/support/can_over_process.rb +60 -0
  68. data/lib/procon_bypass_man/support/gc.rb +8 -0
  69. data/lib/procon_bypass_man/support/http_client.rb +9 -6
  70. data/lib/procon_bypass_man/support/load_agv.rb +20 -0
  71. data/lib/procon_bypass_man/support/procon_performance_http_client.rb +7 -0
  72. data/lib/procon_bypass_man/support/retryable.rb +16 -0
  73. data/lib/procon_bypass_man/support/signal_handler.rb +1 -1
  74. data/lib/procon_bypass_man/version.rb +1 -1
  75. data/lib/procon_bypass_man/websocket/client.rb +2 -2
  76. data/lib/procon_bypass_man/worker.rb +32 -0
  77. data/lib/procon_bypass_man.rb +41 -10
  78. data/procon_bypass_man.gemspec +1 -1
  79. data/project_template/README.md +0 -5
  80. data/project_template/app.rb +21 -11
  81. data/project_template/app.rb.erb +62 -0
  82. data/project_template/lib/app_generator.rb +31 -0
  83. data/project_template/web.rb +1 -1
  84. data/sig/main.rbs +10 -52
  85. data/tmp/.keep +0 -0
  86. metadata +34 -10
  87. data/lib/procon_bypass_man/background/job_runner.rb +0 -45
  88. data/lib/procon_bypass_man/background/jobs/concerns/has_internal_api_setting.rb +0 -5
  89. data/lib/procon_bypass_man/background/jobs/concerns/job_runnable.rb +0 -16
  90. data/lib/procon_bypass_man/background/jobs/report_pressed_buttons_job.rb +0 -15
  91. data/lib/procon_bypass_man/bypass/usb_hid_logger.rb +0 -44
  92. data/lib/procon_bypass_man/io_monitor.rb +0 -108
  93. data/lib/procon_bypass_man/support/server_pool.rb +0 -46
@@ -2,17 +2,26 @@ class ProconBypassMan::DeviceConnection::ProconSettingOverrider
2
2
  attr_accessor :procon, :output_report_watcher, :output_report_generator
3
3
 
4
4
  SUB_COMMAND_HOME_LED_ON = "38"
5
+ SUB_COMMAND_VIBRATION = "48"
6
+
5
7
  SUB_COMMAND_ARG_HOME_LED_ON = "1FF0FF"
8
+ SUB_COMMAND_ARG_VIBRATION_OFF = "00"
9
+
10
+ ALL_SETTINGS = {
11
+ home_led_on: [SUB_COMMAND_HOME_LED_ON, SUB_COMMAND_ARG_HOME_LED_ON],
12
+ vibration_off: [SUB_COMMAND_VIBRATION, SUB_COMMAND_ARG_VIBRATION_OFF],
13
+ }
14
+
15
+ # TODO 自動生成する
6
16
  SPECIAL_SUB_COMMAND_ARGS = {
7
17
  SUB_COMMAND_HOME_LED_ON => SUB_COMMAND_ARG_HOME_LED_ON,
18
+ SUB_COMMAND_VIBRATION => SUB_COMMAND_ARG_VIBRATION_OFF,
8
19
  }
9
- SETTING_HOME_LED_ON = { home_led_on: [SUB_COMMAND_HOME_LED_ON, SUB_COMMAND_ARG_HOME_LED_ON] }
10
- ALL_SETTINGS = SETTING_HOME_LED_ON
11
20
 
12
21
  def initialize(procon: )
13
22
  use_steps = {}
14
23
  if ProconBypassMan.config.enable_home_led_on_connect
15
- use_steps.merge!(SETTING_HOME_LED_ON)
24
+ use_steps.merge!(ALL_SETTINGS)
16
25
  end
17
26
 
18
27
  @setting_steps = use_steps.keys
@@ -99,7 +99,7 @@ class ProconBypassMan::Procon::Macro
99
99
 
100
100
  if nested_step.over?
101
101
  steps.shift # NestedStepを破棄する
102
- self.after_callback_block.call if self.after_callback_block
102
+ self.after_callback_block.call if self.after_callback_block && steps.empty?
103
103
  return next_step
104
104
  else
105
105
  return nested_step.next_step
@@ -0,0 +1,17 @@
1
+ class ProconBypassMan::Procon::PerformanceMeasurement::LastBypassAt
2
+ include Singleton
3
+
4
+ attr_accessor :mutex, :last_bypass_at
5
+
6
+ def initialize
7
+ self.mutex = Mutex.new
8
+ self.last_bypass_at = Time.now
9
+ end
10
+
11
+ def self.touch(&block)
12
+ instance.mutex.synchronize do
13
+ block.call(Time.now - instance.last_bypass_at)
14
+ instance.last_bypass_at = Time.now
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ # TODO SpanCollection にする
2
+ class ProconBypassMan::Procon::PerformanceMeasurement::MeasurementCollection
3
+ attr_accessor :timestamp_key, :spans
4
+
5
+ def initialize(timestamp_key: , spans: )
6
+ self.timestamp_key = timestamp_key
7
+ self.spans = spans
8
+ end
9
+ end
@@ -0,0 +1,84 @@
1
+ class ProconBypassMan::Procon::PerformanceMeasurement::MeasurementsSummarizer
2
+ class PerformanceMetric < Struct.new(:interval_from_previous_succeed_max,
3
+ :interval_from_previous_succeed_p50,
4
+ :write_time_max,
5
+ :write_time_p50,
6
+ :read_time_max,
7
+ :read_time_p50,
8
+ :time_taken_p50,
9
+ :time_taken_p95,
10
+ :time_taken_p99,
11
+ :time_taken_max,
12
+ :read_error_count,
13
+ :write_error_count,
14
+ :gc_count,
15
+ :gc_time,
16
+ :succeed_rate); end
17
+
18
+ def initialize(spans: )
19
+ @spans = spans
20
+ end
21
+
22
+ # @return [PerformanceMetric]
23
+ def summarize
24
+ sorted_write_time = @spans.map(&:write_time).sort
25
+ sorted_read_time = @spans.map(&:read_time).sort
26
+ gc_count = @spans.map(&:gc_count).sum
27
+ gc_time = @spans.map(&:gc_time).sum
28
+
29
+ sorted_time_taken = @spans.select(&:succeed).map(&:time_taken).sort
30
+ sorted_interval_from_previous_succeed = @spans.select(&:succeed).map(&:interval_from_previous_succeed).sort
31
+
32
+ interval_from_previous_succeed_max = sorted_interval_from_previous_succeed.last || 0
33
+ interval_from_previous_succeed_p50 = percentile(sorted_list: sorted_interval_from_previous_succeed , percentile: 0.50)
34
+
35
+ write_time_max = sorted_write_time.last || 0
36
+ write_time_p50 = percentile(sorted_list: sorted_write_time , percentile: 0.50)
37
+
38
+ read_time_max = sorted_read_time.last || 0
39
+ read_time_p50 = percentile(sorted_list: sorted_read_time , percentile: 0.50)
40
+
41
+ time_taken_p50 = percentile(sorted_list: sorted_time_taken, percentile: 0.50)
42
+ time_taken_p95 = percentile(sorted_list: sorted_time_taken, percentile: 0.95)
43
+ time_taken_p99 = percentile(sorted_list: sorted_time_taken, percentile: 0.99)
44
+ time_taken_max = sorted_time_taken.last || 0
45
+
46
+ total_read_error_count = @spans.map(&:read_error_count).sum
47
+ total_write_error_count = @spans.map(&:write_error_count).sum
48
+ succeed_rate =
49
+ if @spans.length.zero?
50
+ 0
51
+ else
52
+ succeed_rate = (sorted_time_taken.length / @spans.length.to_f).floor(3)
53
+ end
54
+
55
+ PerformanceMetric.new(interval_from_previous_succeed_max,
56
+ interval_from_previous_succeed_p50,
57
+ write_time_max,
58
+ write_time_p50,
59
+ read_time_max,
60
+ read_time_p50,
61
+ time_taken_p50,
62
+ time_taken_p95,
63
+ time_taken_p99,
64
+ time_taken_max,
65
+ total_read_error_count,
66
+ total_write_error_count,
67
+ gc_count,
68
+ gc_time,
69
+ succeed_rate)
70
+ end
71
+
72
+ private
73
+
74
+ # @param [Array<Numeric>] sorted_list
75
+ # @param [Float] percentile
76
+ # @return [Float]
77
+ def percentile(sorted_list: , percentile: )
78
+ return 0.0 if sorted_list.empty?
79
+ values_sorted = sorted_list
80
+ k = ((percentile*(values_sorted.length-1))+1).floor - 1
81
+ f = ((percentile*(values_sorted.length-1))+1).modulo(1)
82
+ return(values_sorted[k] + (f * (values_sorted[k+1] - values_sorted[k]))).floor(3)
83
+ end
84
+ end
@@ -0,0 +1,8 @@
1
+ # Bypassプロセスが収集したパフォーマンスメトリクスを、集計するためにmasterプロセスに転送するためジョブ
2
+ class ProconBypassMan::ProconPerformanceSpanTransferJob
3
+ extend ProconBypassMan::Background::JobPerformable
4
+
5
+ def self.perform(spans)
6
+ ProconBypassMan::Procon::PerformanceMeasurement::QueueOverProcess.push(spans)
7
+ end
8
+ end
@@ -0,0 +1,38 @@
1
+ class ProconBypassMan::Procon::PerformanceMeasurement::QueueOverProcess
2
+ extend ProconBypassMan::CanOverProcess
3
+
4
+ include Singleton
5
+
6
+ attr_reader :distributed_queue
7
+
8
+ # @override
9
+ def self.enable?
10
+ ProconBypassMan.config.enable_procon_performance_measurement?
11
+ end
12
+
13
+ # @override
14
+ def self.distributed_class
15
+ ProconBypassMan::Procon::PerformanceMeasurement::SpanQueue
16
+ end
17
+
18
+ # @override
19
+ def self.socket_file_path
20
+ "/tmp/procon_bypass_man_procon_performance_queue".freeze
21
+ end
22
+
23
+ def self.push(value)
24
+ return unless enable?
25
+
26
+ instance.distributed_queue.push(value)
27
+ end
28
+
29
+ def self.pop
30
+ return unless enable?
31
+
32
+ instance.distributed_queue.pop
33
+ end
34
+
35
+ def initialize
36
+ @distributed_queue = DRbObject.new_with_uri(self.class.socket_path)
37
+ end
38
+ end
@@ -0,0 +1,42 @@
1
+ class ProconBypassMan::Procon::PerformanceMeasurement::SpanQueue
2
+ def initialize
3
+ @current_table = {} # 1つのスレッドからしか触らないのでlockはいらない
4
+ @measurement_collection_list = [] # main threadとjob worker threadから触るのでlockが必要
5
+ end
6
+
7
+ # @param [Array<PerformanceSpan>] spans
8
+ # bypassプロセスから呼ばれる. 実際に実行を行なっているのはmasterプロセス
9
+ def push(new_spans)
10
+ current_key = generate_bucket_key
11
+
12
+ if @current_table[current_key].nil?
13
+ if not @current_table.empty?
14
+ timestamp_key = @current_table.keys.first
15
+ spans = @current_table.values.first
16
+ # 本当ならmutexでlockする必要があるけど、正確性はいらないのでパフォーマンスを上げるためにlockしない
17
+ @measurement_collection_list.push(
18
+ ProconBypassMan::Procon::PerformanceMeasurement::MeasurementCollection.new(timestamp_key: timestamp_key, spans: spans)
19
+ )
20
+ end
21
+
22
+ @current_table = {}
23
+ @current_table[current_key] = []
24
+ @current_table[current_key].concat(new_spans)
25
+ else
26
+ @current_table[current_key].concat(new_spans)
27
+ end
28
+ end
29
+
30
+ # job workerから呼ばれる
31
+ # @return [ProconBypassMan::Procon::PerformanceMeasurement::MeasurementCollection]
32
+ def pop
33
+ @measurement_collection_list.pop
34
+ end
35
+
36
+ private
37
+
38
+ # 1分単位で次の値になる
39
+ def generate_bucket_key
40
+ Time.new.strftime("%Y-%m-%d %H:%M:00%:z")
41
+ end
42
+ end
@@ -0,0 +1,39 @@
1
+ class ProconBypassMan::Procon::PerformanceMeasurement::SpanTransferBuffer
2
+ include Singleton
3
+
4
+ def initialize
5
+ @buff = []
6
+ end
7
+
8
+ # @param [Span]
9
+ # @return [void]
10
+ def push_and_run_block_if_buffer_over(value, &block)
11
+ push(value)
12
+ return unless buffer_over?
13
+
14
+ block.call(spans)
15
+ clear
16
+ end
17
+
18
+ private
19
+
20
+ def spans
21
+ @buff
22
+ end
23
+
24
+ def clear
25
+ @buff.clear
26
+ end
27
+
28
+ def push(value)
29
+ @buff << value
30
+ end
31
+
32
+ def buffer_over?
33
+ @buff.length > max_buffer
34
+ end
35
+
36
+ def max_buffer
37
+ 200
38
+ end
39
+ end
@@ -0,0 +1,103 @@
1
+ module ProconBypassMan::Procon::PerformanceMeasurement; end
2
+
3
+ require 'benchmark'
4
+ require 'procon_bypass_man/procon/performance_measurement/measurements_summarizer'
5
+ require 'procon_bypass_man/procon/performance_measurement/span_queue'
6
+ require 'procon_bypass_man/procon/performance_measurement/procon_performance_span_transfer_job'
7
+ require 'procon_bypass_man/procon/performance_measurement/span_transfer_buffer'
8
+ require 'procon_bypass_man/procon/performance_measurement/measurement_collection'
9
+ require 'procon_bypass_man/procon/performance_measurement/queue_over_process'
10
+ require 'procon_bypass_man/procon/performance_measurement/last_bypass_at'
11
+
12
+ module ProconBypassMan::Procon::PerformanceMeasurement
13
+ class PerformanceSpan
14
+ attr_accessor :time_taken, :succeed, :interval_from_previous_succeed, :gc_count, :gc_time
15
+ attr_reader :write_error_count, :read_error_count, :write_time, :read_time
16
+
17
+ def initialize
18
+ @write_error_count = 0
19
+ @read_error_count = 0
20
+ @time_taken = 0.0
21
+ @succeed = nil
22
+ @interval_from_previous_succeed = nil
23
+ @custom_metric = {}
24
+ @write_time = 0.0
25
+ @read_time = 0.0
26
+ @gc_time = 0.0
27
+ end
28
+
29
+ def record_read_error
30
+ @read_error_count += 1
31
+ end
32
+
33
+ def record_write_error
34
+ @write_error_count += 1
35
+ end
36
+
37
+ def record_write_time(&block)
38
+ result = nil
39
+ @write_time = Benchmark.realtime { result = block.call }
40
+ return result
41
+ end
42
+
43
+ def record_read_time(&block)
44
+ @read_time = Benchmark.realtime { block.call }
45
+ end
46
+ end
47
+
48
+ # measureをして、measureの結果をためる
49
+ # @return [Boolean] 成功したか. テスト時に戻り値を使いたい
50
+ def self.measure(&bypass_process_block)
51
+ unless ProconBypassMan.config.enable_procon_performance_measurement?
52
+ bypass_process_block.call(PerformanceSpan.new)
53
+ return
54
+ end
55
+
56
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.1.0")
57
+ snapshot_gc_time = GC.stat(:time) / 1000.0
58
+ end
59
+ snapshot_gc_count = GC.count
60
+ span = PerformanceSpan.new
61
+
62
+ span.time_taken = Benchmark.realtime {
63
+ span.succeed = bypass_process_block.call(span)
64
+ }.floor(3)
65
+
66
+ if span.succeed
67
+ ProconBypassMan::Procon::PerformanceMeasurement::LastBypassAt.touch do |interval_from_previous_succeed|
68
+ span.interval_from_previous_succeed = interval_from_previous_succeed.floor(3)
69
+ end
70
+ end
71
+
72
+ (GC.count - snapshot_gc_count).tap do |increased_gc_count|
73
+ span.gc_count = increased_gc_count
74
+ end
75
+
76
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.1.0")
77
+ ((GC.stat(:time) / 1000.0) - snapshot_gc_time).tap do |increased_time|
78
+ span.gc_time = increased_time
79
+ end
80
+ end
81
+
82
+ ProconBypassMan::GC.stop_gc_in do
83
+ # measureするたびにperform_asyncしているとjob queueが詰まるのでbufferingしている
84
+ ProconBypassMan::Procon::PerformanceMeasurement::SpanTransferBuffer.instance.push_and_run_block_if_buffer_over(span) do |spans|
85
+ ProconBypassMan::ProconPerformanceSpanTransferJob.perform_async(spans.dup)
86
+ end
87
+ end
88
+ return span.succeed
89
+ end
90
+
91
+ # @return [MeasurementCollection, NilClass]
92
+ # bypassしているプロセスから呼ばれる
93
+ def self.pop_measurement_collection
94
+ ProconBypassMan::Procon::PerformanceMeasurement::QueueOverProcess.pop
95
+ end
96
+
97
+ # @param [MeasurementCollection] spans
98
+ # @return [ProconBypassMan::Procon::PerformanceMeasurement::MeasurementsSummarizer::PerformanceMetric]
99
+ # jobから呼ばれる予定
100
+ def self.summarize(spans: )
101
+ ProconBypassMan::Procon::PerformanceMeasurement::MeasurementsSummarizer.new(spans: spans).summarize
102
+ end
103
+ end
@@ -7,8 +7,9 @@ class ProconBypassMan::Procon
7
7
  require "procon_bypass_man/procon/value_objects/rumble_binary"
8
8
  require "procon_bypass_man/procon/value_objects/binary"
9
9
  require "procon_bypass_man/procon/value_objects/bypass_mode"
10
-
11
10
  require "procon_bypass_man/procon/consts"
11
+ require "procon_bypass_man/procon/performance_measurement"
12
+ require "procon_bypass_man/procon/performance_measurement/queue_over_process"
12
13
  require "procon_bypass_man/procon/mode_registry"
13
14
  require "procon_bypass_man/procon/macro"
14
15
  require "procon_bypass_man/procon/macro_registry"
@@ -0,0 +1,12 @@
1
+ module ProconBypassMan::ProconDisplay::BypassHook
2
+ include ProconBypassMan::Callbacks
3
+
4
+ define_callbacks :run
5
+
6
+ set_callback :run, :after, :write_procon_display_status
7
+
8
+ def write_procon_display_status
9
+ return unless bypass_value.binary
10
+ ProconBypassMan::ProconDisplay::Status.instance.current = bypass_value.binary.to_procon_reader.to_hash.dup
11
+ end
12
+ end
@@ -0,0 +1,31 @@
1
+ module ProconBypassMan::ProconDisplay
2
+ # NOTE Support GET only
3
+ class HttpRequest
4
+ def self.parse(conn)
5
+ headers = {}
6
+ loop do
7
+ line = conn.gets("\n")&.strip
8
+ break if line.nil? || line.strip.empty?
9
+ key, value = line.split(/:\s/, 2)
10
+ headers[key] = value
11
+ end
12
+
13
+ new(headers)
14
+ end
15
+
16
+ def initialize(headers)
17
+ @headers = headers
18
+ end
19
+
20
+ def path
21
+ request_method_and_path = @headers.detect { |key, _value| key.start_with?("GET") }.first
22
+ if request_method_and_path =~ /(?:GET) ([^ ]+)/ && (path = $1)
23
+ return path
24
+ end
25
+ end
26
+
27
+ def to_hash
28
+ { "PATH" => path }
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ module ProconBypassMan::ProconDisplay
2
+ class HttpResponse
3
+ def initialize(body, status: , format: "text/json")
4
+ @body = body&.to_json
5
+ @status = status
6
+ @format = format
7
+ end
8
+
9
+ def to_s
10
+ <<~EOH
11
+ HTTP/1.1 #{@status}
12
+ Content-Length: #{@body&.bytes&.size || 0}
13
+ Content-Type: #{@format}
14
+ Access-Control-Allow-Origin: *
15
+ Access-Control-Allow-Methods: GET
16
+ Access-Control-Allow-Private-Network:true
17
+ Connection: close
18
+
19
+ #{@body}
20
+ EOH
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,33 @@
1
+ require 'socket'
2
+
3
+ module ProconBypassMan::ProconDisplay
4
+ class Server
5
+ PORT = 9900
6
+
7
+ def self.start!
8
+ Thread.new do
9
+ new.start_with_foreground
10
+ end
11
+ end
12
+
13
+ def initialize
14
+ @server = TCPServer.new('0.0.0.0', PORT)
15
+ end
16
+
17
+ def start_with_foreground
18
+ loop do
19
+ conn = @server.accept
20
+ response = ServerApp.new(
21
+ HttpRequest.parse(conn).to_hash
22
+ ).call
23
+ conn.write(response)
24
+ conn.close
25
+ end
26
+ rescue Errno::EADDRINUSE => e
27
+ ProconBypassMan::SendErrorCommand.execute(error: e)
28
+ rescue => e
29
+ ProconBypassMan::SendErrorCommand.execute(error: e)
30
+ retry
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,17 @@
1
+ module ProconBypassMan::ProconDisplay
2
+ class ServerApp
3
+ def initialize(env)
4
+ @env = env
5
+ end
6
+
7
+ def call
8
+ case @env["PATH"]
9
+ when "/input"
10
+ response = ProconBypassMan::ProconDisplay::Status.instance.current
11
+ HttpResponse.new(response, status: 200).to_s
12
+ else
13
+ HttpResponse.new(nil, status: 404).to_s
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ require 'singleton'
2
+
3
+ class ProconBypassMan::ProconDisplay::Status
4
+ include Singleton
5
+
6
+ # @return [Hash]
7
+ def current
8
+ @current || {}
9
+ end
10
+
11
+ # @return [void]
12
+ # @param [Hash] value
13
+ def current=(value)
14
+ if value.is_a?(Hash)
15
+ @current = value
16
+ else
17
+ @current = nil
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ # 入力表示用
2
+
3
+ module ProconBypassMan::ProconDisplay
4
+ end
5
+
6
+ require "procon_bypass_man/procon_display/server"
7
+ require "procon_bypass_man/procon_display/server_app"
8
+ require "procon_bypass_man/procon_display/status"
9
+ require "procon_bypass_man/procon_display/http_response"
10
+ require "procon_bypass_man/procon_display/http_request"
11
+ require "procon_bypass_man/procon_display/bypass_hook"
@@ -1,62 +1,44 @@
1
- class ProconBypassMan::QueueOverProcess
2
- attr_reader :drb
3
-
4
- @@drb_server = nil
5
- @@drb_server_thread = nil
6
-
7
- def self.start!
8
- return unless ProconBypassMan.config.enable_remote_macro?
9
- require 'drb/drb'
10
-
11
- FileUtils.rm_rf(file_path) if File.exist?(file_path)
12
- begin
13
- @@drb_server = DRb.start_service(url, Queue.new, safe_level: 1)
14
- rescue Errno::EADDRINUSE => e
15
- ProconBypassMan.logger.error e
16
- raise
17
- end
18
-
19
- @@drb_server_thread =
20
- Thread.new do
21
- DRb.thread.join
22
- end
23
- end
1
+ class ProconBypassMan::RemoteMacro::QueueOverProcess
2
+ extend ProconBypassMan::CanOverProcess
24
3
 
25
- def self.shutdown
26
- if @@drb_server
27
- @@drb_server_thread.kill
28
- @@drb_server.stop_service
29
- end
30
- end
4
+ include Singleton
31
5
 
32
- def self.push(value)
33
- return unless ProconBypassMan.config.enable_remote_macro?
6
+ attr_reader :distributed_queue
34
7
 
35
- drb.push(value)
8
+ # @override
9
+ def self.enable?
10
+ ProconBypassMan.config.enable_remote_macro?
36
11
  end
37
12
 
38
- def self.pop
39
- return unless ProconBypassMan.config.enable_remote_macro?
13
+ # @override
14
+ def self.distributed_class
15
+ Queue
16
+ end
40
17
 
41
- drb.pop
18
+ # @override
19
+ def self.socket_file_path
20
+ "/tmp/procon_bypass_man_remote_macro_queue".freeze
42
21
  end
43
22
 
44
- def self.drb
45
- return unless ProconBypassMan.config.enable_remote_macro?
23
+ def self.push(value)
24
+ return unless enable?
46
25
 
47
- @@drb ||= new.drb
26
+ instance.distributed_queue.push(value)
48
27
  end
49
28
 
50
- PROTOCOL = "drbunix"
51
- def self.url
52
- "#{PROTOCOL}:/tmp/procon_bypass_man_queue"
29
+ def self.pop
30
+ return unless enable?
31
+
32
+ instance.distributed_queue.pop
53
33
  end
54
34
 
55
- def self.file_path
56
- url.gsub("#{PROTOCOL}:", "")
35
+ def self.clear
36
+ return unless enable?
37
+
38
+ instance.distributed_queue.clear
57
39
  end
58
40
 
59
41
  def initialize
60
- @drb = DRbObject.new_with_uri(self.class.url)
42
+ @distributed_queue = DRbObject.new_with_uri(self.class.socket_path)
61
43
  end
62
44
  end