procon_bypass_man 0.1.8 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +3 -2
  3. data/.github/workflows/ruby.yml +5 -4
  4. data/.gitignore +5 -0
  5. data/.rubocop.yml +2 -0
  6. data/.ruby-version +1 -1
  7. data/CHANGELOG.md +18 -1
  8. data/Gemfile +4 -0
  9. data/Gemfile.lock +53 -2
  10. data/README.md +14 -8
  11. data/Steepfile +39 -0
  12. data/bin/console +4 -0
  13. data/bin/dev_api_server.rb +18 -0
  14. data/docs/setup_raspi.mitamae.rb +12 -0
  15. data/lib/procon_bypass_man/background/has_server_pool.rb +54 -0
  16. data/lib/procon_bypass_man/background/http_client.rb +70 -0
  17. data/lib/procon_bypass_man/background/job_performer.rb +16 -0
  18. data/lib/procon_bypass_man/background/job_runnable.rb +16 -0
  19. data/lib/procon_bypass_man/background/job_runner.rb +44 -0
  20. data/lib/procon_bypass_man/background/jobs/base_job.rb +12 -0
  21. data/lib/procon_bypass_man/background/jobs/report_boot_job.rb +10 -0
  22. data/lib/procon_bypass_man/background/jobs/report_error_job.rb +10 -0
  23. data/lib/procon_bypass_man/background/jobs/report_heartbeat_job.rb +10 -0
  24. data/lib/procon_bypass_man/background/jobs/report_pressed_buttons_job.rb +18 -0
  25. data/lib/procon_bypass_man/background/jobs/report_reload_config_job.rb +10 -0
  26. data/lib/procon_bypass_man/background.rb +10 -0
  27. data/lib/procon_bypass_man/boot_message.rb +42 -0
  28. data/lib/procon_bypass_man/{configuration → buttons_setting_configuration}/layer.rb +50 -4
  29. data/lib/procon_bypass_man/{configuration → buttons_setting_configuration}/loader.rb +12 -11
  30. data/lib/procon_bypass_man/{configuration → buttons_setting_configuration}/validator.rb +1 -1
  31. data/lib/procon_bypass_man/buttons_setting_configuration.rb +101 -0
  32. data/lib/procon_bypass_man/bypass/usb_hid_logger.rb +36 -0
  33. data/lib/procon_bypass_man/bypass.rb +61 -29
  34. data/lib/procon_bypass_man/callbacks.rb +70 -0
  35. data/lib/procon_bypass_man/commands/connect_device_command.rb +11 -0
  36. data/lib/procon_bypass_man/commands/print_boot_message_command.rb +9 -0
  37. data/lib/procon_bypass_man/commands/send_error_command.rb +18 -0
  38. data/lib/procon_bypass_man/commands/send_reload_config_event_command.rb +10 -0
  39. data/lib/procon_bypass_man/commands/write_device_id_command.rb +11 -0
  40. data/lib/procon_bypass_man/commands/write_session_id_command.rb +13 -0
  41. data/lib/procon_bypass_man/commands.rb +6 -0
  42. data/lib/procon_bypass_man/configuration.rb +92 -67
  43. data/lib/procon_bypass_man/device_connector.rb +11 -29
  44. data/lib/procon_bypass_man/io_monitor.rb +16 -8
  45. data/lib/procon_bypass_man/on_memory_cache.rb +34 -0
  46. data/lib/procon_bypass_man/procon/analog_stick.rb +31 -0
  47. data/lib/procon_bypass_man/procon/analog_stick_cap.rb +65 -0
  48. data/lib/procon_bypass_man/procon/button_collection.rb +15 -6
  49. data/lib/procon_bypass_man/procon/{data.rb → consts.rb} +1 -1
  50. data/lib/procon_bypass_man/procon/layer_changer.rb +40 -0
  51. data/lib/procon_bypass_man/procon/macro_registry.rb +2 -2
  52. data/lib/procon_bypass_man/procon/mode_registry.rb +4 -4
  53. data/lib/procon_bypass_man/procon/press_button_aware.rb +13 -0
  54. data/lib/procon_bypass_man/procon/user_operation.rb +21 -16
  55. data/lib/procon_bypass_man/procon.rb +23 -9
  56. data/lib/procon_bypass_man/procon_reader.rb +31 -0
  57. data/lib/procon_bypass_man/runner.rb +43 -64
  58. data/lib/procon_bypass_man/uptime.rb +14 -2
  59. data/lib/procon_bypass_man/version.rb +1 -1
  60. data/lib/procon_bypass_man.rb +38 -43
  61. data/project_template/README.md +1 -1
  62. data/project_template/app.rb +7 -5
  63. data/project_template/systemd_units/pbm_web.service +11 -0
  64. data/project_template/web.rb +16 -0
  65. data/sig/README.rb +4 -0
  66. data/sig/main.rbs +505 -0
  67. metadata +42 -11
  68. data/examples/practical/app.rb +0 -21
  69. data/examples/practical/setting.yml +0 -24
  70. data/lib/procon_bypass_man/procon/layer_changeable.rb +0 -28
  71. data/lib/procon_bypass_man/procon/pressed_button_helper.rb +0 -25
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f5d2c0b9137168508351ca8a1492470cd1f03c31d7eb920010a32f08f92122e
4
- data.tar.gz: c7f42dd711cb3d8932a8fd6d99ee0735084618411e6cd723eeee8860ca96cd8c
3
+ metadata.gz: 42551d494786247ae06f81c416f522360001fb0a78ffdd67311520aafb56587a
4
+ data.tar.gz: 92da20f533512ec4e1fdebe93f2c064f5ae1513fd6f26d3c4f34f2a0d488dffd
5
5
  SHA512:
