Commit 7d378e93 authored by Stan Hu's avatar Stan Hu

Merge branch '216527-time-zone-usaage-workers' into 'master'

Update workers usage of Time.now => Time.current

See merge request gitlab-org/gitlab!35108
parents 23f894fc c7b6b46a
...@@ -450,6 +450,11 @@ Rails/TimeZone: ...@@ -450,6 +450,11 @@ Rails/TimeZone:
- 'spec/models/**/*' - 'spec/models/**/*'
- 'ee/app/models/**/*' - 'ee/app/models/**/*'
- 'ee/spec/models/**/*' - 'ee/spec/models/**/*'
- 'app/workers/**/*'
- 'spec/workers/**/*'
- 'ee/app/workers/**/*'
- 'ee/spec/workers/**/*'
# WIP: See https://gitlab.com/gitlab-org/gitlab/-/issues/220040 # WIP: See https://gitlab.com/gitlab-org/gitlab/-/issues/220040
Rails/SaveBang: Rails/SaveBang:
......
...@@ -73,7 +73,7 @@ module Reenqueuer ...@@ -73,7 +73,7 @@ module Reenqueuer
# end # end
# #
def ensure_minimum_duration(minimum_duration) def ensure_minimum_duration(minimum_duration)
start_time = Time.now start_time = Time.current
result = yield result = yield
...@@ -95,7 +95,7 @@ module Reenqueuer ...@@ -95,7 +95,7 @@ module Reenqueuer
end end
def elapsed_time(start_time) def elapsed_time(start_time)
Time.now - start_time Time.current - start_time
end end
end end
end end
...@@ -62,7 +62,7 @@ module Gitlab ...@@ -62,7 +62,7 @@ module Gitlab
end end
def build_label_attrs(issue_id, label_id) def build_label_attrs(issue_id, label_id)
time = Time.now time = Time.current
{ {
label_id: label_id, label_id: label_id,
target_id: issue_id, target_id: issue_id,
......
...@@ -80,7 +80,7 @@ class ProcessCommitWorker ...@@ -80,7 +80,7 @@ class ProcessCommitWorker
# manually parse these values. # manually parse these values.
hash.each do |key, value| hash.each do |key, value|
if key.to_s.end_with?(date_suffix) && value.is_a?(String) if key.to_s.end_with?(date_suffix) && value.is_a?(String)
hash[key] = Time.parse(value) hash[key] = Time.zone.parse(value)
end end
end end
......
...@@ -34,7 +34,7 @@ module RepositoryCheck ...@@ -34,7 +34,7 @@ module RepositoryCheck
end end
def perform_repository_checks def perform_repository_checks
start = Time.now start = Time.current
# This loop will break after a little more than one hour ('a little # This loop will break after a little more than one hour ('a little
# more' because `git fsck` may take a few minutes), or if it runs out of # more' because `git fsck` may take a few minutes), or if it runs out of
...@@ -42,7 +42,7 @@ module RepositoryCheck ...@@ -42,7 +42,7 @@ module RepositoryCheck
# RepositoryCheckWorker each hour so that as long as there are repositories to # RepositoryCheckWorker each hour so that as long as there are repositories to
# check, only one (or two) will be checked at a time. # check, only one (or two) will be checked at a time.
project_ids.each do |project_id| project_ids.each do |project_id|
break if Time.now - start >= RUN_TIME break if Time.current - start >= RUN_TIME
next unless try_obtain_lease_for_project(project_id) next unless try_obtain_lease_for_project(project_id)
......
...@@ -17,7 +17,7 @@ module RepositoryCheck ...@@ -17,7 +17,7 @@ module RepositoryCheck
def update_repository_check_status(project, healthy) def update_repository_check_status(project, healthy)
project.update_columns( project.update_columns(
last_repository_check_failed: !healthy, last_repository_check_failed: !healthy,
last_repository_check_at: Time.now last_repository_check_at: Time.current
) )
end end
......
...@@ -49,8 +49,8 @@ module Geo ...@@ -49,8 +49,8 @@ module Geo
job_id = Geo::ProjectSyncWorker.perform_async( job_id = Geo::ProjectSyncWorker.perform_async(
project_id, project_id,
sync_repository: registry.repository_sync_due?(Time.now), sync_repository: registry.repository_sync_due?(Time.current),
sync_wiki: registry.wiki_sync_due?(Time.now) sync_wiki: registry.wiki_sync_due?(Time.current)
) )
{ project_id: project_id, job_id: job_id } if job_id { project_id: project_id, job_id: job_id } if job_id
......
...@@ -30,7 +30,7 @@ module Geo ...@@ -30,7 +30,7 @@ module Geo
# remaining jobs, excluding ones in progress. # remaining jobs, excluding ones in progress.
# 5. Quit when we have scheduled all jobs or exceeded MAX_RUNTIME. # 5. Quit when we have scheduled all jobs or exceeded MAX_RUNTIME.
def perform def perform
@start_time = Time.now.utc @start_time = Time.current.utc
@loops = 0 @loops = 0
# Prevent multiple Sidekiq workers from attempting to schedule jobs # Prevent multiple Sidekiq workers from attempting to schedule jobs
...@@ -65,7 +65,7 @@ module Geo ...@@ -65,7 +65,7 @@ module Geo
log_error(err.message) log_error(err.message)
raise err raise err
ensure ensure
duration = Time.now.utc - start_time duration = Time.current.utc - start_time
log_info('Finished scheduler', total_loops: loops, duration: duration, reason: reason) log_info('Finished scheduler', total_loops: loops, duration: duration, reason: reason)
end end
end end
...@@ -108,7 +108,7 @@ module Geo ...@@ -108,7 +108,7 @@ module Geo
end end
def over_time? def over_time?
(Time.now.utc - start_time) >= run_time (Time.current.utc - start_time) >= run_time
end end
def should_apply_backoff? def should_apply_backoff?
...@@ -198,7 +198,7 @@ module Geo ...@@ -198,7 +198,7 @@ module Geo
def node_enabled? def node_enabled?
# Only check every minute to avoid polling the DB excessively # Only check every minute to avoid polling the DB excessively
unless @last_enabled_check.present? && @last_enabled_check > 1.minute.ago unless @last_enabled_check.present? && @last_enabled_check > 1.minute.ago
@last_enabled_check = Time.now @last_enabled_check = Time.current
clear_memoization(:current_node_enabled) clear_memoization(:current_node_enabled)
end end
......
...@@ -38,7 +38,7 @@ class UpdateAllMirrorsWorker # rubocop:disable Scalability/IdempotentWorker ...@@ -38,7 +38,7 @@ class UpdateAllMirrorsWorker # rubocop:disable Scalability/IdempotentWorker
# Ignore mirrors that become due for scheduling once work begins, so we # Ignore mirrors that become due for scheduling once work begins, so we
# can't end up in an infinite loop # can't end up in an infinite loop
now = Time.now now = Time.current
last = nil last = nil
scheduled = 0 scheduled = 0
...@@ -73,8 +73,8 @@ class UpdateAllMirrorsWorker # rubocop:disable Scalability/IdempotentWorker ...@@ -73,8 +73,8 @@ class UpdateAllMirrorsWorker # rubocop:disable Scalability/IdempotentWorker
if scheduled > 0 if scheduled > 0
# Wait for all ProjectImportScheduleWorker jobs to complete # Wait for all ProjectImportScheduleWorker jobs to complete
deadline = Time.now + SCHEDULE_WAIT_TIMEOUT deadline = Time.current + SCHEDULE_WAIT_TIMEOUT
sleep 1 while ProjectImportScheduleWorker.queue_size > 0 && Time.now < deadline sleep 1 while ProjectImportScheduleWorker.queue_size > 0 && Time.current < deadline
end end
scheduled scheduled
......
...@@ -11,7 +11,7 @@ RSpec.describe Ci::BatchResetMinutesWorker do ...@@ -11,7 +11,7 @@ RSpec.describe Ci::BatchResetMinutesWorker do
id: 1, id: 1,
shared_runners_minutes_limit: 100, shared_runners_minutes_limit: 100,
extra_shared_runners_minutes_limit: 50, extra_shared_runners_minutes_limit: 50,
last_ci_minutes_notification_at: Time.now, last_ci_minutes_notification_at: Time.current,
last_ci_minutes_usage_notification_level: 30) last_ci_minutes_usage_notification_level: 30)
end end
...@@ -20,7 +20,7 @@ RSpec.describe Ci::BatchResetMinutesWorker do ...@@ -20,7 +20,7 @@ RSpec.describe Ci::BatchResetMinutesWorker do
id: 10, id: 10,
shared_runners_minutes_limit: 100, shared_runners_minutes_limit: 100,
extra_shared_runners_minutes_limit: 50, extra_shared_runners_minutes_limit: 50,
last_ci_minutes_notification_at: Time.now, last_ci_minutes_notification_at: Time.current,
last_ci_minutes_usage_notification_level: 30) last_ci_minutes_usage_notification_level: 30)
end end
......
...@@ -36,7 +36,7 @@ RSpec.describe ClearSharedRunnersMinutesWorker do ...@@ -36,7 +36,7 @@ RSpec.describe ClearSharedRunnersMinutesWorker do
it 'resets timer' do it 'resets timer' do
subject subject
expect(statistics.reload.shared_runners_seconds_last_reset).to be_like_time(Time.now) expect(statistics.reload.shared_runners_seconds_last_reset).to be_like_time(Time.current)
end end
context 'when there are namespaces that were not reset after the reset steps' do context 'when there are namespaces that were not reset after the reset steps' do
...@@ -68,7 +68,7 @@ RSpec.describe ClearSharedRunnersMinutesWorker do ...@@ -68,7 +68,7 @@ RSpec.describe ClearSharedRunnersMinutesWorker do
it 'resets timer' do it 'resets timer' do
subject subject
expect(statistics.reload.shared_runners_seconds_last_reset).to be_like_time(Time.now) expect(statistics.reload.shared_runners_seconds_last_reset).to be_like_time(Time.current)
end end
end end
...@@ -118,7 +118,7 @@ RSpec.describe ClearSharedRunnersMinutesWorker do ...@@ -118,7 +118,7 @@ RSpec.describe ClearSharedRunnersMinutesWorker do
[:last_ci_minutes_notification_at, :last_ci_minutes_usage_notification_level].each do |attr| [:last_ci_minutes_notification_at, :last_ci_minutes_usage_notification_level].each do |attr|
context "when #{attr} is present" do context "when #{attr} is present" do
before do before do
namespace.update_attribute(attr, Time.now) namespace.update_attribute(attr, Time.current)
end end
it 'nullifies the field' do it 'nullifies the field' do
......
...@@ -12,7 +12,7 @@ describe ClusterUpdateAppWorker do ...@@ -12,7 +12,7 @@ describe ClusterUpdateAppWorker do
subject { described_class.new } subject { described_class.new }
around do |example| around do |example|
Timecop.freeze(Time.now) { example.run } Timecop.freeze(Time.current) { example.run }
end end
before do before do
...@@ -22,11 +22,11 @@ describe ClusterUpdateAppWorker do ...@@ -22,11 +22,11 @@ describe ClusterUpdateAppWorker do
describe '#perform' do describe '#perform' do
context 'when the application last_update_started_at is higher than the time the job was scheduled in' do context 'when the application last_update_started_at is higher than the time the job was scheduled in' do
it 'does nothing' do it 'does nothing' do
application = create(:clusters_applications_prometheus, :updated, last_update_started_at: Time.now) application = create(:clusters_applications_prometheus, :updated, last_update_started_at: Time.current)
expect(prometheus_update_service).not_to receive(:execute) expect(prometheus_update_service).not_to receive(:execute)
expect(subject.perform(application.name, application.id, project.id, Time.now - 5.minutes)).to be_nil expect(subject.perform(application.name, application.id, project.id, Time.current - 5.minutes)).to be_nil
end end
end end
...@@ -34,7 +34,7 @@ describe ClusterUpdateAppWorker do ...@@ -34,7 +34,7 @@ describe ClusterUpdateAppWorker do
it 'returns nil' do it 'returns nil' do
application = create(:clusters_applications_prometheus, :updating) application = create(:clusters_applications_prometheus, :updating)
expect(subject.perform(application.name, application.id, project.id, Time.now)).to be_nil expect(subject.perform(application.name, application.id, project.id, Time.current)).to be_nil
end end
end end
...@@ -43,7 +43,7 @@ describe ClusterUpdateAppWorker do ...@@ -43,7 +43,7 @@ describe ClusterUpdateAppWorker do
expect(prometheus_update_service).to receive(:execute) expect(prometheus_update_service).to receive(:execute)
subject.perform(application.name, application.id, project.id, Time.now) subject.perform(application.name, application.id, project.id, Time.current)
end end
context 'with exclusive lease' do context 'with exclusive lease' do
...@@ -60,7 +60,7 @@ describe ClusterUpdateAppWorker do ...@@ -60,7 +60,7 @@ describe ClusterUpdateAppWorker do
it 'does not allow same app to be updated concurrently by same project' do it 'does not allow same app to be updated concurrently by same project' do
expect(Clusters::Applications::PrometheusUpdateService).not_to receive(:new) expect(Clusters::Applications::PrometheusUpdateService).not_to receive(:new)
subject.perform(application.name, application.id, project.id, Time.now) subject.perform(application.name, application.id, project.id, Time.current)
end end
it 'does not allow same app to be updated concurrently by different project', :aggregate_failures do it 'does not allow same app to be updated concurrently by different project', :aggregate_failures do
...@@ -68,7 +68,7 @@ describe ClusterUpdateAppWorker do ...@@ -68,7 +68,7 @@ describe ClusterUpdateAppWorker do
expect(Clusters::Applications::PrometheusUpdateService).not_to receive(:new) expect(Clusters::Applications::PrometheusUpdateService).not_to receive(:new)
subject.perform(application.name, application.id, project1.id, Time.now) subject.perform(application.name, application.id, project1.id, Time.current)
end end
it 'allows different app to be updated concurrently by same project' do it 'allows different app to be updated concurrently by same project' do
...@@ -80,7 +80,7 @@ describe ClusterUpdateAppWorker do ...@@ -80,7 +80,7 @@ describe ClusterUpdateAppWorker do
expect(Clusters::Applications::PrometheusUpdateService).to receive(:new) expect(Clusters::Applications::PrometheusUpdateService).to receive(:new)
.with(application2, project) .with(application2, project)
subject.perform(application2.name, application2.id, project.id, Time.now) subject.perform(application2.name, application2.id, project.id, Time.current)
end end
it 'allows different app to be updated by different project', :aggregate_failures do it 'allows different app to be updated by different project', :aggregate_failures do
...@@ -94,7 +94,7 @@ describe ClusterUpdateAppWorker do ...@@ -94,7 +94,7 @@ describe ClusterUpdateAppWorker do
expect(Clusters::Applications::PrometheusUpdateService).to receive(:new) expect(Clusters::Applications::PrometheusUpdateService).to receive(:new)
.with(application2, project2) .with(application2, project2)
subject.perform(application2.name, application2.id, project2.id, Time.now) subject.perform(application2.name, application2.id, project2.id, Time.current)
end end
end end
end end
......
...@@ -32,7 +32,7 @@ describe ExpireBuildInstanceArtifactsWorker do ...@@ -32,7 +32,7 @@ describe ExpireBuildInstanceArtifactsWorker do
context 'with not yet expired artifacts' do context 'with not yet expired artifacts' do
let_it_be(:build) do let_it_be(:build) do
create(:ci_build, :artifacts, artifacts_expire_at: Time.now + 7.days) create(:ci_build, :artifacts, artifacts_expire_at: Time.current + 7.days)
end end
it 'does not expire' do it 'does not expire' do
......
...@@ -18,7 +18,7 @@ describe PipelineMetricsWorker do ...@@ -18,7 +18,7 @@ describe PipelineMetricsWorker do
ref: 'master', ref: 'master',
sha: project.repository.commit('master').id, sha: project.repository.commit('master').id,
started_at: 1.hour.ago, started_at: 1.hour.ago,
finished_at: Time.now) finished_at: Time.current)
end end
let(:status) { 'pending' } let(:status) { 'pending' }
......
...@@ -33,7 +33,7 @@ describe PipelineScheduleWorker do ...@@ -33,7 +33,7 @@ describe PipelineScheduleWorker do
expect(Ci::Pipeline.last).to be_schedule expect(Ci::Pipeline.last).to be_schedule
pipeline_schedule.reload pipeline_schedule.reload
expect(pipeline_schedule.next_run_at).to be > Time.now expect(pipeline_schedule.next_run_at).to be > Time.current
expect(pipeline_schedule).to eq(project.ci_pipelines.last.pipeline_schedule) expect(pipeline_schedule).to eq(project.ci_pipelines.last.pipeline_schedule)
expect(pipeline_schedule).to be_active expect(pipeline_schedule).to be_active
end end
......
...@@ -200,9 +200,9 @@ describe ProcessCommitWorker do ...@@ -200,9 +200,9 @@ describe ProcessCommitWorker do
it 'parses date strings into Time instances' do it 'parses date strings into Time instances' do
commit = worker.build_commit(project, commit = worker.build_commit(project,
id: '123', id: '123',
authored_date: Time.now.to_s) authored_date: Time.current.to_s)
expect(commit.authored_date).to be_an_instance_of(Time) expect(commit.authored_date).to be_a_kind_of(Time)
end end
end end
end end
...@@ -7,7 +7,7 @@ describe RepositoryCheck::ClearWorker do ...@@ -7,7 +7,7 @@ describe RepositoryCheck::ClearWorker do
project = create(:project) project = create(:project)
project.update_columns( project.update_columns(
last_repository_check_failed: true, last_repository_check_failed: true,
last_repository_check_at: Time.now last_repository_check_at: Time.current
) )
described_class.new.perform described_class.new.perform
......
...@@ -6,10 +6,10 @@ describe RepositoryUpdateRemoteMirrorWorker, :clean_gitlab_redis_shared_state do ...@@ -6,10 +6,10 @@ describe RepositoryUpdateRemoteMirrorWorker, :clean_gitlab_redis_shared_state do
subject { described_class.new } subject { described_class.new }
let(:remote_mirror) { create(:remote_mirror) } let(:remote_mirror) { create(:remote_mirror) }
let(:scheduled_time) { Time.now - 5.minutes } let(:scheduled_time) { Time.current - 5.minutes }
around do |example| around do |example|
Timecop.freeze(Time.now) { example.run } Timecop.freeze(Time.current) { example.run }
end end
def expect_mirror_service_to_return(mirror, result, tries = 0) def expect_mirror_service_to_return(mirror, result, tries = 0)
...@@ -26,7 +26,7 @@ describe RepositoryUpdateRemoteMirrorWorker, :clean_gitlab_redis_shared_state do ...@@ -26,7 +26,7 @@ describe RepositoryUpdateRemoteMirrorWorker, :clean_gitlab_redis_shared_state do
end end
it 'does not do anything if the mirror was already updated' do it 'does not do anything if the mirror was already updated' do
remote_mirror.update(last_update_started_at: Time.now, update_status: :finished) remote_mirror.update(last_update_started_at: Time.current, update_status: :finished)
expect(Projects::UpdateRemoteMirrorService).not_to receive(:new) expect(Projects::UpdateRemoteMirrorService).not_to receive(:new)
...@@ -48,7 +48,7 @@ describe RepositoryUpdateRemoteMirrorWorker, :clean_gitlab_redis_shared_state do ...@@ -48,7 +48,7 @@ describe RepositoryUpdateRemoteMirrorWorker, :clean_gitlab_redis_shared_state do
expect_next_instance_of(Projects::UpdateRemoteMirrorService) do |service| expect_next_instance_of(Projects::UpdateRemoteMirrorService) do |service|
expect(service).to receive(:execute).with(remote_mirror, 1).and_raise('Unexpected!') expect(service).to receive(:execute).with(remote_mirror, 1).and_raise('Unexpected!')
end end
expect { subject.perform(remote_mirror.id, Time.now, 1) }.to raise_error('Unexpected!') expect { subject.perform(remote_mirror.id, Time.current, 1) }.to raise_error('Unexpected!')
lease = Gitlab::ExclusiveLease.new("#{described_class.name}:#{remote_mirror.id}", timeout: 1.second) lease = Gitlab::ExclusiveLease.new("#{described_class.name}:#{remote_mirror.id}", timeout: 1.second)
......
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