Module: ProconBypassMan::Websocket::Client

Defined in:
lib/procon_bypass_man/websocket/client.rb

Constant Summary collapse

CHANNEL =
'PbmJobChannel'

Class Method Summary collapse

Class Method Details

.dispatch(data:, client:) ⇒ Object

Parameters:

  • data (Hash)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/procon_bypass_man/websocket/client.rb', line 63

def self.dispatch(data: , client: )
  case data.dig("message")['action']
  when "ping"
    client.perform('pong', { device_id: ProconBypassMan.device_id, message: 'hello from pbm' })
  when ProconBypassMan::RemoteAction::ACTION_MACRO
    run_remote_macro(data: data)
  when *ProconBypassMan::RemoteAction::RemotePbmJob::ACTIONS_IN_MASTER_PROCESS
    run_remote_pbm_job(data: data, process_to_execute: :master)
  when *ProconBypassMan::RemoteAction::RemotePbmJob::ACTIONS_IN_BYPASS_PROCESS
    run_remote_pbm_job(data: data, process_to_execute: :bypass)
  else
    ProconBypassMan::SendErrorCommand.execute(error: 'unknown remote pbm action')
  end
end

.run(watchdog:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/procon_bypass_man/websocket/client.rb', line 16

def self.run(watchdog: )
  EventMachine.run do
    client = ActionCableClient.new(
      ProconBypassMan.config.current_ws_server_url, {
        channel: CHANNEL, device_id: ProconBypassMan.device_id,
      }
    )

    client.connected {
      ProconBypassMan.logger.info('[WebsocketClient] successfully connected in ProconBypassMan::Websocket::Client')
    }
    client.subscribed { |msg|
      ProconBypassMan.logger.info("[WebsocketClient] subscribed(#{msg})")
      ProconBypassMan::SyncDeviceStatsJob.perform(ProconBypassMan::DeviceStatus.current)
    }

    client.received do |data|
      ProconBypassMan.logger.info('[WebsocketClient] received!!')
      ProconBypassMan.logger.info(data)

      dispatch(data: data, client: client)
    rescue => e
      ProconBypassMan::SendErrorCommand.execute(error: e)
    end

    client.disconnected {
      ProconBypassMan.logger.info('[WebsocketClient] disconnected!!')
      client.reconnect!
      sleep 2
    }
    client.errored { |msg|
      ProconBypassMan.logger.error("[WebsocketClient] errored!!, #{msg}")
      client.reconnect!
      sleep 2
    }
    client.pinged { |msg|
      watchdog.active!

      ProconBypassMan.cache.fetch key: 'ws_pinged', expires_in: 10 do
        ProconBypassMan.logger.debug('[WebsocketClient] pinged!!')
        ProconBypassMan.logger.debug(msg)
      end
    }
  end
end

.run_remote_macro(data:) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/procon_bypass_man/websocket/client.rb', line 115

def self.run_remote_macro(data: )
  pbm_job_hash = data.dig("message")
  begin
    remote_action_object = ProconBypassMan::RemoteAction::RemoteActionObject.new(name: pbm_job_hash["name"],
                                                                              uuid: pbm_job_hash["uuid"],
                                                                              steps: pbm_job_hash["steps"])
    remote_action_object.validate!
  rescue ProconBypassMan::RemoteAction::RemoteActionObject::ValidationError => e
    ProconBypassMan::SendErrorCommand.execute(error: e)
    return
  end

  # TODO: インラインしたい
  ProconBypassMan::RemoteActionSender.execute(
    name: remote_action_object.name,
    uuid: remote_action_object.uuid,
    steps: remote_action_object.steps,
    type: ProconBypassMan::RemoteAction::Task::TYPE_MACRO,
  )
end

.run_remote_pbm_job(data:, process_to_execute:) ⇒ Void

Parameters:

  • data (Hash)
  • process_to_execute (Symbol)

    どのプロセスで実行するか

Returns:

  • (Void)

Raises:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/procon_bypass_man/websocket/client.rb', line 82

def self.run_remote_pbm_job(data: , process_to_execute: )
  pbm_job_hash = data.dig("message")
  begin
    pbm_job_object = ProconBypassMan::RemotePbmJobObject.new(action: pbm_job_hash["action"],
                                                                status: pbm_job_hash["status"],
                                                                uuid: pbm_job_hash["uuid"],
                                                                created_at: pbm_job_hash["created_at"],
                                                                job_args: pbm_job_hash["args"])
    pbm_job_object.validate!
  rescue ProconBypassMan::RemotePbmJobObject::ValidationError => e
    ProconBypassMan::SendErrorCommand.execute(error: e)
    return
  end

  case process_to_execute
  when :master
    ProconBypassMan::RemoteAction::RemotePbmJob::RunRemotePbmJobDispatchCommand.execute(
      action: pbm_job_object.action,
      uuid: pbm_job_object.uuid,
      job_args: pbm_job_object.job_args
    )
  when :bypass
    ProconBypassMan::RemoteAction::QueueOverProcess.push(
      ProconBypassMan::RemoteAction::Task.new(pbm_job_object.action,
                                             pbm_job_object.uuid,
                                             pbm_job_object.job_args,
                                             ProconBypassMan::RemoteAction::Task::TYPE_ACTION)
    )
  else
    ProconBypassMan::SendErrorCommand.execute(error: 'unknown process to execute')
  end
end

.start!Object



6
7
8
9
10
11
12
13
14
# File 'lib/procon_bypass_man/websocket/client.rb', line 6

def self.start!
  return unless ProconBypassMan.config.enable_ws?

  Thread.start do
    ProconBypassMan::Forever.run do |watchdog|
      run(watchdog: watchdog)
    end
  end
end