Module: Jets::Core

Extended by:
Memoist
Included in:
Jets
Defined in:
lib/jets/core.rb

Constant Summary collapse

ENV_MAP =
{
  development: 'dev',
  production: 'prod',
  staging: 'stag',
}
@@extra_warning_shown =
false
@@call_count =

NOTE: In development this will always be 1 because the app gets reloaded. On AWS Lambda, this will be ever increasing until the container gets replaced.

0
@@prewarm_count =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#app_classObject

Returns the value of attribute app_class.



11
12
13
# File 'lib/jets/core.rb', line 11

def app_class
  @app_class
end

#applicationObject



12
13
14
# File 'lib/jets/core.rb', line 12

def application
  @application ||= (app_class.instance if app_class)
end

Instance Method Details

#autoloadersObject



253
254
255
# File 'lib/jets/core.rb', line 253

def autoloaders
  application.autoloaders
end

#backtrace_cleanerObject



16
17
18
# File 'lib/jets/core.rb', line 16

def backtrace_cleaner
  @backtrace_cleaner ||= Jets::BacktraceCleaner.new
end

#bootObject

Load all application base classes and project classes



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

def boot
  Jets::Booter.boot!
end

#build_rootObject



106
107
108
# File 'lib/jets/core.rb', line 106

def build_root
  "/tmp/jets/#{Jets.project_name}".freeze
end

#call_countObject



139
140
141
# File 'lib/jets/core.rb', line 139

def call_count
  @@call_count
end

#configurationObject

The Configuration instance used to configure the Rails environment



21
22
23
# File 'lib/jets/core.rb', line 21

def configuration
  application.config
end

#custom_domain?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/jets/core.rb', line 157

def custom_domain?
  Jets.config.domain.hosted_zone_name
end

#deprecatorObject

:nodoc:



119
120
121
# File 'lib/jets/core.rb', line 119

def deprecator # :nodoc:
  @deprecator ||= ActiveSupport::Deprecation.new
end

#eager_load_gem?Boolean

