@@ -3,55 +3,21 @@ module Message
33 class PublishJob
44 include Sidekiq ::Job
55
6- sidekiq_options queue : 'events' , retry : false , backtrace : true
6+ sidekiq_options retry : false , backtrace : true
77
8- def perform ( args )
9- messageable = ActiveRecord ::Base . connection_pool . with_connection do
10- args [ 'messageable_type' ] . constantize . find ( args [ 'messageable_id' ] )
11- end
12-
13- case args [ 'messageable_type' ]
14- when 'Accountify::Models::Organisation::CreatedEvent'
15- Accountify ::InvoiceStatusSummary ::GenerateJob . perform_async ( {
16- 'tenant_id' => messageable . tenant_id ,
17- 'organisation_id' => messageable . body [ 'organisation' ] [ 'id' ] } )
18-
19- when 'Accountify::Models::Invoice::DraftedEvent'
20- Accountify ::InvoiceStatusSummary ::RegenerateJob . perform_async ( {
21- 'tenant_id' => messageable . tenant_id ,
22- 'organisation_id' => messageable . body [ 'organisation' ] [ 'id' ] ,
23- 'invoice_updated_at' => messageable . created_at . utc . iso8601 } )
24-
25- when 'Accountify::Models::Invoice::UpdatedEvent'
26- Accountify ::InvoiceStatusSummary ::RegenerateJob . perform_async ( {
27- 'tenant_id' => messageable . tenant_id ,
28- 'organisation_id' => messageable . body [ 'organisation' ] [ 'id' ] ,
29- 'invoice_updated_at' => messageable . created_at . utc . iso8601 } )
8+ MESSAGEABLE_TYPE_REGEX = /\A ([A-Za-z]+)::Models::([A-Za-z]+)::([A-Za-z]+)Event\z /
309
31- when 'Accountify::Models::Invoice::IssuedEvent'
32- Accountify ::InvoiceStatusSummary ::RegenerateJob . perform_async ( {
33- 'tenant_id' => messageable . tenant_id ,
34- 'organisation_id' => messageable . body [ 'organisation' ] [ 'id' ] ,
35- 'invoice_updated_at' => messageable . created_at . utc . iso8601 } )
36-
37- when 'Accountify::Models::Invoice::PaidEvent'
38- Accountify ::InvoiceStatusSummary ::RegenerateJob . perform_async ( {
39- 'tenant_id' => messageable . tenant_id ,
40- 'organisation_id' => messageable . body [ 'organisation' ] [ 'id' ] ,
41- 'invoice_updated_at' => messageable . created_at . utc . iso8601 } )
42-
43- when 'Accountify::Models::Invoice::VoidedEvent'
44- Accountify ::InvoiceStatusSummary ::RegenerateJob . perform_async ( {
45- 'tenant_id' => messageable . tenant_id ,
46- 'organisation_id' => messageable . body [ 'organisation' ] [ 'id' ] ,
47- 'invoice_updated_at' => messageable . created_at . utc . iso8601 } )
10+ def perform ( args )
11+ messageable_type = args [ 'messageable_type' ]
4812
49- when 'Accountify::Models::Invoice::DeletedEvent'
50- Accountify ::InvoiceStatusSummary ::RegenerateJob . perform_async ( {
51- 'tenant_id' => messageable . tenant_id ,
52- 'organisation_id' => messageable . body [ 'organisation' ] [ 'id' ] ,
53- 'invoice_updated_at' => messageable . created_at . utc . iso8601 } )
13+ if !messageable_type . match ( MESSAGEABLE_TYPE_REGEX )
14+ raise StandardError , "Unexpected class name format: #{ messageable_type } "
5415 end
16+
17+ namespace , model , event = messageable_type . match ( MESSAGEABLE_TYPE_REGEX ) . captures
18+ job_class_name = "#{ namespace } ::#{ model } ::#{ event } Job"
19+ job_class = job_class_name . constantize
20+ job_class . perform_async ( { 'id' => args [ 'messageable_id' ] } )
5521 end
5622 end
5723 end
0 commit comments