Module: Executo

Defined in:
lib/executo/scheduler_worker.rb,
lib/executo.rb,
lib/executo/cli.rb,
lib/executo/worker.rb,
lib/executo/command.rb,
lib/executo/version.rb,
lib/executo/command_dsl.rb,
lib/executo/configuration.rb,
lib/executo/tagged_logger.rb,
lib/executo/encrypted_worker.rb,
lib/executo/feedback_process_job.rb,
lib/executo/feedback_process_service.rb

Overview

Defined Under Namespace

Modules: CommandDsl, TaggedLogger Classes: CLI, Command, Configuration, EncryptedWorker, Error, FeedbackProcessJob, FeedbackProcessService, SchedulerWorker, Worker

Constant Summary collapse

VERSION =
'0.3.12'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



28
29
30
# File 'lib/executo.rb', line 28

def config
  @config
end

Class Method Details

.active_job_connection_poolObject



102
103
104
# File 'lib/executo.rb', line 102

def active_job_connection_pool
  @active_job_connection_pool ||= ConnectionPool.new(size: 5, timeout: 5) { Redis.new(config.active_job_redis) }
end

.connection_poolObject



98
99
100
# File 'lib/executo.rb', line 98

def connection_pool
  @connection_pool ||= ConnectionPool.new(size: 5, timeout: 5) { Redis.new(config.redis) }
end

.cryptorObject



37
38
39
# File 'lib/executo.rb', line 37

def cryptor
  @cryptor ||= ActiveSupport::MessageEncryptor.new(ENV['EXECUTO_KEY'])
end

.decrypt(string) ⇒ Object



45
46
47
# File 'lib/executo.rb', line 45

def decrypt(string)
  cryptor.decrypt_and_verify(string)
end

.encrypt(obj) ⇒ Object



41
42
43
# File 'lib/executo.rb', line 41

def encrypt(obj)
  cryptor.encrypt_and_sign(obj)
end

.publish(target:, command:, parameters: [], encrypt: false, options: {}, job_options: {}, feedback: {}) ⇒ Object

Publishes a command to be executed on a target

Parameters:

  • target (String)

    a server or a role

  • command (String)

    command to be executed

  • params (Array)

    params for the command

  • encrypt (Boolean) (defaults to: false)

    whether to encrypt all parameters

  • options (Hash) (defaults to: {})

    options for the worker

  • job_options, (Hash)

    options for sidekiq, valid options are: queue - the named queue to use, default ‘default’ class - the worker class to call, required args - an array of simple arguments to the perform method, must be JSON-serializable at - timestamp to schedule the job (optional), must be Numeric (e.g. Time.now.to_f) retry - whether to retry this job if it fails, default true or an integer number of retries backtrace - whether to save any error backtrace, default false



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/executo.rb', line 64

def publish(target:, command:, parameters: [], encrypt: false, options: {}, job_options: {}, feedback: {})
  options['feedback'] = feedback&.stringify_keys
  options['feedback']['id'] ||= SecureRandom.uuid

  args = [command, parameters, options.deep_stringify_keys]
  args = args.map { |a| encrypt(a) } if encrypt

  sidekiq_options = { 'retry' => 0 }.merge(job_options).merge(
    'queue' => target,
    'class' => encrypt ? 'Executo::EncryptedWorker' : 'Executo::Worker',
    'args' => args
  )

  if defined?(Rails) && Rails.env.test?
    $executo_jobs ||= {}
    $executo_jobs[options.dig('feedback', 'id')] = sidekiq_options
  else
    Sidekiq::Client.new(connection_pool).push(sidekiq_options)
  end

  logger.info("Published #{command} to #{target} with id #{options['feedback']['id']}")
  options.dig('feedback', 'id')
end

.schedule(target, list) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/executo.rb', line 88

def schedule(target, list)
  options = {
    'retry' => 0,
    'queue' => target,
    'class' => 'Executo::SetScheduleWorker',
    'args' => list
  }
  Sidekiq::Client.new(connection_pool).push(options)
end

.setup {|config| ... } ⇒ Object

Yields:



32
33
34
35
# File 'lib/executo.rb', line 32

def setup
  @config = Configuration.new
  yield config
end