Skip to content

Commit 08c3176

Browse files
committed
Add Salesforce sync base infrastructure
1 parent d279eca commit 08c3176

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
module Salesforce
4+
class SalesforceSyncJob < ApplicationJob
5+
include GoodJob::ActiveJobExtensions::Concurrency
6+
7+
# Serialise concurrent performs for the same record (same class + record ID)
8+
# to prevent TOCTOU races on find_or_initialize_by + save!, while allowing
9+
# jobs for different records to run fully in parallel.
10+
good_job_control_concurrency_with(
11+
perform_limit: 1,
12+
key: -> { "#{self.class.name}/#{arguments.first.values.first}" }
13+
)
14+
15+
class SalesforceRecordNotFound < StandardError
16+
end
17+
18+
class SkipBecauseSalesforceIsDisabled < StandardError
19+
end
20+
21+
discard_on SkipBecauseSalesforceIsDisabled
22+
23+
retry_on SalesforceRecordNotFound, wait: :polynomially_longer, attempts: 10
24+
25+
queue_as :salesforce_sync
26+
27+
before_perform do |_job|
28+
salesforce_enabled = ENV.fetch('SALESFORCE_ENABLED', 'true') == 'true'
29+
raise SkipBecauseSalesforceIsDisabled, 'SALESFORCE_ENABLED is not true.' unless salesforce_enabled
30+
end
31+
32+
def perform(*)
33+
raise NotImplementedError, 'Subclasses must implement perform'
34+
end
35+
36+
private
37+
38+
def truncate_value(sf_field:, value:)
39+
column = self.class::MODEL_CLASS.column_for_attribute(sf_field)
40+
return value if column.limit.nil?
41+
42+
value.truncate(column.limit, omission: '…')
43+
end
44+
end
45+
end

app/models/salesforce/base.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
module Salesforce
4+
class Base < ApplicationRecord
5+
self.abstract_class = true
6+
7+
connects_to database: { writing: :salesforce_connect }
8+
end
9+
end

0 commit comments

Comments
 (0)