156 行
5.3 KiB
Ruby
156 行
5.3 KiB
Ruby
require 'fileutils'
|
|
require 'timeout'
|
|
|
|
# Development PoC: one filesystem slot and Rails' in-process async executor.
|
|
# A process restart loses queued work; stale state fails rather than reporting success.
|
|
class StaticMirrorJob < ApplicationJob
|
|
self.queue_adapter = :async
|
|
EXPIRY = 10.minutes
|
|
COOLDOWN = 60
|
|
MAX_SECONDS = 60
|
|
class Busy < StandardError; end
|
|
|
|
def self.directory = Rails.root.join('tmp/static_mirror')
|
|
|
|
def self.synchronise(blocking: false)
|
|
FileUtils.mkdir_p(directory, mode: 0o700)
|
|
File.open(directory.join('.registry.lock'), File::RDWR | File::CREAT, 0o600) do |lock|
|
|
mode = blocking ? File::LOCK_EX : File::LOCK_EX | File::LOCK_NB
|
|
raise Busy unless lock.flock(mode)
|
|
|
|
yield
|
|
end
|
|
end
|
|
|
|
def self.state
|
|
JSON.parse(directory.join('state.json').read)
|
|
rescue Errno::ENOENT
|
|
nil
|
|
end
|
|
|
|
def self.write(value)
|
|
path = directory.join('state.tmp')
|
|
File.open(path, 'w', 0o600) { _1.write(JSON.generate(value)) }
|
|
File.rename(path, directory.join('state.json'))
|
|
ensure
|
|
File.unlink(path) if path&.exist?
|
|
end
|
|
|
|
def self.start(owner:, locale:, origin:)
|
|
id = synchronise do
|
|
previous = state
|
|
if previous && Time.current.to_f - previous.fetch('created_at') < COOLDOWN
|
|
raise Busy
|
|
end
|
|
File.open(directory.join('.generation.lock'), File::RDWR | File::CREAT, 0o600) do |lock|
|
|
raise Busy unless lock.flock(File::LOCK_EX | File::LOCK_NB)
|
|
|
|
FileUtils.rm_f(directory.join('archive.zip'))
|
|
FileUtils.rm_f(directory.join('archive.part'))
|
|
identifier = SecureRandom.hex(24)
|
|
write({ 'id' => identifier, 'owner' => owner, 'locale' => locale, 'origin' => origin,
|
|
'created_at' => Time.current.to_f, 'expiry_at' => (Time.current + EXPIRY).to_f,
|
|
'status' => 'queued', 'processed' => 0, 'total' => nil, 'data_at' => nil })
|
|
identifier
|
|
end
|
|
end
|
|
perform_later(id)
|
|
id
|
|
rescue ActiveJob::EnqueueError
|
|
update(id) { _1.merge!('status' => 'failed', 'error' => 'generation_failed') } if id
|
|
raise
|
|
end
|
|
|
|
def self.authorised_state(id, owner)
|
|
value = state
|
|
return unless owner.present? && value && value['id'] == id && value['owner'] == owner
|
|
return unless value['expiry_at'] > Time.current.to_f
|
|
|
|
if (['queued', 'running'].include?(value['status']) &&
|
|
Time.current.to_f - value['created_at'] > MAX_SECONDS + 15)
|
|
value.merge!('status' => 'failed', 'error' => 'worker_stopped')
|
|
write(value)
|
|
end
|
|
if value['status'] == 'ready'
|
|
permitted = HistoryAccess.set(user: nil) { HistoryAccess.authorised_artifact?(value) }
|
|
return unless permitted && directory.join('archive.zip').file?
|
|
end
|
|
value
|
|
end
|
|
|
|
def self.status(id, owner)
|
|
synchronise do
|
|
authorised_state(id, owner)&.slice('status', 'processed', 'total', 'data_at', 'error')
|
|
end
|
|
end
|
|
|
|
def self.download(id, owner)
|
|
synchronise do
|
|
value = authorised_state(id, owner)
|
|
return unless value && value['status'] == 'ready'
|
|
|
|
bytes = directory.join('archive.zip').binread
|
|
# Revalidate after reading; send_data avoids delayed sendfile reuse of a stale path.
|
|
permitted = HistoryAccess.set(user: nil) { HistoryAccess.authorised_artifact?(value) }
|
|
permitted ? bytes : nil
|
|
end
|
|
end
|
|
|
|
def self.update(id)
|
|
# Worker updates may wait for a short status/download read; request intake never waits.
|
|
synchronise(blocking: true) do
|
|
value = state
|
|
return unless value && value['id'] == id
|
|
|
|
yield value
|
|
write(value)
|
|
end
|
|
end
|
|
|
|
def perform(id)
|
|
File.open(self.class.directory.join('.generation.lock'), File::RDWR) do |lock|
|
|
return unless lock.flock(File::LOCK_EX | File::LOCK_NB)
|
|
|
|
value = self.class.state
|
|
return unless value && value['id'] == id && value['status'] == 'queued'
|
|
|
|
generate(id, value)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def generate(id, value)
|
|
self.class.update(id) { _1['status'] = 'running' }
|
|
remaining = MAX_SECONDS - (Time.current.to_f - value.fetch('created_at'))
|
|
raise Timeout::Error unless remaining.positive?
|
|
|
|
Timeout.timeout(remaining) do
|
|
locale = Locale.find_by!(code: value.fetch('locale'), deprecated_at: nil)
|
|
mirror = StaticMirror.new(locale:, origin: value.fetch('origin'))
|
|
bytes = mirror.export do |processed, total|
|
|
self.class.update(id) do |metadata|
|
|
metadata.merge!('processed' => processed, 'total' => total, 'data_at' => mirror.data_at)
|
|
end
|
|
end
|
|
part = self.class.directory.join('archive.part')
|
|
File.open(part, 'wb', 0o600) { _1.write(bytes) }
|
|
File.rename(part, self.class.directory.join('archive.zip'))
|
|
self.class.update(id) do |metadata|
|
|
metadata.merge!(mirror.authority).merge!('status' => 'ready', 'data_at' => mirror.data_at)
|
|
end
|
|
end
|
|
rescue StandardError => error
|
|
FileUtils.rm_f(self.class.directory.join('archive.part'))
|
|
FileUtils.rm_f(self.class.directory.join('archive.zip'))
|
|
code = case error
|
|
when StaticMirror::TooLarge then 'too_large'
|
|
when Timeout::Error then 'generation_timeout'
|
|
when HistoryAccess::Forbidden then 'publication_changed'
|
|
else 'generation_failed'
|
|
end
|
|
self.class.update(id) { _1.merge!('status' => 'failed', 'error' => code) }
|
|
Rails.logger.warn("static_mirror_failure: #{ error.class }")
|
|
end
|
|
end
|