Class: Jets::Preheat

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/jets/preheat.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Preheat

Returns a new instance of Preheat.



18
19
20
21
# File 'lib/jets/preheat.rb', line 18

def initialize(options)
  @options = options # passed to Call.new options
  @options[:mute_output] = true if @options[:mute_output].nil?
end

Class Method Details

.warm(function_name, options = {}) ⇒ Object

Examples:

Jets::Preheat.warm("posts_controller-index")
Jets::Preheat.warm("jets-preheat_job-warm")


10
11
12
# File 'lib/jets/preheat.rb', line 10

def self.warm(function_name, options={})
  Preheat.new(options).warm(function_name)
end

.warm_all(options = {}) ⇒ Object



14
15
16
# File 'lib/jets/preheat.rb', line 14

def self.warm_all(options={})
  Preheat.new(options).warm_all
end

Instance Method Details

#all_functionsObject

Returns:

[
  "posts_controller-index",
  "posts_controller-show",
  ...
]


70
71
72
73
74
75
76
77
78
79
# File 'lib/jets/preheat.rb', line 70

def all_functions
  classes.map do |klass|
    tasks = klass.tasks.select { |t| t.lang == :ruby } # only prewarm ruby functions
    tasks.map do |task|
      meth = task.meth
      underscored = klass.to_s.underscore.gsub('/','-')
      "#{underscored}-#{meth}" # function_name
    end
  end.flatten.uniq.compact
end

#classesObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/jets/preheat.rb', line 82

def classes
  Jets::Commands::Build.app_files.map do |path|
    next if path.include?("preheat_job.rb") # dont want to cause an infinite loop
    next if path =~ %r{app/functions} # dont support app/functions

    class_path = path.sub(%r{.*app/\w+/},'').sub(/\.rb$/,'')
    class_name = class_path.classify
    # IE: PostsController
    class_name.constantize # load app/**/* class definition
  end.compact
end

#warm(function_name) ⇒ Object

Makes remote call to the Lambda function.



24
25
26
# File 'lib/jets/preheat.rb', line 24

def warm(function_name)
  Jets::Commands::Call.new(function_name, '{"_prewarm": "1"}', @options).run unless ENV['TEST']
end

#warm_allObject

Loop through all methods for each class and makes special prewarm call to each method.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jets/preheat.rb', line 29

def warm_all
  threads = []
  all_functions.each do |function_name|
    threads << Thread.new do
      warm(function_name)
    end
  end
  threads.each { |t| t.join }

  # Warm the jets-public_controller more since it gets called more naturally
  warm_public_controller_more

  # return the funciton names so we can see in the Lambda console
  # the functions being prewarmed
  all_functions
end

#warm_public_controller_moreObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jets/preheat.rb', line 46

def warm_public_controller_more
  function_name = 'jets-public_controller-show' # key function name
  return unless all_functions.include?(function_name)

  public_ratio = Jets.config.prewarm.public_ratio
  return if public_ratio == 0

  puts "Prewarming the public controller extra at a ratio of #{public_ratio}" unless @options[:mute]

  threads = []
  public_ratio.times do
    threads << Thread.new do
      warm(function_name)
    end
  end
  threads.each { |t| t.join }
end