Merge pull request #12837 from NickHackman/service-launch-only-once

service: launch only once
This commit is contained in:
Mike McQuaid 2022-02-14 09:17:44 +00:00 committed by GitHub
commit 249bc2136a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 5 deletions

View File

@ -112,6 +112,18 @@ module Homebrew
end end
end end
sig { params(value: T.nilable(T::Boolean)).returns(T.nilable(T::Boolean)) }
def launch_only_once(value = nil)
case T.unsafe(value)
when nil
@launch_only_once
when true, false
@launch_only_once = value
else
raise TypeError, "Service#launch_only_once expects a Boolean"
end
end
sig { params(value: T.nilable(Integer)).returns(T.nilable(Integer)) } sig { params(value: T.nilable(Integer)).returns(T.nilable(Integer)) }
def restart_delay(value = nil) def restart_delay(value = nil)
case T.unsafe(value) case T.unsafe(value)
@ -291,6 +303,7 @@ module Homebrew
} }
base[:KeepAlive] = @keep_alive if @keep_alive == true base[:KeepAlive] = @keep_alive if @keep_alive == true
base[:LaunchOnlyOnce] = @launch_only_once if @launch_only_once == true
base[:LegacyTimers] = @macos_legacy_timers if @macos_legacy_timers == true base[:LegacyTimers] = @macos_legacy_timers if @macos_legacy_timers == true
base[:TimeOut] = @restart_delay if @restart_delay.present? base[:TimeOut] = @restart_delay if @restart_delay.present?
base[:ProcessType] = @process_type.to_s.capitalize if @process_type.present? base[:ProcessType] = @process_type.to_s.capitalize if @process_type.present?
@ -321,11 +334,14 @@ module Homebrew
WantedBy=multi-user.target WantedBy=multi-user.target
[Service] [Service]
Type=simple
ExecStart=#{command.join(" ")}
EOS EOS
# command needs to be first because it initializes all other values
cmd = command.join(" ")
options = [] options = []
options << "Type=#{@launch_only_once == true ? "oneshot" : "simple"}"
options << "ExecStart=#{cmd}"
options << "Restart=always" if @keep_alive == true options << "Restart=always" if @keep_alive == true
options << "RestartSec=#{restart_delay}" if @restart_delay.present? options << "RestartSec=#{restart_delay}" if @restart_delay.present?
options << "WorkingDirectory=#{@working_dir}" if @working_dir.present? options << "WorkingDirectory=#{@working_dir}" if @working_dir.present?

View File

@ -102,6 +102,7 @@ describe Homebrew::Service do
root_dir var root_dir var
working_dir var working_dir var
keep_alive true keep_alive true
launch_only_once true
process_type :interactive process_type :interactive
restart_delay 30 restart_delay 30
interval 5 interval 5
@ -127,6 +128,8 @@ describe Homebrew::Service do
\t<true/> \t<true/>
\t<key>Label</key> \t<key>Label</key>
\t<string>homebrew.mxcl.formula_name</string> \t<string>homebrew.mxcl.formula_name</string>
\t<key>LaunchOnlyOnce</key>
\t<true/>
\t<key>LegacyTimers</key> \t<key>LegacyTimers</key>
\t<true/> \t<true/>
\t<key>ProcessType</key> \t<key>ProcessType</key>
@ -288,10 +291,11 @@ describe Homebrew::Service do
expect(unit).to eq(unit_expect.strip) expect(unit).to eq(unit_expect.strip)
end end
it "returns valid partial unit" do it "returns valid partial oneshot unit" do
f.class.service do f.class.service do
run opt_bin/"beanstalkd" run opt_bin/"beanstalkd"
run_type :immediate run_type :immediate
launch_only_once true
end end
unit = f.service.to_systemd_unit unit = f.service.to_systemd_unit
@ -303,10 +307,10 @@ describe Homebrew::Service do
WantedBy=multi-user.target WantedBy=multi-user.target
[Service] [Service]
Type=simple Type=oneshot
ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd
EOS EOS
expect(unit).to eq(unit_expect) expect(unit).to eq(unit_expect.strip)
end end
end end