# frozen_string_literal: true require "websocket-client-simple" require "thor" require "colorize" require_relative "../session" require_relative "./constants" module NeetoDeploy class CLI module Logs class Base < CLI::Base include Constants include Session attr_reader :app_name, :process_type def initialize(app_name, process_type = nil) super() @app_name = app_name @process_type = process_type end def process! stream_logs end private def uri "wss://connect.neetodeploy.com/cable" end def ensure_app_access? response = send_get_request(app_verify_access_url, { app_slug: app_name }) @ui.error(JSON[response.body]["error"]) unless response.success? response.success? end def stream_logs ws_connection = WebSocket::Client::Simple.connect(uri) session_token = common_body[:session_token] app_slug = @app_name process_type = @process_type pubsub_token = SecureRandom.hex(16) ws_connection.on :open do payload = { command: "subscribe", identifier: { channel: "Cli::LogChannel", app_slug:, pubsub_token:, process_type:, session_token: }.to_json } ws_connection.send(payload.to_json) end ws_connection.on :message do |msg| unless msg.data.empty? parsed_msg = JSON.parse(msg.data) if parsed_msg["type"].nil? parsed_msg["message"]&.each do |log| STDOUT.write "#{Time.at(log[0].to_i / 1e9).to_s.light_magenta} #{log[1]}\n" end elsif parsed_msg["type"] == "disconnect" puts parsed_msg["error"].red exit end end end ws_connection.on :close do |e| p e unless e.nil? exit 1 end ws_connection.on :error do |e| p e puts "MyserverBackend>> Close entered. Last error:#{$!.class}:#{$!.to_s};Module:#{$0};" $@.each { |backtrace| puts backtrace } exit 1 end loop do end end end end end end