Skip to content

Conversation

groyoh
Copy link
Contributor

@groyoh groyoh commented Jul 9, 2025

Context

We expect Utils::ActivityLog.produce_after_commit:

  1. to return the value returned by the block
  2. to run the block before the commit but produce the event on Kafka after the commit

The current implementation is not matching those behaviors:

> lago exec api bundle exec rspec --color --format documentation spec/services/utils/activity_log_spec.rb
Utils::ActivityLog
  .produce_after_commit
    when kafka is configured
      produces the event on kafka after the commit (FAILED - 1)
Failures:

  1) Utils::ActivityLog.produce_after_commit when kafka is configured when providing a block produces the event on kafka after the commit
     Failure/Error: expect(produce_result).to eq(result)
     
       expected: #<BaseService::LegacyResult coupon=#<Coupon id: "61398514-d441-4440-a96c-e586ad240a29", organization_..._plans: false, deleted_at: nil, limited_billable_metrics: false, description: "Coupon Description">>
            got: [#<AfterCommitEverywhere::Wrap:0x0000ffff604a0fc8 @connection=#<ActiveRecord::ConnectionAdapters::Pos...>, @handlers={after_commit: #<Proc:0x0000ffff604a11f8 /app/app/services/utils/activity_log.rb:25>}>]
     
       (compared using ==)
     
       Diff:
       @@ -1 +1 @@
       -#<BaseService::LegacyResult coupon=#<Coupon id: "61398514-d441-4440-a96c-e586ad240a29", organization_id: "9ccf4cec-b0a2-449c-bddf-cffecb000256", name: "new name", code: "m507v7gtmn", status: "active", terminated_at: nil, amount_cents: 200, amount_currency: "EUR", expiration: "no_expiration", created_at: "2023-03-22 12:00:00.000000000 +0000", updated_at: "2023-03-22 12:00:00.000000000 +0000", coupon_type: "fixed_amount", percentage_rate: nil, frequency: "once", frequency_duration: nil, expiration_at: nil, reusable: true, limited_plans: false, deleted_at: nil, limited_billable_metrics: false, description: "Coupon Description">>
       +[#<AfterCommitEverywhere::Wrap:0x0000ffff604a0fc8 @connection=#<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x00000000005360 env_name="test" role=:writing>, @handlers={after_commit: #<Proc:0x0000ffff604a11f8 /app/app/services/utils/activity_log.rb:25>}>]
       
     # ./spec/services/utils/activity_log_spec.rb:93:in 'block (5 levels) in <top (required)>'
     # ...
     # /usr/local/bundle/gems/webmock-3.23.1/lib/webmock/rspec.rb:39:in 'block (2 levels) in <main>'

1 example, 1 failure

Failed examples:

rspec ./spec/services/utils/activity_log_spec.rb:86 # Utils::ActivityLog.produce_after_commit when kafka is configured when providing a block produces the event on kafka after the commit

Description

The fix is to handle the after_commit behavior in the Utils::ActivityLog#produce instance method after the block is executed.

@groyoh groyoh merged commit 4a8c2ce into main Jul 9, 2025
14 checks passed
@groyoh groyoh deleted the fix/activity-log-after-commit branch July 9, 2025 14:51
diegocharles pushed a commit that referenced this pull request Jul 11, 2025
…n passing block (#3953)

## Context

We expect `Utils::ActivityLog.produce_after_commit`:

1. to return the value returned by the block
2. to run the block before the commit but produce the event on Kafka after the commit

The current implementation is not matching those behaviors:

```shell
⋊> lago exec api bundle exec rspec --color --format documentation spec/services/utils/activity_log_spec.rb
Utils::ActivityLog
  .produce_after_commit
    when kafka is configured
      produces the event on kafka after the commit (FAILED - 1)
Failures:

  1) Utils::ActivityLog.produce_after_commit when kafka is configured when providing a block produces the event on kafka after the commit
     Failure/Error: expect(produce_result).to eq(result)
     
       expected: #<BaseService::LegacyResult coupon=#<Coupon id: "61398514-d441-4440-a96c-e586ad240a29", organization_..._plans: false, deleted_at: nil, limited_billable_metrics: false, description: "Coupon Description">>
            got: [#<AfterCommitEverywhere::Wrap:0x0000ffff604a0fc8 @connection=#<ActiveRecord::ConnectionAdapters::Pos...>, @handlers={after_commit: #<Proc:0x0000ffff604a11f8 /app/app/services/utils/activity_log.rb:25>}>]
     
       (compared using ==)
     
       Diff:
       @@ -1 +1 @@
       -#<BaseService::LegacyResult coupon=#<Coupon id: "61398514-d441-4440-a96c-e586ad240a29", organization_id: "9ccf4cec-b0a2-449c-bddf-cffecb000256", name: "new name", code: "m507v7gtmn", status: "active", terminated_at: nil, amount_cents: 200, amount_currency: "EUR", expiration: "no_expiration", created_at: "2023-03-22 12:00:00.000000000 +0000", updated_at: "2023-03-22 12:00:00.000000000 +0000", coupon_type: "fixed_amount", percentage_rate: nil, frequency: "once", frequency_duration: nil, expiration_at: nil, reusable: true, limited_plans: false, deleted_at: nil, limited_billable_metrics: false, description: "Coupon Description">>
       +[#<AfterCommitEverywhere::Wrap:0x0000ffff604a0fc8 @connection=#<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x00000000005360 env_name="test" role=:writing>, @handlers={after_commit: #<Proc:0x0000ffff604a11f8 /app/app/services/utils/activity_log.rb:25>}>]
       
     # ./spec/services/utils/activity_log_spec.rb:93:in 'block (5 levels) in <top (required)>'
     # ...
     # /usr/local/bundle/gems/webmock-3.23.1/lib/webmock/rspec.rb:39:in 'block (2 levels) in <main>'

1 example, 1 failure

Failed examples:

rspec ./spec/services/utils/activity_log_spec.rb:86 # Utils::ActivityLog.produce_after_commit when kafka is configured when providing a block produces the event on kafka after the commit
```

## Description

The fix is to handle the `after_commit` behavior in the `Utils::ActivityLog#produce` instance method after the block is executed.
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