Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service: launch only once #12837

Merged
merged 4 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Library/Homebrew/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ def keep_alive(value = nil)
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
NickHackman marked this conversation as resolved.
Show resolved Hide resolved

sig { params(value: T.nilable(Integer)).returns(T.nilable(Integer)) }
def restart_delay(value = nil)
case T.unsafe(value)
Expand Down Expand Up @@ -291,6 +303,7 @@ def to_plist
}

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[:TimeOut] = @restart_delay if @restart_delay.present?
base[:ProcessType] = @process_type.to_s.capitalize if @process_type.present?
Expand Down Expand Up @@ -321,11 +334,14 @@ def to_systemd_unit
WantedBy=multi-user.target

[Service]
Type=simple
ExecStart=#{command.join(" ")}
EOS

# command needs to be first because it initializes all other values
cmd = command.join(" ")

options = []
options << "Type=#{@launch_only_once == true ? "oneshot" : "simple"}"
options << "ExecStart=#{cmd}"
options << "Restart=always" if @keep_alive == true
options << "RestartSec=#{restart_delay}" if @restart_delay.present?
options << "WorkingDirectory=#{@working_dir}" if @working_dir.present?
Expand Down
10 changes: 7 additions & 3 deletions Library/Homebrew/test/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
root_dir var
working_dir var
keep_alive true
launch_only_once true
process_type :interactive
restart_delay 30
interval 5
Expand All @@ -127,6 +128,8 @@
\t<true/>
\t<key>Label</key>
\t<string>homebrew.mxcl.formula_name</string>
\t<key>LaunchOnlyOnce</key>
\t<true/>
\t<key>LegacyTimers</key>
\t<true/>
\t<key>ProcessType</key>
Expand Down Expand Up @@ -288,10 +291,11 @@
expect(unit).to eq(unit_expect.strip)
end

it "returns valid partial unit" do
it "returns valid partial oneshot unit" do
f.class.service do
run opt_bin/"beanstalkd"
run_type :immediate
launch_only_once true
end

unit = f.service.to_systemd_unit
Expand All @@ -303,10 +307,10 @@
WantedBy=multi-user.target

[Service]
Type=simple
Type=oneshot
ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd
EOS
expect(unit).to eq(unit_expect)
expect(unit).to eq(unit_expect.strip)
end
end

Expand Down