6
- metadata.gz: 71a45ddf739c64355197e1a0610d2441eb6445eb6d77eef8fd9cf711265d0a9fc23dfcf2d75075115bf1e546141fec7eab0e20c6469feff6b7dea238d4916951
7
- data.tar.gz: 1bb4bc56c9310d94d682454b33c3d28ebc0a45753ee292c98cdee47bc94b3400c50ddbbb9c1f08b17ade330829760b4dbc1eef85ea22faf1aa8f9271e1b53e0f
6
+ metadata.gz: b52fde78831c29dc1b56b56217a604e421b6c0cdf5859b997732b36d94b9b7dc9d02f3906835d41df5f7168a2b694c2c457d3f96fd235903fc4827f42e74422b
7
+ data.tar.gz: 50defecca29919fca30680469deddc5adc5c41d0c4f02f084c0a73f9633aa8e11101a5f07e863ea4e0b6481178d4f11e0d466a4bf2d9954f08cb5407d3cb10dd
data/.circleci/config.yml CHANGED
@@ -32,15 +32,16 @@ jobs:
32
32
  - run: bundle install --jobs 4
33
33
  - run: bundle exec rubocop
34
34
  - run: bundle exec rspec
35
+ - run: bundle exec steep check
35
36
 
36
37
  build_jobs: &build_jobs
37
38
  - rspec:
38
39
  matrix:
39
40
  parameters:
40
41
  version:
41
- - "2.5"
42
42
  - "2.7"
43
- - "3.0"
43
+ - "3.0.1"
44
+ - "3.0.2"
44
45
  workflows:
45
46
  version: 2
46
47
  build:
@@ -16,7 +16,7 @@ jobs:
16
16
  runs-on: ubuntu-latest
17
17
  strategy:
18
18
  matrix:
19
- ruby-version: ['2.5', '2.7', '3.0']
19
+ ruby-version: ['2.7', '3.0.1']
20
20
 
21
21
  steps:
22
22
  - uses: actions/checkout@v2
@@ -24,10 +24,11 @@ jobs:
24
24
  # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
25
25
  # change this to (see https://github.com/ruby/setup-ruby#versioning):
26
26
  # uses: ruby/setup-ruby@v1
27
- uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
27
+ uses: ruby/setup-ruby@v1
28
28
  with:
29
29
  ruby-version: ${{ matrix.ruby-version }}
30
30
  bundler-cache: true # runs 'bundle install' and caches installed gems automatically
