Sha256: 8bf220c2844e85fd8bb9dda94e03166cc12d9eec9c942b94fde5d585ec4023df

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

class Jets::PreheatJob < ApplicationJob
  ENABLED = Jets.config.prewarm.enable
  CONCURRENCY = Jets.config.prewarm.concurrency
  PREWARM_RATE = Jets.config.prewarm.rate
  torching = ENABLED && CONCURRENCY > 1
  warming = ENABLED && CONCURRENCY == 1

  class_timeout 30
  class_memory 1024
  class_iam_policy(Jets.config.preheat_job_iam_policy)

  rate(PREWARM_RATE) if torching
  def torch
    threads = []
    CONCURRENCY.times do
      threads << Thread.new do
        # intentionally calling remote lambda for concurrency
        # avoid passing the _prewarm=1 flag because we want the PreheatJob#warm
        # to run it's normal workload.
        # Do not use Jets::Preheat.warm(function_name) here as that passes the _prewarm=1.
        function_name = "jets-preheat_job-warm"
        event_json = JSON.dump(event)
        options = call_options(event[:quiet])
        Jets::Commands::Call.new(function_name, event_json, options).run unless Jets.env.test?
      end
    end
    threads.each { |t| t.join }
    "Finished prewarming your application with a concurrency of #{CONCURRENCY}."
  end

  rate(PREWARM_RATE) if warming
  def warm
    options = call_options(event[:quiet])
    Jets::Preheat.warm_all(options)
    "Finished prewarming your application."
  end

  class << self
    # Can use this to prewarm post deploy
    def prewarm!
      meth = CONCURRENCY > 1 ? :torch : :warm
      perform_now(meth, quiet: true)
    end
  end

private
  def call_options(quiet)
    options = {}
    options.merge!(mute: true, mute_output: true) if quiet
    # All the methods in this Job class leads to Jets::Commands::Call.
    # This is true for the Jets::Preheat.warm_all also.
    # These jobs delegate out to Lambda function calls. We do not need/want
    # the invocation type: RequestResponse in this case.
    options.merge!(invocation_type: "Event")
    options
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jets-4.0.5 lib/jets/internal/app/jobs/jets/preheat_job.rb
jets-4.0.4 lib/jets/internal/app/jobs/jets/preheat_job.rb