Skip to content

Commit

Permalink
feat: always include process to identify log events
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosgz committed Nov 21, 2024
1 parent a17708c commit cc0157b
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 75 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ bundle exec site_maps generate monthly_posts --config-file config/sitemap.rb --c

You can subscribe to the internal events to receive notifications about the sitemap generation. The following events are available:

* `sitemaps.runner.enqueue_process` - Triggered when a process is enqueued.
* `sitemaps.runner.before_process_execution` - Triggered before a process starts execution
* `sitemaps.runner.process_execution` - Triggered when a process finishes execution.
* `sitemaps.builder.finalize_urlset` - Triggered when the sitemap builder finishes the URL set.
* `sitemaps.enqueue_process` - Triggered when a process is enqueued.
* `sitemaps.before_process_execution` - Triggered before a process starts execution
* `sitemaps.process_execution` - Triggered when a process finishes execution.
* `sitemaps.finalize_urlset` - Triggered when the sitemap builder finishes the URL set.

You can subscribe to the events using the following code:

```ruby
SiteMaps::Notification.subscribe("sitemaps.runner.enqueue_process") do |event|
SiteMaps::Notification.subscribe("sitemaps.enqueue_process") do |event|
puts "Enqueueing process #{event.payload[:name]}"
end
```
Expand Down
8 changes: 4 additions & 4 deletions lib/site_maps/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def initialize(object_or_event_id)

include Publisher

register_event "sitemaps.builder.finalize_urlset"
register_event "sitemaps.runner.before_process_execution"
register_event "sitemaps.runner.enqueue_process"
register_event "sitemaps.runner.process_execution"
register_event "sitemaps.finalize_urlset"
register_event "sitemaps.before_process_execution"
register_event "sitemaps.enqueue_process"
register_event "sitemaps.process_execution"
end
end
6 changes: 6 additions & 0 deletions lib/site_maps/process.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# frozen_string_literal: true

require "securerandom"

module SiteMaps
Process = Concurrent::ImmutableStruct.new(:name, :location_template, :kwargs_template, :block) do
def id
@id ||= SecureRandom.hex(4)
end

def location(**kwargs)
return unless location_template