31
31
  - name: Run tests
32
- run: bundle exec rubocop
33
- run: bundle exec rake
32
+ run: |-
33
+ bundle exec rake
34
+ bundle exec steep check
data/.gitignore CHANGED
@@ -8,3 +8,8 @@
8
8
  /tmp/
9
9
  vendor
10
10
  todo.local.md
11
+ *.log
12
+ .setting_yaml_digest
13
+ pbm_pid
14
+ setting.yml
15
+ device_id
data/.rubocop.yml CHANGED
@@ -22,3 +22,5 @@ Lint/UselessAssignment:
22
22
  Enabled: false
23
23
  Lint/EmptyBlock:
24
24
  Enabled: false
25
+ Lint/ReturnInVoidContext:
26
+ Enabled: false
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.0.1
1
+ 3.0.2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
+ ## [0.1.12] - 2021-11-18
2
+ - 起動ログなどを設定した外部サーバに送信できるようにしました
3
+
4
+ ## [0.1.11] - 2021-11-05
5
+ - PBM-WEB(>= 0.1.3)でボタンの入力を見るためのスレッドが動くようになりました
6
+
7
+ ## [0.1.10] - 2021-10-28
8
+ - ボタンを無効化するdisableを設定できるようになりました
9
+ - pvmenvで生成する初期ファイルにprocon_bypass_man-webを同梱しました
10
+ - PBM自体の設定の構文を変更しました
11
+
12
+ ## [0.1.9] - 2021-10-20
13
+ - 左スティックを傾けた時に設定した最大値内に丸めることができるようになりました
14
+ - 関連メソッド: set_neutral_position, left_analog_stick_cap
15
+ - 数時間に一度起きる、数秒間入力が通らなくなる問題の改善
16
+
1
17
  ## [0.1.8] - 2021-09-19
2
- - 使える場合、GC.auto_compactを有効にする
18
+ - 使える場合、GC.auto_compactを有効にしました
19
+ - 起動ログにuptimeからの経過時間を表示しました
3
20
 
4
21
  ## [0.1.7] - 2021-09-11
5
22
  - Support pbmenv
data/Gemfile CHANGED
@@ -10,3 +10,7 @@ gem "rspec"
10
10
  gem "pry"
11
11
  gem "timecop"
12
12
  gem "rubocop", require: false
13
+ gem "steep", require: false
14
+ gem 'rbs', require: false
15
+ gem "sinatra", require: false
16
+ gem "webrick", require: false
data/Gemfile.lock CHANGED
@@ -1,23 +1,47 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- procon_bypass_man (0.1.8)
4
+ procon_bypass_man (0.1.12)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ activesupport (6.1.4.1)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 1.6, < 2)
12
+ minitest (>= 5.1)
13
+ tzinfo (~> 2.0)
14
+ zeitwerk (~> 2.3)
9
15
  ast (2.4.2)
10
16
  coderay (1.1.3)
17
+ concurrent-ruby (1.1.9)
11
18
  diff-lcs (1.4.4)
19
+ ffi (1.15.4)
20
+ i18n (1.8.10)
21
+ concurrent-ruby (~> 1.0)
22
+ language_server-protocol (3.16.0.3)
23
+ listen (3.7.0)
24
+ rb-fsevent (~> 0.10, >= 0.10.3)
25
+ rb-inotify (~> 0.9, >= 0.9.10)
12
26
  method_source (1.0.0)
27
+ minitest (5.14.4)
28
+ mustermann (1.1.1)
29
+ ruby2_keywords (~> 0.0.1)
13
30
  parallel (1.20.1)
14
31
  parser (3.0.2.0)
15
32
  ast (~> 2.4.1)
16
33
  pry (0.14.1)
17
34
  coderay (~> 1.1)
18
35
  method_source (~> 1.0)
36
+ rack (2.2.3)
37
+ rack-protection (2.1.0)
38
+ rack
19
39
  rainbow (3.0.0)
20
40
  rake (13.0.6)
