Skip to content

Commit

Permalink
Make the default unit of retention period to be days
Browse files Browse the repository at this point in the history
  • Loading branch information
Malsourie committed May 16, 2024
1 parent a3b0b73 commit e905dcd
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 27 deletions.
4 changes: 2 additions & 2 deletions jobs/director/spec
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ properties:
description: Max number of tasks per each type to keep in disk
default: 2000
director.tasks_retention_period:
description: the retention period for tasks and their log files
description: the retention period for tasks and their log files (days)
default: ''
director.tasks_deployments_retention_period:
description: the retention period for tasks and their log files of specific deployments
description: the retention period for tasks and their log files of specific deployments (days)
default: ''
director.max_threads:
description: Max number of director concurrent threads
Expand Down
13 changes: 2 additions & 11 deletions src/bosh-director/lib/bosh/director/api/task_remover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,20 @@ def removal_max_tasks_candidates_dataset(type)
end

def removal_retention_candidates_dataset(type)
retention_time = DateTime.now.to_time - convert_to_time_duration(@retention_period)
retention_time = DateTime.now - @retention_period.to_i
Bosh::Director::Models::Task.where(type: type)
.where { checkpoint_time < retention_time }
.exclude(state: %w[processing queued])
.select(:id, :output)
end

def removal_deployment_retention_candidates_dataset(type, deployment_with_retention_period)
retention_time = DateTime.now.to_time - convert_to_time_duration(deployment_with_retention_period['retention_period'])
retention_time = DateTime.now - deployment_with_retention_period['retention_period'].to_i
Bosh::Director::Models::Task.where(type: type)
.where(deployment_name: deployment_with_retention_period['deployment'])
.where { checkpoint_time < retention_time }
.exclude(state: %w[processing queued])
.select(:id, :output)
end

def convert_to_time_duration(string)
multipliers = { "d" => 24*60*60, "h" => 60*60, "m" => 60, "s" => 1 }
segments = string.scan(/(\d+)([a-z])/)

segments.inject(0) do |total, (value, unit)|
total + value.to_i * multipliers[unit]
end
end
end
end
4 changes: 2 additions & 2 deletions src/bosh-director/spec/assets/test-director-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ logging:
level: ERROR

max_tasks: 2000
tasks_retention_period: 20d
tasks_retention_period: 20
tasks_deployments_retention_period:
- deployment: fake-deployment
retention_period: 30d
retention_period: 30

dir: /tmp/boshdir
director_certificate_expiry_json_path: /tmp/boshdir/certificate_expiry.json
Expand Down
19 changes: 9 additions & 10 deletions src/bosh-director/spec/unit/api/task_remover_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,7 @@ def tasks

context 'when there are tasks exceeding the retention period 1 day in the database' do
subject(:remover) do
Timecop.freeze(Time.parse(inside_retention) + 60 * 60) do
TaskRemover.new(2000, '1d', '')
end

TaskRemover.new(2000, '1', '')
end

before do
Expand All @@ -186,18 +183,18 @@ def tasks
end

it 'it removes the task outside retention' do
expect(remover).to_not receive(:remove_task).with(tasks[0])
expect(remover).to receive(:remove_task).with(tasks[1])

remover.remove(default_type)
Timecop.freeze(Time.parse(inside_retention) + 60 * 60) do
remover.remove(default_type)
end
end
end

context 'when there is task exceeding the retention period 1 day in the database' do
subject(:remover) do
Timecop.freeze(Time.parse(inside_retention) + 60 * 60) do
TaskRemover.new(2000, '', [{ 'deployment' => 'deployment1', 'retention_period' => '1d' }])
end

TaskRemover.new(2000, '', [{ 'deployment' => 'deployment1', 'retention_period' => '1' }])
end

before do
Expand All @@ -209,7 +206,9 @@ def tasks
expect(remover).to receive(:remove_task).with(tasks[0])
expect(remover).to_not receive(:remove_task).with(tasks[1])

remover.remove(default_type)
Timecop.freeze(Time.parse(inside_retention) + 60 * 60) do
remover.remove(default_type)
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/bosh-director/spec/unit/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,8 @@
it 'returns the task remover configurations' do
described_class.configure(test_config)
expect(described_class.max_tasks).to eq(2000)
expect(described_class.tasks_retention_period).to eq('20d')
expect(described_class.tasks_deployments_retention_period).to eq([{'deployment' => 'fake-deployment', 'retention_period' => '30d'}])
expect(described_class.tasks_retention_period).to eq('20')
expect(described_class.tasks_deployments_retention_period).to eq([{'deployment' => 'fake-deployment', 'retention_period' => '30'}])
end
end
end
Expand Down

0 comments on commit e905dcd

Please sign in to comment.