lib/rest-ftp-daemon/api/jobs.rb in rest-ftp-daemon-0.424.0 vs lib/rest-ftp-daemon/api/jobs.rb in rest-ftp-daemon-0.424.2

- old
+ new

@@ -3,61 +3,62 @@ module RestFtpDaemon module API class Jobs < Grape::API include BmcDaemonLib + + ### EXCEPTIONS HANDLERS + rescue_from RestFtpDaemon::JobNotFound do |exception| + exception_error :api_job_not_found, 404, exception + end + rescue_from JSON::ParserError do |exception| + exception_error :api_job_not_found, 404, exception + + log_error "JSON::ParserError: #{exception.message}" + error!({error: :api_parse_error, message: exception.message}, 422) + end + rescue_from RestFtpDaemon::QueueCantCreateJob do |exception| + exception_error :api_cant_create_job, 422, exception + error!({error: :api_cant_create_job, message: exception.message}, 422) + end + rescue_from RestFtpDaemonException do |exception| + exception_error exception_to_error(exception), 500, exception + Rollbar.error exception + end + ### ENDPOINTS desc "Read job with ID", http_codes: [ { code: 200, message: "Here is the job you requested" }, { code: 404, message: "Job not found" } ], is_array: false params do requires :id, type: String, desc: "ID of the Job to read" end get "/:id", requirements: { id: /.*/ } do - begin - # Get job to display - raise RestFtpDaemon::JobNotFound if params[:id].nil? - job = RestFtpDaemon::JobQueue.instance.find_by_id(params[:id]) || RestFtpDaemon::JobQueue.instance.find_by_id(params[:id], true) - raise RestFtpDaemon::JobNotFound if job.nil? + # Get job to display + raise RestFtpDaemon::JobNotFound if params[:id].nil? + job = RestFtpDaemon::JobQueue.instance.find_by_id(params[:id]) || RestFtpDaemon::JobQueue.instance.find_by_id(params[:id], true) + raise RestFtpDaemon::JobNotFound if job.nil? + log_debug "found job: #{job.inspect}" - log_debug "found job: #{job.inspect}" - - rescue RestFtpDaemon::JobNotFound => exception - log_error "JobNotFound: #{exception.message}" - error!({ error: :api_job_not_found, message: exception.message }, 404) - - rescue StandardError => exception - log_error "Exception: #{exception.message}" - error!({ error: :api_exception, message: exception.message }, 500) - - else - status 200 - present job, with: RestFtpDaemon::Entities::Job, type: "complete" - - end + # Prepare response + status 200 + present job, with: RestFtpDaemon::Entities::Job, type: "complete" end desc "List all Jobs", http_codes: [ { code: 200, message: "Here are the jobs you requested" }, ], is_array: true get "/" do - begin - # Get jobs to display - jobs = RestFtpDaemon::JobQueue.instance.jobs + # Get jobs to display + jobs = RestFtpDaemon::JobQueue.instance.jobs - rescue StandardError => exception - log_error "Exception: #{exception.message}" - error!({ error: :api_exception, message: exception.message }, 500) - - else - status 200 - present jobs, with: RestFtpDaemon::Entities::Job - - end + # Prepare response + status 200 + present jobs, with: RestFtpDaemon::Entities::Job end desc "Create a new job" # desc 'Creates a new app' do # detail 'It is used to register a new app on the server and get the app_id' @@ -117,10 +118,25 @@ optional :video_custom, type: Hash, desc: "video: custom options passed to FFMPEG encoder", default: {} + optional :options, type: Hash, desc: "Options for transfers" do + optional :overwrite, + type: Boolean, + desc: "Overwrites files at target server", + default: Conf.at(:transfer, :overwrite) + optional :mkdir, + type: Boolean, + desc: "Create missing directories on target server", + default: Conf.at(:transfer, :mkdir) + optional :tempfile, + type: Boolean, + desc: "Upload to a temp file before renaming it to the target filename", + default: Conf.at(:transfer, :tempfile) + end + optional :overwrite, type: Boolean, desc: "Overwrites files at target server", default: Conf.at(:transfer, :overwrite) optional :mkdir, @@ -138,34 +154,18 @@ # requires :description # end end post "/" do - # log_debug params.to_json - begin - # Add up the new job on the queue - job = RestFtpDaemon::JobQueue.instance.create_job(params) + # Add up the new job on the queue + job = RestFtpDaemon::JobQueue.instance.create_job(params) - # Increment a counter - RestFtpDaemon::Counters.instance.increment :jobs, :received + # Increment a counter + RestFtpDaemon::Counters.instance.increment :jobs, :received - rescue JSON::ParserError => exception - log_error "JSON::ParserError: #{exception.message}" - error!({error: :api_parse_error, message: exception.message}, 422) - - rescue QueueCantCreateJob => exception - log_error "QueueCantCreateJob: #{exception.message}" - error!({error: :api_cant_create_job, message: exception.message}, 422) - - rescue RestFtpDaemonException => exception - log_error "#{exception.class.to_s} #{exception.message}" - error!({error: exception_to_error(exception), message: exception.message}, 500) - - else - status 201 - present job, with: RestFtpDaemon::Entities::Job, hide_params: true - - end + # Prepare response + status 201 + present job, with: RestFtpDaemon::Entities::Job, hide_params: true end end end end