Module: Jets::Core

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

Constant Summary collapse

@@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 Method Summary collapse

Instance Method Details

#applicationObject



4
5
6
# File 'lib/jets/core.rb', line 4

def application
  Jets::Application.instance
end

#awsObject



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

def aws
  application.aws
end

#boot(options = {}) ⇒ Object

Load all application base classes and project classes



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

def boot(options={})
  Jets::Booter.boot!(options)
end

#build_rootObject



35
36
37
# File 'lib/jets/core.rb', line 35

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

#call_countObject



131
132
133
# File 'lib/jets/core.rb', line 131

def call_count
  @@call_count
end

#class_mappings(class_name) ⇒ Object



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

def class_mappings(class_name)
  map = {
    "Jets::Io" => "Jets::IO",
  }
  map[class_name] || class_name
end

#configObject



8
9
10
# File 'lib/jets/core.rb', line 8

def config
  application.config
end

#custom_domain?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/jets/core.rb', line 172

def custom_domain?
  Jets.config.domain.hosted_zone_name
end

#default_gems_sourceObject



204
205
206
# File 'lib/jets/core.rb', line 204

def default_gems_source
  "https://gems2.lambdagems.com"
end

#eager_load!Object



58
59
60
61
# File 'lib/jets/core.rb', line 58

def eager_load!
  eager_load_jets
  eager_load_app
end

#eager_load_appObject

Eager load user’s application



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/jets/core.rb', line 108

def eager_load_app
  Dir.glob("#{Jets.root}/app/**/*.rb").select do |path|
    next if !File.file?(path) or path =~ %r{/javascript/} or path =~ %r{/views/}
    next if path.include?('app/functions') || path.include?('app/shared/functions') || path.include?('app/internal/functions')

    class_name = path
                  .sub(/\.rb$/,'') # remove .rb
                  .sub(%{^\./},'') # remove ./
                  .sub("#{Jets.root}/",'')
                  .sub(%r{app/shared/\w+/},'') # remove shared/resources or shared/extensions
                  .sub(%r{app/\w+/},'') # remove app/controllers or app/jobs etc
    class_name = class_name.classify
    class_name.constantize # use constantize instead of require so dont have to worry about order.
  end
end

#eager_load_jetsObject

Eager load jet’s lib and classes



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jets/core.rb', line 64

def eager_load_jets
  lib_jets = File.expand_path(".", File.dirname(__FILE__))
  Dir.glob("#{lib_jets}/**/*.rb").select do |path|
    next if !File.file?(path)
    next if skip_eager_load_paths?(path)

    path = path.sub("#{lib_jets}/","jets/")
    class_name = path
                  .sub(/\.rb$/,'') # remove .rb
                  .sub(/^\.\//,'') # remove ./
                  .sub(/app\/\w+\//,'') # remove app/controllers or app/jobs etc
                  .camelize
    # special class mappings
    class_name = class_mappings(class_name)
    class_name.constantize # use constantize instead of require so dont have to worry about order.
  end
end

#envObject



28
29
30
31
32
# File 'lib/jets/core.rb', line 28

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

#increase_call_countObject



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

def increase_call_count
  @@call_count += 1
end

#increase_prewarm_countObject



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

def increase_prewarm_count
  @@prewarm_count += 1
end

#load_tasksObject



50
51
52
# File 'lib/jets/core.rb', line 50

def load_tasks
  Jets::Commands::RakeTasks.load!
end

#loggerObject



40
41
42
# File 'lib/jets/core.rb', line 40

def logger
  Jets.application.config.logger
end

#on_exception(exception) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/jets/core.rb', line 163

def on_exception(exception)
  Jets::Turbine.subclasses.each do |subclass|
    reporters = subclass.on_exceptions || []
    reporters.each do |label, block|
      block.call(exception)
    end
  end
end

#onceObject



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

def once
  boot
  override_lambda_ruby_runtime
  tmp_load!
  start_rack_server
end

#override_lambda_ruby_runtimeObject



208
209
210
# File 'lib/jets/core.rb', line 208

def override_lambda_ruby_runtime
  require "jets/overrides/lambda"
end

#poly_only?Boolean

Returns:

  • (Boolean)


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

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

#prewarm_countObject



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

def prewarm_count
  @@prewarm_count
end

#process(event, context, handler) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'lib/jets/core.rb', line 176

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_namespaceObject



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

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

#rack?Boolean

Returns:

  • (Boolean)


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

def rack?
  path = "#{Jets.root}/rack"
  File.exist?(path) || File.symlink?(path)
end

#report_exception(exception) ⇒ Object



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

def report_exception(exception)
  puts "DEPRECATED: report_exception. Use on_exception instead.".color(:yellow)
  on_exception(exception)
end

#rootObject



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

def root
  # Do not memoize this method. Turbo mode can change it
  root = ENV['JETS_ROOT'].to_s
  root = Dir.pwd if root == ''
  Pathname.new(root)
end

#skip_eager_load_paths?(path) ⇒ Boolean

Skip these paths because eager loading doesnt work for them.

Returns:

  • (Boolean)


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

def skip_eager_load_paths?(path)
  path =~ %r{/cli} ||
  path =~ %r{/core_ext} ||
  path =~ %r{/default/application} ||
  path =~ %r{/functions} ||
  path =~ %r{/internal/app} ||
  path =~ %r{/jets/stack} ||
  path =~ %r{/overrides} ||
  path =~ %r{/rackup_wrappers} ||
  path =~ %r{/reconfigure_rails} ||
  path =~ %r{/templates/} ||
  path =~ %r{/turbo/project/} ||
  path =~ %r{/version} ||
  path =~ %r{/webpacker} ||
  path =~ %r{/jets/spec}
end

#start_rack_server(options = {}) ⇒ Object

Megamode support



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

def start_rack_server(options={})
  rack = Jets::RackServer.new(options)
  rack.start
  rack.wait_for_socket
end

#tmp_load!Object



193
194
195
# File 'lib/jets/core.rb', line 193

def tmp_load!
  Jets::TmpLoader.load!
end

#versionObject



54
55
56
# File 'lib/jets/core.rb', line 54

def version
  Jets::VERSION
end

#webpacker?Boolean

Returns:

  • (Boolean)


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

def webpacker?
  Gem.loaded_specs.keys.include?("webpacker")
end