From 7d19ef7070d4b16330f6c0940714c518bdb0e5ac Mon Sep 17 00:00:00 2001 From: DenizUgur Date: Mon, 13 Feb 2023 23:19:34 -0800 Subject: [PATCH 1/9] dedicated 'RunAtLoad' property --- Library/Homebrew/service.rb | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/service.rb b/Library/Homebrew/service.rb index 77d3b47862..49fb1076fa 100644 --- a/Library/Homebrew/service.rb +++ b/Library/Homebrew/service.rb @@ -159,6 +159,26 @@ module Homebrew @require_root.present? && @require_root == true end + sig { params(value: T.nilable(T::Boolean)).returns(T.nilable(T::Boolean)) } + def run_at_load(value = nil) + case T.unsafe(value) + when nil + @run_at_load + when true, false + @run_at_load = value + else + raise TypeError, "Service#run_at_load expects a Boolean" + end + end + + # Returns a `Boolean` describing if a service requires root access. + # @return [Boolean] + sig { returns(T::Boolean) } + def run_at_load? + instance_eval(&@service_block) + @run_at_load.present? && @run_at_load == true + end + sig { params(value: T.nilable(String)).returns(T.nilable(T::Hash[Symbol, String])) } def sockets(value = nil) case T.unsafe(value) @@ -371,7 +391,7 @@ module Homebrew base = { Label: @formula.plist_name, ProgramArguments: command, - RunAtLoad: @run_type == RUN_TYPE_IMMEDIATE, + RunAtLoad: @run_type == RUN_TYPE_IMMEDIATE || @run_at_load == true, } base[:LaunchOnlyOnce] = @launch_only_once if @launch_only_once == true From ff748ea030855c850b70d58bcfb5def7e4ac8db2 Mon Sep 17 00:00:00 2001 From: DenizUgur Date: Tue, 14 Feb 2023 01:04:34 -0800 Subject: [PATCH 2/9] change the default run_at_load value --- Library/Homebrew/service.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/service.rb b/Library/Homebrew/service.rb index 49fb1076fa..643c69e0b5 100644 --- a/Library/Homebrew/service.rb +++ b/Library/Homebrew/service.rb @@ -171,12 +171,12 @@ module Homebrew end end - # Returns a `Boolean` describing if a service requires root access. + # Returns a `Boolean` describing if a service requires the command to execute at load. # @return [Boolean] sig { returns(T::Boolean) } def run_at_load? instance_eval(&@service_block) - @run_at_load.present? && @run_at_load == true + @run_at_load.present? && @run_at_load == false end sig { params(value: T.nilable(String)).returns(T.nilable(T::Hash[Symbol, String])) } From a9d1ee44c8c5c0be27bffa8bba4eec330e866189 Mon Sep 17 00:00:00 2001 From: DenizUgur Date: Tue, 14 Feb 2023 02:24:20 -0800 Subject: [PATCH 3/9] suggestion for linux compatibility --- Library/Homebrew/service.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/service.rb b/Library/Homebrew/service.rb index 643c69e0b5..46cbf5f0bb 100644 --- a/Library/Homebrew/service.rb +++ b/Library/Homebrew/service.rb @@ -391,7 +391,7 @@ module Homebrew base = { Label: @formula.plist_name, ProgramArguments: command, - RunAtLoad: @run_type == RUN_TYPE_IMMEDIATE || @run_at_load == true, + RunAtLoad: @run_type == RUN_TYPE_IMMEDIATE || (@run_at_load == true && @run_type == RUN_TYPE_INTERVAL), } base[:LaunchOnlyOnce] = @launch_only_once if @launch_only_once == true @@ -494,6 +494,7 @@ module Homebrew instance_eval(&@service_block) options = [] options << "Persistent=true" if @run_type == RUN_TYPE_CRON + options << "OnActiveSec=0s" if @run_at_load == true && @run_type == RUN_TYPE_INTERVAL options << "OnUnitActiveSec=#{@interval}" if @run_type == RUN_TYPE_INTERVAL if @run_type == RUN_TYPE_CRON From d80d94ab2828bcfb63bfec7c14865abab59080c5 Mon Sep 17 00:00:00 2001 From: DenizUgur Date: Tue, 14 Feb 2023 03:55:45 -0800 Subject: [PATCH 4/9] comments --- Library/Homebrew/service.rb | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/Library/Homebrew/service.rb b/Library/Homebrew/service.rb index 46cbf5f0bb..b83503c76a 100644 --- a/Library/Homebrew/service.rb +++ b/Library/Homebrew/service.rb @@ -162,23 +162,15 @@ module Homebrew sig { params(value: T.nilable(T::Boolean)).returns(T.nilable(T::Boolean)) } def run_at_load(value = nil) case T.unsafe(value) - when nil + when nil, false @run_at_load - when true, false - @run_at_load = value + when true + @run_at_load = @run_type == RUN_TYPE_INTERVAL else raise TypeError, "Service#run_at_load expects a Boolean" end end - # Returns a `Boolean` describing if a service requires the command to execute at load. - # @return [Boolean] - sig { returns(T::Boolean) } - def run_at_load? - instance_eval(&@service_block) - @run_at_load.present? && @run_at_load == false - end - sig { params(value: T.nilable(String)).returns(T.nilable(T::Hash[Symbol, String])) } def sockets(value = nil) case T.unsafe(value) @@ -391,7 +383,7 @@ module Homebrew base = { Label: @formula.plist_name, ProgramArguments: command, - RunAtLoad: @run_type == RUN_TYPE_IMMEDIATE || (@run_at_load == true && @run_type == RUN_TYPE_INTERVAL), + RunAtLoad: @run_type == RUN_TYPE_IMMEDIATE || @run_at_load == true, } base[:LaunchOnlyOnce] = @launch_only_once if @launch_only_once == true @@ -494,7 +486,7 @@ module Homebrew instance_eval(&@service_block) options = [] options << "Persistent=true" if @run_type == RUN_TYPE_CRON - options << "OnActiveSec=0s" if @run_at_load == true && @run_type == RUN_TYPE_INTERVAL + options << "OnActiveSec=0s" if @run_at_load == true options << "OnUnitActiveSec=#{@interval}" if @run_type == RUN_TYPE_INTERVAL if @run_type == RUN_TYPE_CRON From 16b51d2a431cfbe51ec63f6de979e087117ec975 Mon Sep 17 00:00:00 2001 From: DenizUgur Date: Tue, 14 Feb 2023 04:24:26 -0800 Subject: [PATCH 5/9] revert type check --- Library/Homebrew/service.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/service.rb b/Library/Homebrew/service.rb index b83503c76a..e4211ed3f7 100644 --- a/Library/Homebrew/service.rb +++ b/Library/Homebrew/service.rb @@ -162,10 +162,10 @@ module Homebrew sig { params(value: T.nilable(T::Boolean)).returns(T.nilable(T::Boolean)) } def run_at_load(value = nil) case T.unsafe(value) - when nil, false + when nil @run_at_load - when true - @run_at_load = @run_type == RUN_TYPE_INTERVAL + when true, false + @run_at_load = value else raise TypeError, "Service#run_at_load expects a Boolean" end From 66fe6d4169f63ae0d162de251d1c74a08139fa9a Mon Sep 17 00:00:00 2001 From: DenizUgur Date: Wed, 15 Feb 2023 09:49:08 -0800 Subject: [PATCH 6/9] remove onActiveSec --- Library/Homebrew/service.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/Library/Homebrew/service.rb b/Library/Homebrew/service.rb index e4211ed3f7..bb42b51bec 100644 --- a/Library/Homebrew/service.rb +++ b/Library/Homebrew/service.rb @@ -486,7 +486,6 @@ module Homebrew instance_eval(&@service_block) options = [] options << "Persistent=true" if @run_type == RUN_TYPE_CRON - options << "OnActiveSec=0s" if @run_at_load == true options << "OnUnitActiveSec=#{@interval}" if @run_type == RUN_TYPE_INTERVAL if @run_type == RUN_TYPE_CRON From f4ed50235052cead0ef884a49aa2dd2ea5f1548b Mon Sep 17 00:00:00 2001 From: DenizUgur Date: Thu, 23 Feb 2023 10:15:06 -0800 Subject: [PATCH 7/9] run_at_load set to true by default --- Library/Homebrew/service.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/service.rb b/Library/Homebrew/service.rb index bb42b51bec..4725a8acb0 100644 --- a/Library/Homebrew/service.rb +++ b/Library/Homebrew/service.rb @@ -27,6 +27,7 @@ module Homebrew def initialize(formula, &block) @formula = formula @run_type = RUN_TYPE_IMMEDIATE + @run_at_load = true @environment_variables = {} @service_block = block end @@ -383,7 +384,7 @@ module Homebrew base = { Label: @formula.plist_name, ProgramArguments: command, - RunAtLoad: @run_type == RUN_TYPE_IMMEDIATE || @run_at_load == true, + RunAtLoad: @run_at_load == true, } base[:LaunchOnlyOnce] = @launch_only_once if @launch_only_once == true From 026709962d488ca34becb635cc0eb5e6316a0707 Mon Sep 17 00:00:00 2001 From: DenizUgur Date: Thu, 23 Feb 2023 11:02:20 -0800 Subject: [PATCH 8/9] update tests --- Library/Homebrew/test/service_spec.rb | 42 +++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/test/service_spec.rb b/Library/Homebrew/test/service_spec.rb index 6b08b4f03b..21546e7b04 100644 --- a/Library/Homebrew/test/service_spec.rb +++ b/Library/Homebrew/test/service_spec.rb @@ -185,6 +185,7 @@ describe Homebrew::Service do service do run [opt_bin/"beanstalkd", "test"] run_type :immediate + run_at_load true environment_variables PATH: std_service_path_env, FOO: "BAR", ETC_DIR: etc/"beanstalkd" error_log_path var/"log/beanstalkd.error.log" log_path var/"log/beanstalkd.log" @@ -346,6 +347,43 @@ describe Homebrew::Service do expect(plist).to eq(plist_expect) end + it "returns valid partial plist" do + f = stub_formula do + service do + run opt_bin/"beanstalkd" + run_type :immediate + run_at_load false + end + end + + plist = f.service.to_plist + plist_expect = <<~EOS + + + + + \tLabel + \thomebrew.mxcl.formula_name + \tLimitLoadToSessionType + \t + \t\tAqua + \t\tBackground + \t\tLoginWindow + \t\tStandardIO + \t\tSystem + \t + \tProgramArguments + \t + \t\t#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd + \t + \tRunAtLoad + \t + + + EOS + expect(plist).to eq(plist_expect) + end + it "returns valid interval plist" do f = stub_formula do service do @@ -376,7 +414,7 @@ describe Homebrew::Service do \t\t#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd \t \tRunAtLoad - \t + \t \tStartInterval \t5 @@ -415,7 +453,7 @@ describe Homebrew::Service do \t\t#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd \t \tRunAtLoad - \t + \t \tStartCalendarInterval \t \t\tHour From 46aefe776fb9c347f11b12a7ad70973c97c96a7e Mon Sep 17 00:00:00 2001 From: DenizUgur Date: Thu, 23 Feb 2023 11:08:39 -0800 Subject: [PATCH 9/9] fix tests --- Library/Homebrew/test/service_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Library/Homebrew/test/service_spec.rb b/Library/Homebrew/test/service_spec.rb index 21546e7b04..5554aa2860 100644 --- a/Library/Homebrew/test/service_spec.rb +++ b/Library/Homebrew/test/service_spec.rb @@ -185,7 +185,6 @@ describe Homebrew::Service do service do run [opt_bin/"beanstalkd", "test"] run_type :immediate - run_at_load true environment_variables PATH: std_service_path_env, FOO: "BAR", ETC_DIR: etc/"beanstalkd" error_log_path var/"log/beanstalkd.error.log" log_path var/"log/beanstalkd.log" @@ -347,7 +346,7 @@ describe Homebrew::Service do expect(plist).to eq(plist_expect) end - it "returns valid partial plist" do + it "returns valid partial plist with run_at_load being false" do f = stub_formula do service do run opt_bin/"beanstalkd"