Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 9 additions & 31 deletions app/controllers/concerns/uploadable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ module Uploadable
private

def process_upload_background(args = {})
attachment = args.fetch(:attachment)

job_logger.write 'Enqueueing job to start in the background.'

uid = is_api? ? job_logger.uid : upload_params[:item_id]
attachment = args[:attachment]

# NOTE: call the bg job as last thing in the action helps us
# avoid SQLite3::BusyException when using sqlite and
Expand All @@ -24,38 +20,20 @@ def process_upload_background(args = {})
file: attachment.fullpath.to_s,
plugin_name: @uploader.to_s,
project_id: current_project.id,
state: @state,
uid: uid
state: @state
)
end

def process_upload_inline(args = {})
attachment = args[:attachment]

job_logger.write('Small attachment detected. Processing in line.')
begin
importer = @uploader::Importer.new(
default_user_id: current_user.id,
logger: job_logger,
plugin: @uploader,
project_id: current_project.id,
state: @state,
)

importer.import(file: attachment.fullpath)
rescue Exception => e
# Fail noisily in test mode; re-raise the error so the test fails:
raise if Rails.env.test?
job_logger.write('There was a fatal error processing your upload:')
job_logger.write(e.message)
if Rails.env.development?
e.backtrace[0..10].each do |trace|
job_logger.debug { trace }
sleep(0.2)
end
end
end
job_logger.write('Worker process completed.')
UploadJob.perform_now(
default_user_id: current_user.id,
file: attachment.fullpath.to_s,
plugin_name: @uploader.to_s,
project_id: current_project.id,
state: @state
)
end

def validate_state
Expand Down
18 changes: 8 additions & 10 deletions app/controllers/upload_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def create
@attachment.save

@success = true
@item_id = Log.new.uid
flash.now[:notice] = "Successfully uploaded #{ filename }"
end

Expand All @@ -40,15 +39,14 @@ def parse
# cause the processing of a small file to time out).
#
# In Development and testing, if the file is small, process in line.
if Rails.env.production? || (File.size(attachment.fullpath) > 1024 * 1024)
process_upload_background(attachment: attachment)
else
process_upload_inline(attachment: attachment)
end

# Nothing to do, the client-side JS will poll ./status for updates
# from now on
head :ok
job =
if Rails.env.production? || (File.size(attachment.fullpath) > 1024 * 1024)
process_upload_background(attachment: attachment)
else
process_upload_inline(attachment: attachment)
end

render json: { job_id: job.job_id }
end

private
Expand Down
7 changes: 4 additions & 3 deletions app/jobs/upload_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ class UploadJob < ApplicationJob

queue_as :dradis_upload

def perform(default_user_id:, file:, plugin_name:, project_id:, state:, uid:)
logger = Log.new(uid: uid)
def perform(default_user_id:, file:, plugin_name:, project_id:, state:)
logger = Log.new(uid: job_id)

logger.write { "Job id is #{uid}." }
logger.write { "Job id is #{job_id}." }
logger.write { 'Running Ruby version %s' % RUBY_VERSION }
logger.write { 'Worker process starting background task.' }

Expand All @@ -24,6 +24,7 @@ def perform(default_user_id:, file:, plugin_name:, project_id:, state:, uid:)

logger.write { 'Worker process completed.' }

self
rescue => exception
logger.write { "There was an error with the upload: #{exception.message}" }
if Rails.env.development?
Expand Down
23 changes: 12 additions & 11 deletions app/views/upload/create.js.erb
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
<% if @success -%>
ConsoleUpdater.jobId = '<%= @item_id %>'
$('#result').data('id', ConsoleUpdater.jobId)
$('#result').show()
$('#item_id').val(ConsoleUpdater.jobId)

$('#filesize').text('<%=j number_to_human_size( File.size(@attachment.fullpath) ) %>');
$('#spinner').hide();
ConsoleUpdater.parsing = true;
url = $('#new_upload').data('parse-url');
data = {
file: '<%= @attachment.filename %>',
item_id: '<%= @item_id %>',
uploader: '<%= params[:uploader] %>'
};
<% if @state %>
data.state = '<%= @state %>';
<% end %>

$.post(url, data, null, 'script');
url = $('#new_upload').data('parse-url');
$.post(url, data, function(response) {
ConsoleUpdater.jobId = response['job_id']

$('#result').data('id', ConsoleUpdater.jobId)
$('#result').show()
$('#item_id').val(ConsoleUpdater.jobId)

$('#filesize').text('<%=j number_to_human_size( File.size(@attachment.fullpath) ) %>');
$('#spinner').hide();
ConsoleUpdater.parsing = true;
}, 'json');

$('#attachment').val('<%= @attachment.filename %>');
setTimeout(ConsoleUpdater.updateConsole, 1000);
Expand Down