41
+ rb-fsevent (0.11.0)
42
+ rb-inotify (0.10.1)
43
+ ffi (~> 1.0)
44
+ rbs (1.6.2)
21
45
  regexp_parser (2.1.1)
22
46
  rexml (3.2.5)
23
47
  rspec (3.10.0)
@@ -45,12 +69,35 @@ GEM
45
69
  rubocop-ast (1.11.0)
46
70
  parser (>= 3.0.1.1)
47
71
  ruby-progressbar (1.11.0)
72
+ ruby2_keywords (0.0.5)
73
+ sinatra (2.1.0)
74
+ mustermann (~> 1.0)
75
+ rack (~> 2.2)
76
+ rack-protection (= 2.1.0)
77
+ tilt (~> 2.0)
78
+ steep (0.46.0)
79
+ activesupport (>= 5.1)
80
+ language_server-protocol (>= 3.15, < 4.0)
81
+ listen (~> 3.0)
82
+ parallel (>= 1.0.0)
83
+ parser (>= 3.0)
84
+ rainbow (>= 2.2.2, < 4.0)
85
+ rbs (>= 1.2.0)
86
+ terminal-table (>= 2, < 4)
87
+ terminal-table (3.0.2)
88
+ unicode-display_width (>= 1.1.1, < 3)
89
+ tilt (2.0.10)
48
90
  timecop (0.9.4)
91
+ tzinfo (2.0.4)
92
+ concurrent-ruby (~> 1.0)
49
93
  unicode-display_width (2.0.0)
94
+ webrick (1.7.0)
95
+ zeitwerk (2.4.2)
50
96
 
51
97
  PLATFORMS
52
98
  arm-linux
53
99
  arm64-darwin-20
100
+ arm64-darwin-21
54
101
  armv7l-linux
55
102
  x86_64-linux
56
103
 
@@ -58,9 +105,13 @@ DEPENDENCIES
58
105
  procon_bypass_man!
59
106
  pry
60
107
  rake (~> 13.0)
108
+ rbs
61
109
  rspec
62
110
  rubocop
111
+ sinatra
112
+ steep
63
113
  timecop
114
+ webrick
64
115
 
65
116
  BUNDLED WITH
66
- 2.2.15
117
+ 2.2.30
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
1
  # ProconBypassMan
