diff --git a/app/controllers/api/settings_controller.rb b/app/controllers/api/settings_controller.rb
index 33e268cbd..08e39fc1f 100644
--- a/app/controllers/api/settings_controller.rb
+++ b/app/controllers/api/settings_controller.rb
@@ -14,7 +14,7 @@ class API::SettingsController < API::ApiController
render status: :not_modified and return if setting_params[:value] == @setting.value
if @setting.save && @setting.history_values.create(value: setting_params[:value], invoicing_profile: current_user.invoicing_profile)
- SettingService.new.after_update(@setting)
+ SettingService.after_update(@setting)
render status: :ok
else
render json: @setting.errors.full_messages, status: :unprocessable_entity
@@ -30,6 +30,7 @@ class API::SettingsController < API::ApiController
db_setting = Setting.find_or_initialize_by(name: setting[:name])
db_setting.save && db_setting.history_values.create(value: setting[:value], invoicing_profile: current_user.invoicing_profile)
+ SettingService.after_update(db_setting)
@settings.push db_setting
end
end
diff --git a/app/services/setting_service.rb b/app/services/setting_service.rb
index b3a0a6ae9..97dfa72e3 100644
--- a/app/services/setting_service.rb
+++ b/app/services/setting_service.rb
@@ -4,7 +4,7 @@
# Due to the way the controller updates the settings, we cannot safely use ActiveRecord's callbacks (eg. after_update, after_commit...)
# so this service provides a wrapper around these operations.
class SettingService
- def after_update(setting)
+ def self.after_update(setting)
# update the stylesheet
Stylesheet.theme&.rebuild! if %w[main_color secondary_color].include? setting.name
Stylesheet.home_page&.rebuild! if setting.name == 'home_css'
@@ -13,6 +13,10 @@ class SettingService
NotifyPrivacyUpdateWorker.perform_async(id) if setting.name == 'privacy_body'
# sync all users on stripe
- StripeWorker.perform_async(:sync_members) if %w[stripe_public_key stripe_secret_key].include? setting.name
+ return unless %w[stripe_public_key stripe_secret_key].include? setting.name
+
+ SyncMembersOnStripeWorker.perform_async(
+ setting.history_values.last&.invoicing_profile&.user&.id
+ )
end
end
diff --git a/app/workers/availability_indexer_worker.rb b/app/workers/availability_indexer_worker.rb
index 7466904a9..aa29eeb8f 100644
--- a/app/workers/availability_indexer_worker.rb
+++ b/app/workers/availability_indexer_worker.rb
@@ -2,7 +2,6 @@ class AvailabilityIndexerWorker
include Sidekiq::Worker
sidekiq_options queue: 'elasticsearch', retry: true
- Logger = Sidekiq.logger.level == Logger::DEBUG ? Sidekiq.logger : nil
Client = Elasticsearch::Model.client
def perform(operation, record_id)
@@ -14,13 +13,13 @@ class AvailabilityIndexerWorker
record = Availability.find(record_id)
Client.index index: Availability.index_name, type: Availability.document_type, id: record.id, body: record.as_indexed_json
rescue ActiveRecord::RecordNotFound
- STDERR.puts "Availability id(#{record_id}) will not be indexed in ElasticSearch as it does not exists anymore in database"
+ logger.warn "Availability id(#{record_id}) will not be indexed in ElasticSearch as it does not exists anymore in database"
end
when /delete/
begin
Client.delete index: Availability.index_name, type: Availability.document_type, id: record_id
rescue Elasticsearch::Transport::Transport::Errors::NotFound
- STDERR.puts "Availability id(#{record_id}) will not be deleted form ElasticSearch as it has not been already indexed"
+ logger.warn "Availability id(#{record_id}) will not be deleted form ElasticSearch as it has not been already indexed"
end
else raise ArgumentError, "Unknown operation '#{operation}'"
end
diff --git a/app/workers/openlab_worker.rb b/app/workers/openlab_worker.rb
index 173287221..2281b3acb 100644
--- a/app/workers/openlab_worker.rb
+++ b/app/workers/openlab_worker.rb
@@ -5,8 +5,6 @@ class OpenlabWorker
include Sidekiq::Worker
sidekiq_options queue: 'default', retry: true
- LOGGER = Sidekiq.logger.level == Logger::DEBUG ? Sidekiq.logger : nil
-
def initialize
client = Openlab::Client.new(app_secret: Setting.get('openlab_app_secret'))
@projets = Openlab::Projects.new(client)
@@ -14,7 +12,7 @@ class OpenlabWorker
end
def perform(action, project_id)
- LOGGER&.debug ['Openlab sync', action, "project ID: #{project_id}"]
+ logger.debug ['Openlab sync', action, "project ID: #{project_id}"]
case action.to_s
when /create/
@@ -29,6 +27,6 @@ class OpenlabWorker
raise NotImplementedError
end
- LOGGER&.debug ['Openlab sync', 'RESPONSE ERROR', response.inspect] unless response.success?
+ logger.debug ['Openlab sync', 'RESPONSE ERROR', response.inspect] unless response.success?
end
end
diff --git a/app/workers/stripe_worker.rb b/app/workers/stripe_worker.rb
index 4d423e208..b45f50ce2 100644
--- a/app/workers/stripe_worker.rb
+++ b/app/workers/stripe_worker.rb
@@ -5,8 +5,6 @@ class StripeWorker
include Sidekiq::Worker
sidekiq_options queue: :stripe
- LOGGER = Sidekiq.logger.level == Logger::DEBUG ? Sidekiq.logger : nil
-
def perform(action, *params)
send(action, *params)
end
@@ -43,24 +41,4 @@ class StripeWorker
cpn = Stripe::Coupon.retrieve(coupon_code)
cpn.delete
end
-
- def sync_members
- LOGGER&.debug ['StripeWorker', 'SyncMembers', 'We create all non-existing customers on stripe. This may take a while...']
- total = User.online_payers.count
- User.online_payers.each_with_index do |member, index|
- LOGGER&.debug ['StripeWorker', 'SyncMembers' "#{index} / #{total}"]
- begin
- stp_customer = Stripe::Customer.retrieve member.stp_customer_id
- create_stripe_customer(member.id) if stp_customer.nil? || stp_customer[:deleted]
- rescue Stripe::InvalidRequestError
- create_stripe_customer(member.id)
- end
- end
- LOGGER&.debug ['StripeWorker', 'SyncMembers', 'Sync is done']
- notify_user = Setting.find_by(name: 'stripe_secret_key')&.history_values&.last&.invoicing_profile&.user
- return unless notify_user
-
- NotificationCenter.call type: :notify_admin_members_stripe_sync,
- receiver: notify_user
- end
end
diff --git a/app/workers/sync_members_on_stripe_worker.rb b/app/workers/sync_members_on_stripe_worker.rb
new file mode 100644
index 000000000..c375c5344
--- /dev/null
+++ b/app/workers/sync_members_on_stripe_worker.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+# This worker perform various requests to the Stripe API (payment service)
+class SyncMembersOnStripeWorker
+ include Sidekiq::Worker
+ sidekiq_options lock: :until_executed, on_conflict: :reject, queue: :stripe
+
+ def perform(notify_user_id = nil)
+ logger.debug 'We create all non-existing customers on stripe. This may take a while...'
+ total = User.online_payers.count
+ User.online_payers.each_with_index do |member, index|
+ logger.debug "#{index} / #{total}"
+ begin
+ stp_customer = Stripe::Customer.retrieve member.stp_customer_id
+ StripeWorker.perform_async(:create_stripe_customer, member.id) if stp_customer.nil? || stp_customer[:deleted]
+ rescue Stripe::InvalidRequestError
+ StripeWorker.perform_async(:create_stripe_customer, member.id)
+ end
+ end
+ logger.debug 'Sync is done'
+ return unless notify_user_id
+
+ logger.debug "Notify user #{notify_user_id}"
+ NotificationCenter.call type: :notify_admin_members_stripe_sync,
+ receiver: User.find(notify_user_id)
+ end
+end
diff --git a/app/workers/version_check_worker.rb b/app/workers/version_check_worker.rb
index dd744aaa9..83c572658 100644
--- a/app/workers/version_check_worker.rb
+++ b/app/workers/version_check_worker.rb
@@ -3,6 +3,9 @@
# Will check the application version to ensure it is up-to-date
class VersionCheckWorker
include Sidekiq::Worker
+ sidekiq_options lock: :until_executed,
+ on_conflict: :reject,
+ queue: :system
def perform
require 'fab_hub'
@@ -10,7 +13,7 @@ class VersionCheckWorker
res = FabHub.fab_manager_version_check
rescue Errno::ECONNREFUSED => e
if Rails.env.development?
- puts "Unable to check the version, maybe FabHub is not running: #{e}"
+ logger.warn "Unable to check the version, maybe FabHub is not running: #{e}"
return
end
end
diff --git a/config/routes.rb b/config/routes.rb
index 3525a2e56..f29ea4b6f 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-require 'sidekiq/web'
+require 'sidekiq_unique_jobs/web'
require 'sidekiq/cron/web'
Rails.application.routes.draw do
diff --git a/lib/tasks/fablab/stripe.rake b/lib/tasks/fablab/stripe.rake
index 27173769d..579eaf513 100644
--- a/lib/tasks/fablab/stripe.rake
+++ b/lib/tasks/fablab/stripe.rake
@@ -49,7 +49,7 @@ namespace :fablab do
desc 'sync users to the stripe database'
task sync_members: :environment do
puts 'We create all non-existing customers on stripe. This may take a while, please wait...'
- StripeWorker.perform(:sync_members)
+ SyncMembersOnStripeWorker.perform
puts 'Done'
end