Added methods to queue registered import backends as import jobs and start them easily via the scheduler.

This commit is contained in:
Thorsten Eckel 2017-04-19 14:37:11 +02:00
parent c9b2255e4f
commit feef4211f7

View file

@ -59,6 +59,17 @@ class ImportJob < ApplicationModel
end end
end end
# Queues and starts all import backends as import jobs.
#
# @example
# ImportJob.start_registered
#
# return [nil]
def self.start_registered
queue_registered
start
end
# Starts all import jobs that have not started yet and are no dry runs. # Starts all import jobs that have not started yet and are no dry runs.
# #
# @example # @example
@ -68,4 +79,44 @@ class ImportJob < ApplicationModel
def self.start def self.start
where(started_at: nil, dry_run: false).each(&:start) where(started_at: nil, dry_run: false).each(&:start)
end end
# Queues all configured import backends from Setting 'import_backends' as import jobs
# that are not yet queued.
#
# @example
# ImportJob.queue_registered
#
# return [nil]
def self.queue_registered
import_backends = Setting.get('import_backends')
return if import_backends.blank?
import_backends.each do |backend|
if !backend_valid?(backend)
Rails.logger.error "Invalid import backend '#{backend}'"
next
end
# skip if no entry exists
# skip if a not finished entry exists
next if ImportJob.exists?(name: backend, finished_at: nil)
ImportJob.create(name: backend)
end
end
# Checks if the given import backend is valid.
#
# @example
# ImportJob.backend_valid?('Import::Ldap')
# # => true
#
# return [Boolean]
def self.backend_valid?(backend)
backend.constantize
true
rescue NameError
false
end
end end