2017-08-14 11:56:23 +00:00
|
|
|
module Integration::ImportJobBase
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
def job_try_index
|
|
|
|
job_index(
|
|
|
|
dry_run: true,
|
|
|
|
take_finished: params[:finished] == 'true'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def job_try_create
|
2018-01-08 15:27:23 +00:00
|
|
|
ImportJob.dry_run(name: backend, payload: payload_dry_run)
|
2017-08-14 11:56:23 +00:00
|
|
|
render json: {
|
|
|
|
result: 'ok',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def job_start_index
|
|
|
|
job_index(dry_run: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def job_start_create
|
2018-01-08 15:27:23 +00:00
|
|
|
if !ImportJob.exists?(name: backend, finished_at: nil)
|
|
|
|
job = ImportJob.create(name: backend)
|
2017-08-14 11:56:23 +00:00
|
|
|
job.delay.start
|
|
|
|
end
|
|
|
|
render json: {
|
|
|
|
result: 'ok',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def payload_dry_run
|
2017-10-24 15:00:48 +00:00
|
|
|
clean_payload(params.permit!.to_h)
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-10-24 15:00:48 +00:00
|
|
|
def clean_payload(payload)
|
|
|
|
payload.except(:wizardData, :action, :controller)
|
|
|
|
end
|
|
|
|
|
2017-08-14 11:56:23 +00:00
|
|
|
def answer_with
|
|
|
|
result = yield
|
|
|
|
render json: result.merge(result: 'ok')
|
|
|
|
rescue => e
|
|
|
|
logger.error(e)
|
|
|
|
render json: {
|
|
|
|
result: 'failed',
|
|
|
|
message: e.message,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-01-08 15:27:23 +00:00
|
|
|
def backend
|
|
|
|
"Import::#{controller_name.classify}"
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def job_index(dry_run:, take_finished: true)
|
|
|
|
job = ImportJob.find_by(
|
2018-01-08 15:27:23 +00:00
|
|
|
name: backend,
|
2017-08-14 11:56:23 +00:00
|
|
|
dry_run: dry_run,
|
|
|
|
finished_at: nil
|
|
|
|
)
|
|
|
|
if !job && take_finished
|
|
|
|
job = ImportJob.where(
|
2018-01-08 15:27:23 +00:00
|
|
|
name: backend,
|
2017-08-14 11:56:23 +00:00
|
|
|
dry_run: dry_run
|
|
|
|
).order(created_at: :desc).limit(1).first
|
|
|
|
end
|
|
|
|
|
|
|
|
if job
|
|
|
|
model_show_render_item(job)
|
|
|
|
else
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|