Skip to content

Commit

Permalink
use atomic flag to ensure only one job runs at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
quoideneuf committed Jan 7, 2014
1 parent 1089ebf commit a170e1d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
4 changes: 0 additions & 4 deletions app/lib/migrate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -51,7 +48,6 @@ def initialize(params = {})
FileUtils.rm_rf(download_path)
end

Thread.current[:archon_migration_job] = self
end


Expand Down
34 changes: 17 additions & 17 deletions app/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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])
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions app/views/busy.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>Archon Migration Service is Busy</h1>

<p>Another user is running a migration. Please try again later</p>

0 comments on commit a170e1d

Please sign in to comment.