Skip to content

Commit

Permalink
Implement asynchronous account synchronization with Sidekiq and add i…
Browse files Browse the repository at this point in the history
…nitial configuration
  • Loading branch information
andrew committed Nov 28, 2024
1 parent cef4e5d commit 88e75e4
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
12 changes: 12 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"cron": [
{
"command": "bundle exec rake accounts:sync_least_recent",
"schedule": "0 * * * *"
},
{
"command": "bundle exec rake accounts:import_from_repos",
"schedule": "0 0 * * *"
}
]
}
20 changes: 17 additions & 3 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ class Account < ApplicationRecord

before_save :set_sponsors_count

def self.sync_least_recently_synced
Account.where(last_synced_at: nil).or(Account.where("last_synced_at < ?", 1.day.ago)).order('last_synced_at asc nulls first').limit(500).each do |account|
account.sync_async
end
end

def to_s
login
end
Expand All @@ -39,13 +45,21 @@ def self.import_from_repos
logins = JSON.parse(resp.body)

logins.map(&:downcase).each do |login|
account = Account.find_or_create_by(login: login)
account.update(has_sponsors_listing: true) if account.changed?
account = Account.find_by(login: login)

if account.nil?
account = Account.create(login: login, has_sponsors_listing: true)
account.sync_async
end

end
end

def ping_repos
Faraday.get(repos_api_url + '/ping')
rescue => e
puts "Error pinging repos for #{login}"
puts e
end

def repos_api_url
Expand Down Expand Up @@ -93,7 +107,7 @@ def sync
end

def sync_async
# TODO
AccountWorker.perform_async(id)
end

def scrape_sponsored_page
Expand Down
9 changes: 9 additions & 0 deletions app/sidekiq/account_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AccountWorker
include Sidekiq::Worker

sidekiq_options queue: 'default', lock: :until_executed, lock_expiration: 1.hours.to_i

def perform(account_id)
Account.find_by_id(account_id).try(:sync_all)
end
end
22 changes: 22 additions & 0 deletions config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Sidekiq.configure_server do |config|
config.redis = { ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE } }

config.client_middleware do |chain|
chain.add SidekiqUniqueJobs::Middleware::Client
end

config.server_middleware do |chain|
chain.add SidekiqUniqueJobs::Middleware::Server
end

SidekiqUniqueJobs::Server.configure(config)
end

Sidekiq.configure_client do |config|
config.logger = Rails.logger if Rails.env.test?
config.redis = { ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE } }

config.client_middleware do |chain|
chain.add SidekiqUniqueJobs::Middleware::Client
end
end
3 changes: 3 additions & 0 deletions config/sidekiq.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:concurrency: 1
:queues:
- default
9 changes: 9 additions & 0 deletions lib/tasks/accounts.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace :accounts do
task sync_least_recent: :environment do
Account.sync_least_recently_synced
end

task import_from_repos: :environment do
Account.import_from_repos
end
end

0 comments on commit 88e75e4

Please sign in to comment.