Expand Down
9 changes: 5 additions & 4 deletions lib/site_maps/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def enqueue(process_name = :default, **kwargs)
raise ArgumentError, "Process :#{process_name} not found"
end
kwargs = process.keyword_arguments(kwargs)
SiteMaps::Notification.instrument("sitemaps.runner.enqueue_process") do |payload|
SiteMaps::Notification.instrument("sitemaps.enqueue_process") do |payload|
payload[:process] = process
payload[:kwargs] = kwargs
if process.dynamic?
Expand Down Expand Up @@ -53,15 +53,16 @@ def run
futures = []
@execution.each do |_process_name, items|
items.each do |process, kwargs|
SiteMaps::Notification.publish("sitemaps.runner.before_process_execution", process: process, kwargs: 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.runner.process_execution") do |payload|
SiteMaps::Notification.instrument("sitemaps.process_execution") do |payload|
payload[:process] = process
payload[:kwargs] = kwargs
builder = SiteMaps::SitemapBuilder.new(
adapter: adapter,
location: process.location(**kwargs)
location: process.location(**kwargs),
notification_payload: { process: process }
)
process.call(builder, **kwargs)
builder.finalize!
Expand Down
27 changes: 16 additions & 11 deletions lib/site_maps/runner/event_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,61 +13,66 @@ def [](event_name)
method(:"on_#{method_name}")
end

def on_sitemaps_runner_enqueue_process(event)
def on_sitemaps_enqueue_process(event)
process = event[:process]
kwargs = event[:kwargs]
location = process.location(**kwargs)
print_message(
"Enqueue process %<name>s#{" at %<location>s" if location}",
"[%<id>s] Enqueue process %<name>s#{" at %<location>s" if location}",
id: process.id,
name: colorize(process.name, :bold),
location: colorize(location, :lightgray)
)
if kwargs.any?
print_message("└── Keyword Arguments: {%<kwargs>s}", kwargs: kwargs.map { |k, v| "#{k}: #{v.inspect}" }.join(", "))
print_message(" └── Keyword Arguments: {%<kwargs>s}", kwargs: kwargs.map { |k, v| "#{k}: #{v.inspect}" }.join(", "))
end
end

def on_sitemaps_runner_before_process_execution(event)
def on_sitemaps_before_process_execution(event)
process = event[:process]
kwargs = event[:kwargs]
location = process.location(**kwargs)
print_message(
"Executing process %<name>s#{" at %<location>s" if location}",
"[%<id>s] Executing process %<name>s#{" at %<location>s" if location}",
id: process.id,
name: colorize(process.name, :bold),
location: colorize(location, :lightgray)
)
if kwargs.any?
print_message("└── Keyword Arguments: {%<kwargs>s}", kwargs: kwargs.map { |k, v| "#{k}: #{v.inspect}" }.join(", "))
print_message(" └── Keyword Arguments: {%<kwargs>s}", kwargs: kwargs.map { |k, v| "#{k}: #{v.inspect}" }.join(", "))
end
end

def on_sitemaps_runner_process_execution(event)
def on_sitemaps_process_execution(event)
process = event[:process]
kwargs = event[:kwargs]
location = process.location(**kwargs)
print_message(
"[%<runtime>s] Executed process %<name>s#{" at %<location>s" if location}",
"[%<id>s][%<runtime>s] Executed process %<name>s#{" at %<location>s" if location}",
id: process.id,
name: colorize(process.name, :bold),
location: colorize(location, :lightgray),
runtime: formatted_runtime(event[:runtime])
)
if kwargs.any?
print_message("└── Keyword Arguments: {%<kwargs>s}", kwargs: kwargs.map { |k, v| "#{k}: #{v.inspect}" }.join(", "))
print_message(" └── Keyword Arguments: {%<kwargs>s}", kwargs: kwargs.map { |k, v| "#{k}: #{v.inspect}" }.join(", "))
end
end

def on_sitemaps_builder_finalize_urlset(event)
def on_sitemaps_finalize_urlset(event)
process = event[:process]
links_count = event[:links_count]
news_count = event[:news_count]
url = event[:url]
text = +"[%<runtime>s] Finalize URLSet with "
text = +"[%<id>s][%<runtime>s] Finalize URLSet with "
text << "%<links>d links" if links_count > 0
text << " and " if links_count > 0 && news_count > 0
text << "%<news>d news" if news_count > 0
text << " URLs at %<url>s"

print_message(
text,
id: process.id,
links: links_count,
news: news_count,
url: colorize(url, :lightgray),
Expand Down
9 changes: 5 additions & 4 deletions lib/site_maps/sitemap_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ module SiteMaps
class SitemapBuilder
extend Forwardable

def initialize(adapter:, location: nil)
def initialize(adapter:, location: nil, notification_payload: {})
@adapter = adapter
@url_set = SiteMaps::Builder::URLSet.new
@location = location
@mutex = Mutex.new
@notification_payload = notification_payload
end

def add(path, params: nil, **options)
Expand All @@ -31,7 +32,7 @@ def finalize!

raw_data = url_set.finalize!

SiteMaps::Notification.instrument("sitemaps.builder.finalize_urlset") do |payload|
SiteMaps::Notification.instrument("sitemaps.finalize_urlset", notification_payload) do |payload|
payload[:links_count] = url_set.links_count
payload[:news_count] = url_set.news_count
payload[:last_modified] = url_set.last_modified
Expand All @@ -50,13 +51,13 @@ def finalize!

protected

attr_reader :url_set, :adapter, :location
attr_reader :url_set, :adapter, :location, :notification_payload

def_delegators :adapter, :sitemap_index, :config, :repo

def finalize_and_start_next_urlset!
raw_data = url_set.finalize!
SiteMaps::Notification.instrument("sitemaps.builder.finalize_urlset") do |payload|
SiteMaps::Notification.instrument("sitemaps.finalize_urlset", notification_payload) do |payload|
sitemap_url = repo.generate_url(location)
payload[:url] = sitemap_url
payload[:links_count] = url_set.links_count
Expand Down
7 changes: 7 additions & 0 deletions spec/site_maps/process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
require "spec_helper"

RSpec.describe SiteMaps::Process do
describe "#id" do
it "returns a unique id" do
process = described_class.new(:name, nil, nil, nil)
expect(process.id).to match(/\A[0-9a-f]{8}\z/)
end
end

describe "#location" do
it "returns the location" do
process = described_class.new(:name, "/path/%{year}-%{month}", {}, nil)
Expand Down
Loading

0 comments on commit cc0157b

Please sign in to comment.