2
+ [![Ruby](https://github.com/splaplapla/procon_bypass_man/actions/workflows/ruby.yml/badge.svg?branch=master)](https://github.com/splaplapla/procon_bypass_man/actions/workflows/ruby.yml)
3
+
2
4
  * Switchに繋いだプロコンを連射機にしたり、キーのリマップをしたり、マクロを実行できるツールです
3
5
  * 設定ファイルはrubyスクリプトで記述します
4
6
  * 特定のタイトルに特化した振る舞いにしたい時は各プラグインを使ってください
@@ -57,21 +59,25 @@ Switch <-- (PBM): ZR連打
57
59
  * 設定ファイル マクロの引数に、ボタンを取れるようにする
58
60
 
59
61
  ## 開発系
60
- ```ruby
61
- ProconBypassMan.tap do |pbm|
62
- pbm.root = File.expand_path(__dir__)
63
- pbm.logger = Logger.new("#{ProconBypassMan.root}/app.log", 5, 1024 * 1024 * 10)
64
- pbm.logger.level = :debug
65
- end
66
- ```
62
+ * pbmenvで生成するapp.rbに開発用ブランチを参照してください
67
63
 
68
64
  ### プロコンとの接続を維持したまま、現在の設定ファイルをPBMに反映する
69
65
  ```shell
70
66
  sudo kill -USR2 `cat ./pbm_pid`
71
67
  ```
72
68
 
69
+ ### 起動ログをサーバに送信する
70
+ * `ProconBypassMan.api_server = "http://.."` を設定すると、 `POST /api/events` に対して起動ログなどを送信するようになります
71
+
72
+ ### 開発環境でログの送信を確認する方法
73
+ * `bundle exec bin/dev_api_server.rb`
74
+ * `INTERNAL_API_SERVER=http://localhost:4567 bin/console`
75
+ * `API_SERVER=http://localhost:4567 bin/console`
76
+ * `message = ProconBypassMan::BootMessage.new; ProconBypassMan::ReportBootJob.report(body: message.to_hash)`
77
+
73
78
  ### リリース手順
74
- * project_template/app.rb, lib/procon_bypass_man/version.rb のバージョンをあげる
79
+ * project_template/web.rb, project_template/app.rb, lib/procon_bypass_man/version.rb のバージョンをあげる
80
+ * CHANGELOG.md に日付を書く
75
81
  * be rake release
76
82
 
77
83
  ## License
data/Steepfile ADDED
@@ -0,0 +1,39 @@
1
+ # D = Steep::Diagnostic
2
+ #
3
+ # target :lib do
4
+ # signature "sig"
5
+ #
6
+ # check "lib" # Directory name
7
+ # check "Gemfile" # File name
8
+ # check "app/models/**/*.rb" # Glob
9
+ # # ignore "lib/templates/*.rb"
10
+ #
11
+ # # library "pathname", "set" # Standard libraries
12
+ # # library "strong_json" # Gems
13
+ #
14
+ # # configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
15
+ # # configure_code_diagnostics(D::Ruby.lenient) # `lenient` diagnostics setting
16
+ # # configure_code_diagnostics do |hash| # You can setup everything yourself
17
+ # # hash[D::Ruby::NoMethod] = :information
18
+ # # end
19
+ # end
20
+
21
+ # target :test do
22
+ # signature "sig", "sig-private"
23
+ #
24
+ # check "test"
25
+ #
26
+ # # library "pathname", "set" # Standard libraries
27
+ # end
28
+
29
+ target :lib do
30
+ check "lib/procon_bypass_man/timer.rb"
31
+ check "lib/procon_bypass_man/uptime.rb"
32
+ check "lib/procon_bypass_man/configuration.rb"
33
+
34
+ signature "sig"
35
+
36
+ library "time"
37
+ library "logger"
38
+ library "monitor"
39
+ end
data/bin/console CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  require "bundler/setup"
5
5
  require "procon_bypass_man"
6
+ require "pry"
6
7
 
7
8
  # You can add fixtures and/or initialization code here to make experimenting
8
9
  # with your gem easier. You can also use a different console, if you like.
@@ -12,4 +13,7 @@ require "procon_bypass_man"
12
13
  # Pry.start
13
14
 
14
15
  require "irb"
16
+
17
+ ProconBypassMan.config.logger = Logger.new($stdout)
18
+
15
19
  IRB.start(__FILE__)
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "sinatra"
4
+ require "webrick"
5
+ require "pry"
6
+
7
+ post '/api/events' do
8
+ puts JSON.parse(request.body.read)
9
+ status 200
10
+ body ''
11
+ end
12
+
13
+ post '/api/pressed_buttons' do
14
+ r = JSON.parse(request.body.read)
15
+ puts r
16
+ status 200
17
+ body ''
18
+ end
@@ -41,6 +41,18 @@ execute "Initialize PBM" do
41
41
  SHELL
42
42
  end
43
43
 
44
+ # rbenv
45
+ execute "Install rbenv" do
46
+ not_if "which rbenv"
47
+ command "git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build"
48
+ end
49
+
50
+ # ruby
51
+ execute "Install ruby" do
52
+ not_if "rbenv versions | grep 3.0.1"
53
+ command "rbenv install 3.0.1"
54
+ end
55
+
44
56
  run_command 'sudo systemctl disable triggerhappy.socket'
45
57
  run_command 'sudo systemctl disable triggerhappy.service'
46
58
  run_command 'sudo systemctl disable bluetooth'
@@ -0,0 +1,54 @@
1
+ module ProconBypassMan::Background::HasServerPool
2
+ class ServerPool
3
+ def initialize(servers: )
4
+ if servers.nil? || servers.empty?
5
+ return
6
+ end
7
+
8
+ @servers = servers
9
+ if @servers.size >= 1
10
+ @index = 0
11
+ else
12
+ @index = nil
13
+ end
14
+ end
15
+
16
+ def pick
17
+ if @index.nil?
18
+ return @servers&.first
19
+ end
20
+ @servers[@index] or raise "bug!!!"
21
+ end
22
+ def server; pick; end
23
+
24
+ def next!
25
+ inc_index
26
+ if @servers[@index].nil?
27
+ reset
28
+ return
29
+ end
30
+ end
31
+
32
+ def reset
33
+ @index = 0
34
+ end
35
+
36
+ def inc_index
37
+ @index = @index + 1
38
+ end
39
+ end
40
+
41
+ def reset_server_pool!
42
+ @pool_server = nil
43
+ end
44
+
45
+ def pool_server
46
+ @pool_server ||= ServerPool.new(
47
+ servers: servers
48
+ )
49
+ end
50
+
51
+ def servers
52
+ raise NotImplementedError, nil
53
+ end
54
+ end
@@ -0,0 +1,70 @@
1
+ module ProconBypassMan
2
+ module Background
3
+ class HttpClient
4
+ class HttpRequest
5
+ def self.request!(uri: , hostname: , body: , device_id: , session_id: nil, event_type: )
6
+ @uri = uri
7
+ @http = Net::HTTP.new(uri.host, uri.port)
8
+ @http.use_ssl = uri.scheme === "https"
9
+ @params = {
10
+ hostname: hostname,
11
+ event_type: event_type,
12
+ session_id: session_id,
13
+ device_id: device_id,
14
+ }.merge(body)
15
+ @http.post(
16
+ @uri.path,
17
+ @params.to_json,
18
+ { "Content-Type" => "application/json" },
19
+ )
20
+ end
21
+ end
22
+
23
+ def initialize(path: , pool_server: , retry_on_connection_error: false)
24
+ @path = path
25
+ @pool_server = pool_server
26
+ @hostname = `hostname`.chomp
27
+ @retry_on_connection_error = retry_on_connection_error
28
+ end
29
+
30
+ def post(body: , event_type: )
31
+ if @pool_server.server.nil?
32
+ ProconBypassMan.logger.info('送信先が未設定なのでスキップしました')
33
+ return
34
+ end
35
+ if body.is_a?(Hash)
36
+ body = { body: body, event_type: event_type }
37
+ else
38
+ body = { body: { value: body, event_type: event_type } }
39
+ end
40
+ session_id = ProconBypassMan.session_id
41
+ device_id = ProconBypassMan.device_id
42
+
43
+ response = HttpRequest.request!(
44
+ uri: URI.parse("#{@pool_server.server}#{@path}"),
45
+ hostname: @hostname,
46
+ device_id: device_id,
47
+ session_id: session_id,
48
+ body: body,
49
+ event_type: event_type,
50
+ )
51
+ case response.code
52
+ when /^200/
53
+ return
54
+ else
55
+ @pool_server.next!
56
+ ProconBypassMan.logger.error("200以外(#{response.code})が帰ってきました. #{response.body}")
57
+ end
58
+ rescue SocketError => e
59
+ ProconBypassMan.logger.error("error in outbound module: #{e}")
60
+ if @retry_on_connection_error
61
+ sleep(10)
62
+ retry
63
+ end
64
+ rescue => e
65
+ puts e
66
+ ProconBypassMan.logger.error("error in outbound module: #{e}")
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,16 @@
1
+ module ProconBypassMan
2
+ module Background
3
+ class JobPerformer
4
+ def initialize(klass: , args: )
5
+ @klass = klass
6
+ @args = args
7
+ end
8
+
9
+ def perform
10
+ @klass.perform(*@args)
11
+ rescue => e
12
+ ProconBypassMan::ReportErrorJob.perform(e)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module ProconBypassMan
2
+ module Background
3
+ module JobRunnable
4
+ def perform(*)
5
+ raise NotImplementedError, nil
6
+ end
7
+
8
+ def perform_async(*args)
9
+ ProconBypassMan::Background::JobRunner.push(
10
+ args: args,
11
+ reporter_class: self,
12
+ )
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,44 @@
1
+ module ProconBypassMan
2
+ module Background
3
+ class JobRunner
4
+ MAX_QUEUE_SIZE = 100
5
+
6
+ def self.start!
7
+ new.start!
8
+ end
9
+
10
+ # for test
11
+ def self.stop!
12
+ return unless defined?(@@thread)
13
+ @@thread.kill
14
+ end
15
+
16
+ def start!
17
+ return if defined?(@@thread)
18
+ @@thread = Thread.new do
19
+ while(item = self.class.queue.pop)
20
+ begin
21
+ JobPerformer.new(klass: item[:reporter_class], args: item[:args]).perform
22
+ sleep(1)
23
+ rescue => e
24
+ ProconBypassMan.logger.error(e)
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ def self.queue
31
+ @@queue ||= Queue.new
32
+ end
33
+
34
+ def self.push(hash)
35
+ if queue.size > MAX_QUEUE_SIZE
36
+ ProconBypassMan.logger.error('Over queue size cap!!')
37
+ return
38
+ end
39
+
40
+ queue.push(hash)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,12 @@
1
+ class ProconBypassMan::BaseJob
2
+ extend ProconBypassMan::Background::HasServerPool
3
+ extend ProconBypassMan::Background::JobRunnable
4
+
5
+ def self.servers
6
+ ProconBypassMan.config.api_servers
7
+ end
8
+
9
+ def self.path
10
+ "/api/events"
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ class ProconBypassMan::ReportBootJob < ProconBypassMan::BaseJob
2
+ # @param [String] body
3
+ def self.perform(body)
4
+ ProconBypassMan::Background::HttpClient.new(
5
+ path: path,
6
+ pool_server: pool_server,
7
+ retry_on_connection_error: true,
8
+ ).post(body: body, event_type: :boot)
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class ProconBypassMan::ReportErrorJob < ProconBypassMan::BaseJob
2
+ # @param [String] body
3
+ def self.perform(body)
4
+ ProconBypassMan::Background::HttpClient.new(
5
+ path: path,
6
+ pool_server: pool_server,
7
+ retry_on_connection_error: false,
8
+ ).post(body: body, event_type: :error,)
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class ProconBypassMan::ReportHeartbeatJob < ProconBypassMan::BaseJob
2
+ # @param [String] body
3
+ def self.perform(body)
4
+ ProconBypassMan::Background::HttpClient.new(
5
+ path: path,
6
+ pool_server: pool_server,
7
+ retry_on_connection_error: false,
8
+ ).post(body: body, event_type: :heartbeat)
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+ class ProconBypassMan::ReportPressedButtonsJob
2
+ extend ProconBypassMan::Background::HasServerPool
3
+ extend ProconBypassMan::Background::JobRunnable
4
+
5
+ PATH = "/api/pressed_buttons"
6
+
7
+ # @param [String] body
8
+ def self.perform(body)
9
+ ProconBypassMan::Background::HttpClient.new(
10
+ path: PATH,
11
+ pool_server: pool_server,
12
+ ).post(body: body, event_type: :internal)
13
+ end
14
+
15
+ def self.servers
16
+ ProconBypassMan.config.internal_api_servers
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ class ProconBypassMan::ReportReloadConfigJob < ProconBypassMan::BaseJob
2
+ # @param [String] body
3
+ def self.perform(body)
4
+ ProconBypassMan::Background::HttpClient.new(
5
+ path: path,
6
+ pool_server: pool_server,
7
+ retry_on_connection_error: false,
8
+ ).post(body: body, event_type: :reload_config)
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require "procon_bypass_man/background/job_runnable"
2
+ require "procon_bypass_man/background/job_performer"
3
+ require "procon_bypass_man/background/has_server_pool"
4
+ require "procon_bypass_man/background/http_client"
5
+ require "procon_bypass_man/background/jobs/base_job"
6
+ require "procon_bypass_man/background/jobs/report_boot_job"
7
+ require "procon_bypass_man/background/jobs/report_reload_config_job"
8
+ require "procon_bypass_man/background/jobs/report_error_job"
9
+ require "procon_bypass_man/background/jobs/report_heartbeat_job"
10
+ require "procon_bypass_man/background/jobs/report_pressed_buttons_job"