Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage webhook state from background job #14

Merged
merged 7 commits into from
Jul 24, 2024

Conversation

julik
Copy link
Contributor

@julik julik commented Jul 24, 2024

Instead of requiring the webhook handler to manage the state of the webhook, manage it from the background job. Atomically lock the webhook for processing, and skip the job if the webhook has already been taken by a different worker. This removes the need for handlers to manage the processing state, and also enables locking - removing the need for us to have locks on the level of the job.

We want to know whether the correct state transitions take place, and whether the actual processing of the webhook gets done correctly
@julik julik requested a review from skatkov July 24, 2024 08:35
@julik julik changed the title Manage webhook processing state Manage webhook state from background job Jul 24, 2024
@@ -5,14 +5,23 @@
module Munster
class ProcessingJob < ActiveJob::Base
def perform(webhook)
webhook.class.transaction do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: #with_lock opens transaction internally. Is there a need to open another transaction?

See rails code:
https://github.com/rails/rails/blob/36c1591bcb5e0ee3084759c7f42a706fe5bb7ca7/activerecord/lib/active_record/locking/pessimistic.rb#L92

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None - I didn't know it does! thanks!

else
Rails.logger.info { "Webhook #{webhook.inspect} did not pass validation and was skipped" }
Rails.logger.info { "#{webhook.class} #{webhook.id} did not pass validation and was skipped" }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: I would consider this to be an error-level severity and would like to see it in AppSignal, instead of just as a message in the logs (which I have a 99% chance of missing).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough

webhook.handler.process(webhook)
webhook.processed! if webhook.processing?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: could this if webhook.processing? check be a bit redundant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because we still assume a handler could have set the state to something else than processing.

@julik julik merged commit 869907b into main Jul 24, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants