-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Campaigns, Issues, and Issue Categories now syncing from Tijuana to I…
…dentity
- Loading branch information
Showing
13 changed files
with
421 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module IdentityTijuana | ||
class Campaign < ReadWrite | ||
self.table_name = 'campaigns' | ||
|
||
scope :deleted_campaigns, -> (last_updated_at, exclude_from) { | ||
where('deleted_at is not null and deleted_at >= ? and deleted_at < ?', last_updated_at, exclude_from) | ||
.order('deleted_at, id') | ||
} | ||
|
||
scope :updated_campaigns, -> (last_updated_at, last_id) { | ||
updated_campaigns_all(last_updated_at, last_id) | ||
.order('updated_at, id') | ||
.limit(Settings.tijuana.pull_batch_amount) | ||
} | ||
|
||
scope :updated_campaigns_all, -> (last_updated_at, last_id) { | ||
where('updated_at > ? or (updated_at = ? and id > ?)', last_updated_at, last_updated_at, last_id) | ||
} | ||
|
||
def import(sync_id) | ||
begin | ||
# The campaigns table in TJ maps onto the issues table in ID. | ||
issue = ::Issue.find_or_create_by(external_id: self.id.to_s, external_source: 'tijuana') | ||
issue.name = self.name | ||
issue.save! | ||
# The campaigns.accounts_key column in TJ maps onto the issue_categories table in ID. | ||
issue.issue_categories.clear | ||
accounts_key = self.accounts_key | ||
if accounts_key.present? | ||
issue_category = ::IssueCategory.find_or_create_by(name: accounts_key) | ||
issue.issue_categories << issue_category | ||
end | ||
rescue Exception => e | ||
Rails.logger.error "Tijuana campaigns sync id:#{self.id}, error: #{e.message}" | ||
raise | ||
end | ||
end | ||
|
||
def erase(sync_id) | ||
begin | ||
issue = ::Issue.find_by(external_id: self.id.to_s, external_source: 'tijuana') | ||
if issue.present? | ||
issue.campaigns.destroy_all # TODO: Will need to cascade to other tables. | ||
issue.issue_categories.clear | ||
issue.destroy | ||
end | ||
rescue Exception => e | ||
Rails.logger.error "Tijuana campaigns delete id:#{self.id}, error: #{e.message}" | ||
raise | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
module IdentityTijuana | ||
module CampaignHelper | ||
module ClassMethods | ||
def fetch_campaign_updates(sync_id) | ||
started_at = DateTime.now | ||
last_updated_at = get_redis_date('tijuana:campaigns:last_updated_at') | ||
last_id = (Sidekiq.redis { |r| r.get 'tijuana:campaigns:last_id' } || 0).to_i | ||
campaigns_dependent_data_cutoff = DateTime.now | ||
updated_campaigns = IdentityTijuana::Campaign.updated_campaigns(last_updated_at, last_id) | ||
updated_campaigns_all = IdentityTijuana::Campaign.updated_campaigns_all(last_updated_at, last_id) | ||
|
||
updated_campaigns.each do |campaign| | ||
campaign.import(sync_id) | ||
end | ||
|
||
unless updated_campaigns.empty? | ||
campaigns_dependent_data_cutoff = updated_campaigns.last.updated_at if updated_campaigns.count < updated_campaigns_all.count | ||
end | ||
|
||
# Erase any logically deleted campaigns from ID. | ||
deleted_campaigns = IdentityTijuana::Campaign.deleted_campaigns(last_updated_at, campaigns_dependent_data_cutoff) | ||
deleted_campaigns.each do |campaign| | ||
campaign.erase(sync_id) | ||
end | ||
|
||
unless updated_campaigns.empty? | ||
set_redis_date('tijuana:campaigns:last_updated_at', updated_campaigns.last.updated_at) | ||
Sidekiq.redis { |r| r.set 'tijuana:campaigns:last_id', updated_campaigns.last.id } | ||
end | ||
|
||
set_redis_date('tijuana:campaigns:dependent_data_cutoff', campaigns_dependent_data_cutoff) | ||
|
||
execution_time_seconds = ((DateTime.now - started_at) * 24 * 60 * 60).to_i | ||
yield( | ||
updated_campaigns.size, | ||
updated_campaigns.pluck(:id), | ||
{ | ||
scope: 'tijuana:campaigns:last_updated_at', | ||
scope_limit: Settings.tijuana.pull_batch_amount, | ||
from: last_updated_at, | ||
to: updated_campaigns.empty? ? nil : updated_campaigns.last.updated_at, | ||
started_at: started_at, | ||
completed_at: DateTime.now, | ||
execution_time_seconds: execution_time_seconds, | ||
remaining_behind: updated_campaigns_all.count | ||
}, | ||
false | ||
) | ||
|
||
release_mutex_lock(:fetch_campaign_updates) | ||
need_another_batch = updated_campaigns.count < updated_campaigns_all.count | ||
if need_another_batch | ||
schedule_pull_batch(:fetch_campaign_updates) | ||
else | ||
schedule_pull_batch(:fetch_page_sequence_updates) | ||
schedule_pull_batch(:fetch_push_updates) | ||
end | ||
end | ||
end | ||
|
||
extend ClassMethods | ||
def self.included(other) | ||
other.extend(ClassMethods) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require "identity_tijuana/redis_helper" | ||
|
||
module IdentityTijuana | ||
module MutexHelper | ||
include IdentityTijuana::RedisHelper | ||
|
||
module ClassMethods | ||
def acquire_mutex_lock(method_name, sync_id) | ||
mutex_name = "#{SYSTEM_NAME}:mutex:#{method_name}" | ||
new_mutex_expiry = DateTime.now + MUTEX_EXPIRY_DURATION | ||
mutex_acquired = set_redis_date(mutex_name, new_mutex_expiry, true) | ||
unless mutex_acquired | ||
mutex_expiry = get_redis_date(mutex_name) | ||
if mutex_expiry.past? | ||
unless worker_currently_running?(method_name, sync_id) | ||
delete_redis_date(mutex_name) | ||
mutex_acquired = set_redis_date(mutex_name, new_mutex_expiry, true) | ||
end | ||
end | ||
end | ||
mutex_acquired | ||
end | ||
|
||
def release_mutex_lock(method_name) | ||
mutex_name = "#{SYSTEM_NAME}:mutex:#{method_name}" | ||
delete_redis_date(mutex_name) | ||
end | ||
end | ||
|
||
extend ClassMethods | ||
def self.included(other) | ||
other.extend(ClassMethods) | ||
end | ||
end | ||
end |
Oops, something went wrong.