It’s useful to eager load and find out any error within the jets code immediately. Leaving in place because think the layer of protection is good. Eager load outside of a jets project can error. IE: ‘jets -h` Eager load inside a jets project is fine.

Returns:

  • (Boolean)


261
262
263
# File 'lib/jets/core.rb', line 261

def eager_load_gem?
  File.exist?("config/application.rb") || ENV['JETS_TEST'] || defined?(ENGINE_ROOT) # jets project
end

#envObject



31
32
33
34
35
# File 'lib/jets/core.rb', line 31

def env
  env = ENV['JETS_ENV'] || 'development'
  ENV['RAILS_ENV'] = ENV['RACK_ENV'] = env
  ActiveSupport::StringInquirer.new(env)
end

#errorObject

Returns the ActiveSupport::ErrorReporter of the current Jets project, otherwise it returns nil if there is no project.

Jets.error.handle(IOError) do
  # ...
end
Jets.error.report(error)


239
240
241
242
# File 'lib/jets/core.rb', line 239

def error
  application && application.executor.error_reporter
  # ActiveSupport.error_reporter
end

#extraObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/jets/core.rb', line 39

def extra
  # Keep for backwards compatibility
  unless ENV['JETS_ENV_EXTRA'].blank?
    puts "DEPRECATION WARNING: JETS_ENV_EXTRA is deprecated. Use JETS_EXTRA instead.".color(:yellow) unless @@extra_warning_shown
    @@extra_warning_shown = true
    extra = ENV['JETS_ENV_EXTRA']
  end
  extra = ENV['JETS_EXTRA'] unless ENV['JETS_EXTRA'].blank?
  extra
end

#gem_layer?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/jets/core.rb', line 210

def gem_layer?
  !Jets.poly_only? || Jets.one_lambda_for_all_controllers?
end

#increase_call_countObject



135
136
137
# File 'lib/jets/core.rb', line 135

def increase_call_count
  @@call_count += 1
end

#increase_prewarm_countObject



144
145
146
# File 'lib/jets/core.rb', line 144

def increase_prewarm_count
  @@prewarm_count += 1
end

#loggerObject



111
112
113
# File 'lib/jets/core.rb', line 111

def logger
  @logger
end

#logger=(logger) ⇒ Object



115
116
117
# File 'lib/jets/core.rb', line 115

def logger=(logger)
  @logger = logger
end

#onceObject



175
176
177
178
179
180
# File 'lib/jets/core.rb', line 175

def once
  boot
  override_lambda_ruby_runtime
  # require "jets/overrides/puma" # leaving around as a comment in case needed in the future
  tmp_load!
end

#one_lambda_for_all_controllers?Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/jets/core.rb', line 206

def one_lambda_for_all_controllers?
  Jets.config.cfn.build.controllers == "one_lambda_for_all_controllers"
end

#one_lambda_per_controller?Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/jets/core.rb', line 202

def one_lambda_per_controller?
  Jets.config.cfn.build.controllers == "one_lambda_per_controller"
end

#override_lambda_ruby_runtimeObject



186
187
188
# File 'lib/jets/core.rb', line 186

def override_lambda_ruby_runtime
  require "jets/overrides/lambda"
end

#parsed_project_nameObject

Double evaling config/application.rb causes subtle issues:

* double loading of shared resources: Jets::Stack.subclasses will have the same
class twice when config is called when declaring a function
* forces us to rescue all exceptions, which is a big hammer

Lets parse for the project name instead for now.

Keep for backwards compatibility



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jets/core.rb', line 88

def parsed_project_name
  lines = IO.readlines("#{Jets.root}/config/application.rb")
  project_name_line = lines.find { |l| l =~ /config\.project_name.*=/ && l !~ /^\s+#/ }
  if project_name_line
    parsed = project_name_line.gsub(/.*=/,'').strip
    # The +? makes it non-greedy
    # See: https://ruby-doc.org/core-2.5.1/Regexp.html#class-Regexp-label-Repetition
    md = parsed.match(/['"](.+?)['"]/)
    md ? md[1] : raise("Unable to parse project name from config/application.rb: #{project_name_line}")
  end
end

#poly_only?Boolean

Returns:

  • (Boolean)


152
153
154
155
# File 'lib/jets/core.rb', line 152

def poly_only?
  return true if ENV['JETS_POLY_ONLY'] # bypass to allow rapid development of handlers
  Jets::Cfn::Builder.poly_only?
end

#prewarm_countObject



148
149
150
# File 'lib/jets/core.rb', line 148

def prewarm_count
  @@prewarm_count
end

#process(event, context, handler) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/jets/core.rb', line 165

def process(event, context, handler)
  if event['_prewarm']
    Jets.increase_prewarm_count
    Jets.logger.info("Prewarm request")
    {prewarmed_at: Time.now.to_s}
  else
    Jets::Processors::MainProcessor.new(event, context, handler).run
  end
end

#project_nameObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jets/core.rb', line 50

def project_name
  path = "config/project_name"
  if ENV['JETS_PROJECT_NAME'] && !ENV['JETS_PROJECT_NAME'].blank?
    ENV['JETS_PROJECT_NAME']
  elsif File.exist?(path)
    IO.read(path).strip
  elsif parsed_project_name
    parsed_project_name
  else
    Dir.pwd.split("/").last # conventionally infer app name from current directory
  end
end

#project_namespaceObject



63
64
65
# File 'lib/jets/core.rb', line 63

def project_namespace
  [project_name, short_env, extra].compact.join('-').gsub('_','-')
end

#public_pathObject

Returns a Pathname object of the public folder of the current Jets project, otherwise it returns nil if there is no project:

Jets.public_path
  # => #<Pathname:/Users/someuser/some/path/project/public>


249
250
251
# File 'lib/jets/core.rb', line 249

def public_path
  application && Pathname.new(application.paths["public"].first)
end

#report_exception(exception) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/jets/core.rb', line 219

def report_exception(exception)
  # See Jets::ExceptionReporting decorate_exception_with_exception_reported!
  if exception.respond_to?(:with_exception_reported?) && exception.with_exception_reported?
    return
  end

  Jets.application.turbines.each do |turbine|
    turbine.on_exception_blocks.each do |block|
      block.call(exception)
    end
  end
end

#rootObject



25
26
27
28
29
# File 'lib/jets/core.rb', line 25

def root
  root = ENV['JETS_ROOT'].to_s
  root = Dir.pwd if root == ''
  Pathname.new(root)
end

#ruby_folderObject



190
191
192
# File 'lib/jets/core.rb', line 190

def ruby_folder
  RUBY_VERSION.split('.')[0..1].join('.') + '.0'
end

#ruby_runtimeObject

used to configure internal lambda functions current ruby runtime that user is running IE: ruby2.5 ruby2.7



197
198
199
200
# File 'lib/jets/core.rb', line 197

def ruby_runtime
  version = RUBY_VERSION.split('.')[0..1].join('.')
  "ruby#{version}"
end

#s3_bucketObject

Do not memoize here. The JetsBucket.name does it’s own special memoization.



215
216
217
# File 'lib/jets/core.rb', line 215

def s3_bucket
  Jets::Cfn::Resource::S3::JetsBucket.name
end

#s3_events?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/jets/core.rb', line 161

def s3_events?
  !Jets::Job::Base._s3_events.empty?
end

#short_envObject



76
77
78
# File 'lib/jets/core.rb', line 76

def short_env
  ENV_MAP[Jets.env.to_sym] || Jets.env
end

#table_namespaceObject



67
68
69
# File 'lib/jets/core.rb', line 67

def table_namespace
  [project_name, short_env].compact.join('-')
end

#tmp_load!Object



182
183
184
# File 'lib/jets/core.rb', line 182

def tmp_load!
  Jets::TmpLoader.load!
end

#versionObject



128
129
130
# File 'lib/jets/core.rb', line 128

def version
  Jets::VERSION
end

#webpacker?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/jets/core.rb', line 123

def webpacker?
  Gem.loaded_specs.keys.any?{|k| k.start_with?("webpacker")}
end