Sha256: 5cf535b782630fb54cb2505ee3e2e23c92c7217dc0a35eaabb2176ed1532679c

Contents?: true

Size: 1.84 KB

Versions: 5

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::Application.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

5 entries across 5 versions & 1 rubygems

Version Path
jets-4.0.10 lib/jets/internal/app/jobs/jets/preheat_job.rb
jets-4.0.9 lib/jets/internal/app/jobs/jets/preheat_job.rb
jets-4.0.8 lib/jets/internal/app/jobs/jets/preheat_job.rb
jets-4.0.7 lib/jets/internal/app/jobs/jets/preheat_job.rb
jets-4.0.6 lib/jets/internal/app/jobs/jets/preheat_job.rb