Class: RailsCriticalCss::Jobs::Extractor

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
lib/rails_critical_css/jobs/extractor.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.perform_if_semaphore_is_released(attrs) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/rails_critical_css/jobs/extractor.rb', line 10

def perform_if_semaphore_is_released(attrs)
  semaphore = semaphore_key(attrs[:cache][:path])
  return if Rails.cache.exist?(semaphore)

  Rails.cache.write(semaphore, '1', { expires_in: 15.minutes })
  perform_later(attrs)
end

.semaphore_key(cache_path) ⇒ Object



6
7
8
# File 'lib/rails_critical_css/jobs/extractor.rb', line 6

def semaphore_key(cache_path)
  "semaphore-#{cache_path}"
end

Instance Method Details

#perform(cache:, css:, html:) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rails_critical_css/jobs/extractor.rb', line 19

def perform(cache:, css:, html:)
  semaphore = self.class.semaphore_key(cache[:path])
  critical_css = ::RailsCriticalCss::Extractor.new(css: css, html: html).try_extract

  # store it as wrapped css, do not regenerate for
  # each request critical_css if something go wrong
  # with css-extractor
  Rails.cache.write(
    cache[:path],
    {
      css: critical_css,
    },
    cache[:store]
  )

  Rails.cache.delete(semaphore)
end