Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikspang committed Oct 28, 2024
1 parent d805a60 commit 8754868
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
42 changes: 42 additions & 0 deletions sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ module Sentry
module Sidekiq
module Cron
module Job
def enque!(**args)
# make sure the current thread has a clean hub
Sentry.clone_hub_to_current_thread

Check warning on line 17 in sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb

View check run for this annotation

Codecov / codecov/patch

sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb#L17

Added line #L17 was not covered by tests

Sentry.with_scope do |scope|
Sentry.with_session_tracking do
scope.set_transaction_name("#{name} (#{klass})")

Check warning on line 21 in sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb

View check run for this annotation

Codecov / codecov/patch

sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb#L19-L21

Added lines #L19 - L21 were not covered by tests

transaction = start_transaction(scope)
scope.set_span(transaction) if transaction
super

Check warning on line 25 in sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb

View check run for this annotation

Codecov / codecov/patch

sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb#L23-L25

Added lines #L23 - L25 were not covered by tests

finish_transaction(transaction, 200)

Check warning on line 27 in sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb

View check run for this annotation

Codecov / codecov/patch

sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb#L27

Added line #L27 was not covered by tests
rescue
finish_transaction(transaction, 500)
raise

Check warning on line 30 in sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb

View check run for this annotation

Codecov / codecov/patch

sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb#L29-L30

Added lines #L29 - L30 were not covered by tests
end
end
end

def save
# validation failed, do nothing
return false unless super
Expand All @@ -34,6 +54,28 @@ def save

true
end

def start_transaction(scope)
options = {
name: scope.transaction_name,

Check warning on line 60 in sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb

View check run for this annotation

Codecov / codecov/patch

sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb#L59-L60

Added lines #L59 - L60 were not covered by tests
source: scope.transaction_source,
op: transaction_op,
origin: "auto.queue.sidekiq.cron"
}

Sentry.start_transaction(**options)

Check warning on line 66 in sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb

View check run for this annotation

Codecov / codecov/patch

sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb#L66

Added line #L66 was not covered by tests
end

def finish_transaction(transaction, status_code)
return unless transaction

Check warning on line 70 in sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb

View check run for this annotation

Codecov / codecov/patch

sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb#L70

Added line #L70 was not covered by tests

transaction.set_http_status(status_code)
transaction.finish

Check warning on line 73 in sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb

View check run for this annotation

Codecov / codecov/patch

sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb#L72-L73

Added lines #L72 - L73 were not covered by tests
end

def transaction_op
"queue.sidekiq-cron"

Check warning on line 77 in sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb

View check run for this annotation

Codecov / codecov/patch

sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb#L77

Added line #L77 was not covered by tests
end
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions sentry-sidekiq/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,14 @@ def execute_worker(processor, klass, **options)
end

jid = options.delete(:jid) || "123123"
timecop_delay = options.delete(:timecop_delay)

msg = Sidekiq.dump_json(created_at: Time.now.to_f, enqueued_at: Time.now.to_f, jid: jid, class: klass, args: [], **options)
Timecop.freeze(options[:timecop_delay]) if options[:timecop_delay]
Timecop.freeze(timecop_delay) if timecop_delay
work = Sidekiq::BasicFetch::UnitOfWork.new('queue:default', msg)
process_work(processor, work)
ensure
Timecop.return if options[:timecop_delay]
Timecop.return if timecop_delay
end

def process_work(processor, work)
Expand Down

0 comments on commit 8754868

Please sign in to comment.