Module: Stealth

Defined in:
lib/stealth/cli.rb,
lib/stealth/base.rb,
lib/stealth/jobs.rb,
lib/stealth/lock.rb,
lib/stealth/redis.rb,
lib/stealth/reply.rb,
lib/stealth/errors.rb,
lib/stealth/logger.rb,
lib/stealth/server.rb,
lib/stealth/session.rb,
lib/stealth/version.rb,
lib/stealth/cli_base.rb,
lib/stealth/reloader.rb,
lib/stealth/flow/base.rb,
lib/stealth/dispatcher.rb,
lib/stealth/flow/state.rb,
lib/stealth/nlp/client.rb,
lib/stealth/nlp/result.rb,
lib/stealth/configuration.rb,
lib/stealth/helpers/redis.rb,
lib/stealth/service_reply.rb,
lib/stealth/controller/nlp.rb,
lib/stealth/commands/server.rb,
lib/stealth/scheduled_reply.rb,
lib/stealth/service_message.rb,
lib/stealth/commands/command.rb,
lib/stealth/commands/console.rb,
lib/stealth/migrations/tasks.rb,
lib/stealth/controller/helpers.rb,
lib/stealth/controller/replies.rb,
lib/stealth/flow/specification.rb,
lib/stealth/generators/builder.rb,
lib/stealth/controller/messages.rb,
lib/stealth/generators/generate.rb,
lib/stealth/controller/callbacks.rb,
lib/stealth/controller/catch_all.rb,
lib/stealth/controller/dev_jumps.rb,
lib/stealth/services/base_client.rb,
lib/stealth/controller/controller.rb,
lib/stealth/migrations/generators.rb,
lib/stealth/migrations/configurator.rb,
lib/stealth/controller/dynamic_delay.rb,
lib/stealth/migrations/railtie_config.rb,
lib/stealth/controller/interrupt_detect.rb,
lib/stealth/services/base_reply_handler.rb,
lib/stealth/services/base_message_handler.rb,
lib/stealth/controller/unrecognized_message.rb,
lib/stealth/services/jobs/handle_message_job.rb

Defined Under Namespace

Modules: CliBase, Commands, Flow, Generators, Migrations, Nlp, Redis, RedisSupport, Services, Version Classes: Cli, Configuration, Controller, Dispatcher, Errors, Jobs, Lock, Logger, RedisConfig, Reloader, Reply, ScheduledReplyJob, Server, ServiceMessage, ServiceReply, Session

Constant Summary collapse

VERSION =
Version.version

Class Method Summary collapse

Class Method Details

.bootObject



39
40
41
42
# File 'lib/stealth/base.rb', line 39

def self.boot
  load_services_config
  load_environment
end

.bot_reloaderObject



63
64
65
# File 'lib/stealth/base.rb', line 63

def self.bot_reloader
  @bot_reloader
end

.configObject



44
45
46
# File 'lib/stealth/base.rb', line 44

def self.config
  Thread.current[:configuration] ||= load_services_config
end

.configuration=(config) ⇒ Object



48
49
50
# File 'lib/stealth/base.rb', line 48

def self.configuration=(config)
  Thread.current[:configuration] = config
end

.default_autoload_pathsObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/stealth/base.rb', line 52

def self.default_autoload_paths
  [
    File.join(Stealth.root, 'bot', 'controllers', 'concerns'),
    File.join(Stealth.root, 'bot', 'controllers'),
    File.join(Stealth.root, 'bot', 'models', 'concerns'),
    File.join(Stealth.root, 'bot', 'models'),
    File.join(Stealth.root, 'bot', 'helpers'),
    File.join(Stealth.root, 'config')
  ]
end

.envObject



31
32
33
# File 'lib/stealth/base.rb', line 31

def self.env
  @env ||= ActiveSupport::StringInquirer.new(ENV['STEALTH_ENV'] || 'development')
end

.load_bot!Object



114
115
116
117
118
119
120
# File 'lib/stealth/base.rb', line 114

def self.load_bot!
  @bot_reloader ||= begin
    bot_reloader = Stealth::Reloader.new
    bot_reloader.load_bot!
    bot_reloader
  end
end

.load_environmentObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/stealth/base.rb', line 122

def self.load_environment
  require File.join(Stealth.root, 'config', 'boot')
  require_directory('config/initializers')

  load_bot!

  Sidekiq.configure_server do |config|
    config[:reloader] = Stealth.bot_reloader
  end

  if defined?(ActiveRecord)
    if ENV['DATABASE_URL'].present?
      ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])
    else
      database_config = File.read(File.join(Stealth.root, 'config', 'database.yml'))
      ActiveRecord::Base.establish_connection(
        YAML.load(ERB.new(database_config).result, aliases: true)[Stealth.env]
      )
    end
  end
end

.load_services_config(services_yaml = nil) ⇒ Object

Loads the services.yml configuration unless one has already been loaded



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/stealth/base.rb', line 85

def self.load_services_config(services_yaml=nil)
  @semaphore ||= Mutex.new
  services_yaml ||= Stealth.load_services_config(
    File.read(File.join(Stealth.root, 'config', 'services.yml'))
  )

  Thread.current[:configuration] ||= begin
    @semaphore.synchronize do
      services_config = YAML.safe_load(ERB.new(services_yaml).result, aliases: true)

      unless services_config.has_key?(env)
        raise Stealth::Errors::ConfigurationError, "Could not find services.yml configuration for #{env} environment"
      end

      config = Stealth::Configuration.new(services_config[env])
      set_config_defaults(config)

      config
    end
  end
end

.load_services_config!(services_yaml = nil) ⇒ Object

Same as ‘load_services_config` but forces the loading even if one has already been loaded



109
110
111
112
# File 'lib/stealth/base.rb', line 109

def self.load_services_config!(services_yaml=nil)
  Thread.current[:configuration] = nil
  load_services_config(services_yaml)
end

.require_directory(directory) ⇒ Object



148
149
150
# File 'lib/stealth/base.rb', line 148

def self.require_directory(directory)
  for_each_file_in(directory) { |file| require_relative(file) }
end

.rootObject



35
36
37
# File 'lib/stealth/base.rb', line 35

def self.root
  @root ||= File.expand_path(Pathname.new(Dir.pwd))
end

.set_config_defaults(config) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/stealth/base.rb', line 67

def self.set_config_defaults(config)
  defaults = {
    dynamic_delay_muliplier: 1.0,                     # values > 1 increase, values < 1 decrease delay
    session_ttl: 0,                                   # 0 seconds; don't expire sessions
    lock_autorelease: 30,                             # 30 seconds
    transcript_logging: false,                        # show user replies in the logs
    hot_reload: Stealth.env.development?,             # hot reload bot files on change (dev only)
    eager_load: Stealth.env.production?,              # eager load bot files for performance (prod only)
    autoload_paths: Stealth.default_autoload_paths,   # array of autoload paths used in eager and hot reloading
    autoload_ignore_paths: [],                        # paths to exclude from eager and hot reloading
    nlp_integration: nil,                             # NLP service to use, defaults to none
    log_all_nlp_results: false,                       # log NLP service requests; useful for debugging/improving NLP models
    auto_insert_delays: true                          # automatically insert delays/typing indicators between all replies
  }
  defaults.each { |option, default| config.set_default(option, default) }
end

.tidObject



144
145
146
# File 'lib/stealth/base.rb', line 144

def self.tid
  Thread.current.object_id.to_s(36)
end