Class: SiteMaps::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/site_maps/runner.rb

Defined Under Namespace

Modules: EventListener

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter = SiteMaps.current_adapter, max_threads: 4, ping: nil) ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
11
12
13
14
# File 'lib/site_maps/runner.rb', line 7

def initialize(adapter = SiteMaps.current_adapter, max_threads: 4, ping: nil)
  @adapter = adapter.tap(&:reset!)
  @ping = ping
  @pool = Concurrent::FixedThreadPool.new(max_threads)
  @execution = Concurrent::Hash.new
  @failed = Concurrent::AtomicBoolean.new(false)
  @errors = Concurrent::Array.new
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



5
6
7
# File 'lib/site_maps/runner.rb', line 5

def adapter
  @adapter
end

Instance Method Details

#enqueue(process_name = :default, **kwargs) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/site_maps/runner.rb', line 16

def enqueue(process_name = :default, **kwargs)
  process_name = process_name.to_sym
  process = @adapter.processes.fetch(process_name) do
    raise ArgumentError, "Process :#{process_name} not found"
  end
  kwargs = process.keyword_arguments(kwargs)
  SiteMaps::Notification.instrument("sitemaps.enqueue_process") do |payload|
    payload[:process] = process
    payload[:kwargs] = kwargs
    if process.dynamic?
      @execution[process_name] ||= Concurrent::Array.new
      if @execution[process_name].any? { |_, k| k == kwargs }
        raise ArgumentError, "Process :#{process_name} already enqueued with kwargs #{kwargs}"
      end
      @execution[process_name] << [process, kwargs]
    else
      if @execution.key?(process_name)
        raise ArgumentError, "Process :#{process_name} already defined"
      end
      @execution[process_name] = Concurrent::Array.new([[process, kwargs]])
    end
  end
  self
end

#enqueue_remainingObject Also known as: enqueue_all



41
42
43
44
45
46
47
48
# File 'lib/site_maps/runner.rb', line 41

def enqueue_remaining
  @adapter.processes.each_key do |process_name|
    next if @execution.key?(process_name)

    enqueue(process_name)
  end
  self
end

#runObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/site_maps/runner.rb', line 51

def run
  may_preload_sitemap_index

  futures = []
  @execution.each do |_process_name, items|
    items.each do |process, kwargs|
      SiteMaps::Notification.publish("sitemaps.before_process_execution", process: process, kwargs: kwargs)
      futures << Concurrent::Future.execute(executor: pool) do
        wrap_process_execution(process) do
          SiteMaps::Notification.instrument("sitemaps.process_execution") do |payload|
            payload[:process] = process
            payload[:kwargs] = kwargs
            builder = SiteMaps::SitemapBuilder.new(
              adapter: adapter,
              location: process.location(**kwargs),
              notification_payload: {process: process}
            )
            adapter.process_mixins.each { |mixin| builder.extend(mixin) }
            process.call(builder, **kwargs)
            builder.finalize!
          end
        end
      end
    end
  end

  futures.each(&:wait)
  failed.false? ? finalize! : fail_with_errors!
ensure
  pool.shutdown
  pool.wait_for_termination
  @execution.clear
end