class RocketJob::Jobs::UploadFileJob
Job
to upload a file into another job.
Intended for use by DirmonJob
to upload a file into a specified job.
Can be used directly for any job class, as long as that job responds to `#upload`.
Constants
- VALID_INSTANCE_METHODS
Public Instance Methods
perform()
click to toggle source
Create the job and upload the file into it.
# File lib/rocket_job/jobs/upload_file_job.rb, line 39 def perform job = job_class.from_properties(properties) job.id = job_id if job_id upload_file(job) job.save! rescue Exception => e # Prevent partial uploads job&.cleanup! if job.respond_to?(:cleanup!) raise(e) end
Private Instance Methods
file_exists()
click to toggle source
# File lib/rocket_job/jobs/upload_file_job.rb, line 100 def file_exists # Only check for file existence when it is a local file return unless upload_file_name.is_a?(IOStreams::Paths::File) return errors.add(:upload_file_name, "Upload file name can't be blank.") if upload_file_name.to_s == "" return if upload_file_name.exist? errors.add(:upload_file_name, "Upload file: #{upload_file_name} does not exist.") rescue NotImplementedError nil end
job_class()
click to toggle source
# File lib/rocket_job/jobs/upload_file_job.rb, line 52 def job_class @job_class ||= job_class_name.constantize rescue NameError nil end
job_has_properties()
click to toggle source
# File lib/rocket_job/jobs/upload_file_job.rb, line 112 def job_has_properties klass = job_class return unless klass properties.each_pair do |k, _v| next if klass.public_method_defined?("#{k}=".to_sym) if %i[output_categories input_categories].include?(k) category_class = k == :input_categories ? RocketJob::Category::Input : RocketJob::Category::Output properties[k].each do |category| category.each_pair do |key, _value| next if category_class.public_method_defined?("#{key}=".to_sym) errors.add( :properties, "Unknown Property in #{k}: Attempted to set a value for #{key}.#{k} which is not allowed on the job #{job_class_name}" ) end end next end errors.add( :properties, "Unknown Property: Attempted to set a value for #{k.inspect} which is not allowed on the job #{job_class_name}" ) end end
job_implements_upload()
click to toggle source
Validates job_class
is a Rocket Job
# File lib/rocket_job/jobs/upload_file_job.rb, line 92 def job_implements_upload klass = job_class return if klass.nil? || klass.instance_methods.any? { |m| VALID_INSTANCE_METHODS.include?(m) } errors.add(:job_class_name, "#{job_class} must implement any one of: :#{VALID_INSTANCE_METHODS.join(' :')} instance methods") end
job_is_a_rocket_job()
click to toggle source
Validates job_class
is a Rocket Job
# File lib/rocket_job/jobs/upload_file_job.rb, line 82 def job_is_a_rocket_job klass = job_class return if klass.nil? || klass.ancestors&.include?(RocketJob::Job) errors.add(:job_class_name, "Model #{job_class_name} must be defined and inherit from RocketJob::Job") end
upload_file(job)
click to toggle source
# File lib/rocket_job/jobs/upload_file_job.rb, line 58 def upload_file(job) if job.respond_to?(:upload) # Return the database connection for this thread back to the connection pool # in case the upload takes a long time and the database connection expires. ActiveRecord::Base.clear_active_connections! if defined?(ActiveRecord::Base) if original_file_name job.upload(upload_file_name, file_name: original_file_name) else job.upload(upload_file_name) end elsif job.respond_to?(:upload_file_name=) job.upload_file_name = upload_file_name elsif job.respond_to?(:full_file_name=) job.full_file_name = upload_file_name else raise( ArgumentError, "Model #{job_class_name} must implement '#upload', or have attribute 'upload_file_name' or 'full_file_name'" ) end end