Skip to content
Snippets Groups Projects
Commit 0dc32274 authored by Hossein Pursultani's avatar Hossein Pursultani Committed by Ian Baum
Browse files

Support units other than milliseconds for the pg-upgrade timeout option

parent 648d1ec7
No related branches found
No related tags found
No related merge requests found
---
title: Support units other than milliseconds for the pg-upgrade timeout option
merge_request: 4493
author:
type: added
require 'optparse'
require 'mixlib/shellout'
require_relative 'util'
require_relative '../gitlab_ctl'
 
Loading
Loading
@@ -178,8 +180,8 @@ module GitlabCtl
options[:skip_unregister] = true
end
 
opts.on('-TTIMEOUT', '--timeout=TIMEOUT', 'Timeout in milliseconds for the execution of the underlying commands') do |t|
i = t.to_i
opts.on('-TTIMEOUT', '--timeout=TIMEOUT', 'Timeout in milliseconds for the execution of the underlying commands. Accepts duration format such as 1d2h3m4s5ms.') do |t|
i = GitlabCtl::Util.parse_duration(t)
options[:timeout] = i.positive? ? i : nil
end
 
Loading
Loading
Loading
Loading
@@ -127,6 +127,38 @@ module GitlabCtl
end
results
end
def warn(message)
$stderr.print "\r\e[33m#{message}\e[0m\n"
end
DURATION_UNITS = {
'ms' => 1,
's' => 1000,
'm' => 1000 * 60,
'h' => 1000 * 60 * 60,
'd' => 1000 * 60 * 60 * 24
}.freeze
def parse_duration(duration)
millis = 0
duration&.scan(/(?<quantity>\d+(\.\d+)?)(?<unit>[a-zA-Z]+)/)&.each do |quantity, unit|
multiplier = DURATION_UNITS[unit]
break if multiplier.nil?
millis += multiplier * quantity.to_f
end
begin
millis = Float(duration || '') if millis.zero?
rescue ArgumentError
# Translating exception
raise ArgumentError, "invalid value for duration: `#{duration}`"
end
millis.to_i
end
end
end
end
Loading
Loading
@@ -24,7 +24,13 @@ REVERT_VERSION_FILE = "#{data_path}/postgresql-version.old".freeze
add_command_under_category 'revert-pg-upgrade', 'database',
'Run this to revert to the previous version of the database',
2 do |_cmd_name|
options = GitlabCtl::PgUpgrade.parse_options(ARGV)
begin
options = GitlabCtl::PgUpgrade.parse_options(ARGV)
rescue ArgumentError => e
log "Command line parameter error: #{e.message}"
Kernel.exit 64
end
revert_version = lookup_version(options[:target_version], read_revert_version || default_version)
 
@attributes = GitlabCtl::Util.get_node_attributes(base_path)
Loading
Loading
Loading
Loading
@@ -113,4 +113,55 @@ RSpec.describe GitlabCtl::Util do
expect { GitlabCtl::Util.parse_json_file('/opt/gitlab/embedded/nodes/12345.json') }.to raise_error(GitlabCtl::Errors::NodeError, "Attributes not found in /opt/gitlab/embedded/nodes/12345.json, has reconfigure been run yet?")
end
end
describe '#parse_duration' do
it 'should raise error for nil, empty, or malformed inputs' do
expect { GitlabCtl::Util.parse_duration(nil) }.to raise_error ArgumentError, 'invalid value for duration: ``'
expect { GitlabCtl::Util.parse_duration('') }.to raise_error ArgumentError, 'invalid value for duration: ``'
expect { GitlabCtl::Util.parse_duration('foo') }.to raise_error ArgumentError, 'invalid value for duration: `foo`'
expect { GitlabCtl::Util.parse_duration('123foo') }.to raise_error ArgumentError, 'invalid value for duration: `123foo`'
expect { GitlabCtl::Util.parse_duration('foo123') }.to raise_error ArgumentError, 'invalid value for duration: `foo123`'
end
it 'should parse unformatted inputs into milliseconds' do
expect(GitlabCtl::Util.parse_duration('123')).to eq(123)
expect(GitlabCtl::Util.parse_duration('123.456')).to eq(123)
end
it 'should recognize and parse different duration units' do
expect(GitlabCtl::Util.parse_duration('123.456ms')).to eq(123)
expect(GitlabCtl::Util.parse_duration('123.456s')).to eq(123.456 * 1000)
expect(GitlabCtl::Util.parse_duration('123.456m')).to eq(123.456 * 1000 * 60)
expect(GitlabCtl::Util.parse_duration('123.456h')).to eq(123.456 * 1000 * 60 * 60)
expect(GitlabCtl::Util.parse_duration('123.456d')).to eq(123.456 * 1000 * 60 * 60 * 24)
end
it 'should parse mixed unit inputs in any order' do
expect(GitlabCtl::Util.parse_duration('1.1d2.2h3.3m4.4s5.5ms')).to eq(
1.1 * 1000 * 60 * 60 * 24 +
2.2 * 1000 * 60 * 60 +
3.3 * 1000 * 60 +
4.4 * 1000 +
5
)
expect(GitlabCtl::Util.parse_duration('5.5ms4.4s3.3m2.2h1.1d')).to eq(
1.1 * 1000 * 60 * 60 * 24 +
2.2 * 1000 * 60 * 60 +
3.3 * 1000 * 60 +
4.4 * 1000 +
5
)
end
it 'should break and return when input is partially valid' do
expect(GitlabCtl::Util.parse_duration('1h2m3foo')).to eq(
1 * 1000 * 60 * 60 +
2 * 1000 * 60
)
expect(GitlabCtl::Util.parse_duration('1h2m3')).to eq(
1 * 1000 * 60 * 60 +
2 * 1000 * 60
)
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment