Sha256: e3271a08947a2cc5ab3a4c25d556c737a994c527747cc1c2b18e77e10053595b

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

#!/usr/bin/env ruby
# encoding: utf-8
# frozen_string_literal: true

lib = File.expand_path('../../lib', __FILE__)
$LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)

require 'netsoul/client'

$stderr.sync = true
require 'optparse'
require 'yaml'

process_name_string = "#{__FILE__} #{ARGV.join(' '.freeze)}".freeze
Process.respond_to?(:setproctitle) ? Process.setproctitle(process_name_string) : $PROGRAM_NAME = process_name_string

options = {}
OptionParser.new do |opts|
  opts.banner = 'Usage: netsoul-ruby [options]'.freeze
  opts.separator ''.freeze
  opts.separator 'Netsoul-Ruby options:'.freeze

  opts.on('-c'.freeze, '--config FILE'.freeze, 'Configuration file in YAML'.freeze) do |file|
    options[:user_opts] = YAML.load_file(file) if File.file?(file)
  end

  opts.on('-h'.freeze, '--help'.freeze, 'Display this screen'.freeze) do
    puts opts
    exit 42
  end
end.parse!

if options.empty? || options[:user_opts].size == 0
  unless ENV.to_a.count { |k, _v| %w(NETSOUL_LOGIN NETSOUL_SOCKS_PASSWORD NETSOUL_LOGIN NETSOUL_UNIX_PASSWORD NETSOUL_AUTH_METHOD).include?(k) } >= 2
    puts '[ERROR] You have to specify a configuration file or environment variables'.freeze
    exit 42
  end
end

retry_count = 10
retry_wait_time = 1.0
RETRY_WAIT_FACTOR = 2.0 # Each time retry is called in Exception block, current 'retry_wait_time' is increased with this factor
begin
  c = Netsoul::Client.new options[:user_opts]
  c.connect
  if c.started
    retry_count = 10
    retry_wait_time = 1.0
    loop do
      res = c.get
      c.send res if res.to_s.match(/^ping.*/)
      sleep 1
    end
  end
rescue Interrupt
  begin c.disconnect; end
  puts '!!! [SIGINT] !!!'.freeze
  exit 42
rescue => e
  puts "[ERROR]: #{e}"
  puts "[RETRY_COUNT]: #{retry_count}"
  begin c.disconnect; end
  if retry_count > 0
    retry_count -= 1
    retry_wait_time *= RETRY_WAIT_FACTOR
    sleep(retry_wait_time)
    retry
  end
  exit 42
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
netsoul-2.3.5 bin/netsoul-ruby
netsoul-2.3.4 bin/netsoul-ruby