Commit 32c1501d authored by Shinya Maeda's avatar Shinya Maeda

Add rake task. Adopt the latest fix. Drop CreateTraceArtifactService

parent f0d7a2ff
module Ci
class CreateTraceArtifactService < BaseService
def execute(job)
return if job.job_artifacts_trace
job.trace.read do |stream|
break unless stream.file?
clone_file!(stream.path, JobArtifactUploader.workhorse_upload_path) do |clone_path|
create_job_trace!(job, clone_path)
FileUtils.rm(stream.path)
end
end
end
private
def create_job_trace!(job, path)
File.open(path) do |stream|
job.create_job_artifacts_trace!(
project: job.project,
file_type: :trace,
file: stream)
end
end
def clone_file!(src_path, temp_dir)
FileUtils.mkdir_p(temp_dir)
Dir.mktmpdir('tmp-trace', temp_dir) do |dir_path|
temp_path = File.join(dir_path, "job.log")
FileUtils.copy(src_path, temp_path)
yield(temp_path)
end
end
end
end
class ArchiveLegacyTraceWorker
include ApplicationWorker
include ObjectStorageQueue
def perform(job_id)
Ci::Build.find_by(id: job_id).try do |job|
job.trace.archive!
end
end
end
......@@ -2,10 +2,8 @@ class CreateTraceArtifactWorker
include ApplicationWorker
include PipelineQueue
# TODO: this worker should use BackgroundMigration or ObjectStorage queue
def perform(job_id)
Ci::Build.preload(:project, :user).find_by(id: job_id).try do |job|
Ci::Build.find_by(id: job_id).try do |job|
job.trace.archive!
end
end
......
......@@ -112,18 +112,28 @@ module Gitlab
private
def archive_stream!(stream)
file = Tempfile.new('trace.log')
size = IO.copy_stream(file, stream)
raise 'Not all saved' unless size == stream.size
file.close
job.create_job_artifacts_trace!(
project: job.project,
file_type: :trace,
file: file)
ensure
file&.close
file&.unlink
clone_file!(stream, JobArtifactUploader.workhorse_upload_path) do |clone_path|
create_job_trace!(job, clone_path)
end
end
def clone_file!(src_stream, temp_dir)
FileUtils.mkdir_p(temp_dir)
Dir.mktmpdir('tmp-trace', temp_dir) do |dir_path|
temp_path = File.join(dir_path, "job.log")
size = IO.write(src_stream, temp_path)
raise 'Not all saved' unless size == src_stream.size
yield(temp_path)
end
end
def create_job_trace!(job, path)
File.open(path) do |stream|
job.create_job_artifacts_trace!(
project: job.project,
file_type: :trace,
file: stream)
end
end
def ensure_path
......
require 'logger'
require 'resolv-replace'
desc "GitLab | Archive legacy traces to trace artifacts"
namespace :gitlab do
namespace :traces do
task archive: :environment do
logger = Logger.new(STDOUT)
logger.info('Archiving legacy traces')
job_ids = Ci::Build.complete.without_trace_artifact.pluck(:id)
job_ids = job_ids.map { |build_id| [build_id] }
ArchiveLegacyTraceWorker.bulk_perform_async(job_ids)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment