2020-06-08 11:38:49 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Asynchronously synchronize the projects with OpenLab-Projects
|
2016-04-20 18:13:36 +02:00
|
|
|
class OpenlabWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
sidekiq_options queue: 'default', retry: true
|
|
|
|
|
2020-06-08 15:17:56 +02:00
|
|
|
def initialize
|
|
|
|
client = Openlab::Client.new(app_secret: Setting.get('openlab_app_secret'))
|
2022-02-25 15:22:14 +01:00
|
|
|
@projects = Openlab::Projects.new(client)
|
2020-06-08 15:17:56 +02:00
|
|
|
super
|
|
|
|
end
|
2016-04-20 18:13:36 +02:00
|
|
|
|
|
|
|
def perform(action, project_id)
|
2020-06-09 18:51:57 +02:00
|
|
|
logger.debug ['Openlab sync', action, "project ID: #{project_id}"]
|
2016-04-20 18:13:36 +02:00
|
|
|
|
|
|
|
case action.to_s
|
|
|
|
when /create/
|
|
|
|
project = Project.find(project_id)
|
2022-02-25 15:22:14 +01:00
|
|
|
response = @projects.create(project.openlab_attributes)
|
2016-04-20 18:13:36 +02:00
|
|
|
when /update/
|
|
|
|
project = Project.find(project_id)
|
2022-02-25 15:22:14 +01:00
|
|
|
response = @projects.update(project_id, project.openlab_attributes)
|
2016-04-20 18:13:36 +02:00
|
|
|
when /destroy/
|
2022-02-25 15:22:14 +01:00
|
|
|
response = @projects.destroy(project_id)
|
2020-06-08 11:38:49 +02:00
|
|
|
else
|
|
|
|
raise NotImplementedError
|
2016-04-20 18:13:36 +02:00
|
|
|
end
|
2016-04-21 11:42:43 +02:00
|
|
|
|
2020-06-09 18:51:57 +02:00
|
|
|
logger.debug ['Openlab sync', 'RESPONSE ERROR', response.inspect] unless response.success?
|
2021-04-06 14:58:12 +02:00
|
|
|
rescue Errno::ECONNREFUSED => e
|
|
|
|
logger.warn "Unable to connect to OpenProject, maybe the dev instance is not running: #{e}" if Rails.env.development?
|
2023-01-11 09:07:06 +01:00
|
|
|
rescue ActiveRecord::RecordNotFound => e
|
|
|
|
logger.warn "#{e.message}: Project #{project_id} was not found, unable to sync it on OpenLab"
|
2016-04-20 18:13:36 +02:00
|
|
|
end
|
|
|
|
end
|