From a170e1d830c86ab54041fee1d1c355975097928a Mon Sep 17 00:00:00 2001 From: Brian Hoffman Date: Mon, 6 Jan 2014 23:15:43 -0500 Subject: [PATCH] use atomic flag to ensure only one job runs at a time --- app/lib/migrate.rb | 4 ---- app/main.rb | 34 +++++++++++++++++----------------- app/views/busy.erb | 3 +++ 3 files changed, 20 insertions(+), 21 deletions(-) create mode 100644 app/views/busy.erb diff --git a/app/lib/migrate.rb b/app/lib/migrate.rb index 715538b9..8f7f619b 100644 --- a/app/lib/migrate.rb +++ b/app/lib/migrate.rb @@ -24,9 +24,6 @@ def initialize(params = {}) Archon.record_type(:digitalfile).base_url = @args[:do_baseurl] - # 1 job per thread - raise "Job thread occupied." if Thread.current[:archon_migration_job] - @aspace = ArchivesSpace::Client.new( :url => @args[:aspace_url], :user => @args[:aspace_user], @@ -51,7 +48,6 @@ def initialize(params = {}) FileUtils.rm_rf(download_path) end - Thread.current[:archon_migration_job] = self end diff --git a/app/main.rb b/app/main.rb index 1b1ad139..9c2c715c 100644 --- a/app/main.rb +++ b/app/main.rb @@ -47,7 +47,12 @@ class MigrationService < Sinatra::Base get '/' do - erb :index + $busy ||= Atomic.new(false) + if $busy.value + erb :busy + else + erb :index + end end @@ -57,28 +62,23 @@ class MigrationService < Sinatra::Base post '/jobs' do - if $busy - return Enumerator.new do |y| - y << JSON.generate({ - :type => :error, - :body => "Sorry, another user is currently waiting for a migration to complete. Check back later, and if this message persists, contact your server administrator" - }) + "---\n" - end - end - - $busy = true + + raise "BUSY" if $busy.value + $busy.value = true $log.debug("POST /jobs with params: #{params.inspect}") Enumerator.new do |y| begin stamp = Time.now.strftime("%Y-%m-%d-%H-%M-%S") - $logfile = File.new(Appdata.app_dir + "/public/log-#{stamp}.txt", 'w') - $syslog = Logger.new($logfile) - $log = MigrationLog.new(y, $syslog) + logfile = File.new(Appdata.app_dir + "/public/log-#{stamp}.txt", 'w') + syslog = Logger.new(logfile) + # todo - separate logs per thread to support + # parallel jobs + $log = MigrationLog.new(y, syslog) y << JSON.generate({ :type => :log, - :file => File.basename($logfile.path) + :file => File.basename(logfile.path) }) + "---\n" m = MigrationJob.new(params[:job]) @@ -94,9 +94,9 @@ class MigrationService < Sinatra::Base $log.debug(e.backtrace) y << JSON.generate({:type => :error, :body => e.to_s}) + "---\n" ensure - $log = $syslog + $log = syslog $log.close - $busy = false + $busy.value = false end end end diff --git a/app/views/busy.erb b/app/views/busy.erb new file mode 100644 index 00000000..9d4e6177 --- /dev/null +++ b/app/views/busy.erb @@ -0,0 +1,3 @@ +

Archon Migration Service is Busy

+ +

Another user is running a migration. Please try again later

\ No newline at end of file