@@ -174,58 +174,74 @@ function generate_launchctl_script(io::IO, brg::BuildkiteRunnerGroup;
174174 print (w_io, """
175175 #!/bin/bash
176176
177- # Cleanup host paths to protect against stale state leaking
177+ # macOS doesn't like services restarting all the time,
178+ # so keep launching agents until we're asked to quit.
179+ while true; do
180+
181+ # Cleanup host paths to protect against stale state leaking
178182 """ )
179183
180184 for path in host_paths_to_cleanup (temp_path, cache_path)
181- println (w_io, " chmod -R u+w $(path) " )
182- println (w_io, " rm -rf $(path) " )
185+ println (w_io, " chmod -R u+w $(path) " )
186+ println (w_io, " rm -rf $(path) " )
183187 end
184188
185189 print (w_io, """
186- # Create host paths that must exist
190+ # Create host paths that must exist
187191 """ )
188192 for path in host_paths_to_create (temp_path, cache_path)
189- println (w_io, " mkdir -p $(path) " )
193+ println (w_io, " mkdir -p $(path) " )
190194 end
191195
192196 println (w_io, """
193- # Copy secrets into cache directory, which will be deleted by agent environment hook
194- rm -rf $(secrets_dst_path)
195- cp -Ra $(secrets_src_path) $(secrets_dst_path)
196-
197- # Invoke agent inside of sandbox
198- sandbox-exec -f $(sb_path) $(agent_path) start \\
199- --disconnect-after-job \\
200- --sockets-path=$(temp_path) \\
201- --hooks-path=$(hooks_path) \\
202- --build-path=$(cache_path) /build \\
203- --experiment=resolve-commit-after-checkout \\
204- --git-mirrors-path=$(cache_path) /repos \\
205- --tags=$(join (tags_with_queues, " ," )) \\
206- --name=$(agent_name)
197+ # Copy secrets into cache directory, which will be deleted by agent environment hook
198+ rm -rf $(secrets_dst_path)
199+ cp -Ra $(secrets_src_path) $(secrets_dst_path)
200+
201+ # Invoke agent inside of sandbox
202+ sandbox-exec -f $(sb_path) $(agent_path) start \\
203+ --disconnect-after-job \\
204+ --sockets-path=$(temp_path) \\
205+ --hooks-path=$(hooks_path) \\
206+ --build-path=$(cache_path) /build \\
207+ --experiment=resolve-commit-after-checkout \\
208+ --git-mirrors-path=$(cache_path) /repos \\
209+ --tags=$(join (tags_with_queues, " ," )) \\
210+ --name=$(agent_name)
211+ ret=\$ ?
212+ echo "Agent exited with status \$ ret"
207213 """ )
208214
209215 print (w_io, """
210- # Cleanup host paths
216+ # Cleanup host paths
211217 """ )
212218 for path in host_paths_to_cleanup (temp_path, cache_path)
213- println (w_io, " chmod -R u+w $(path) " )
214- println (w_io, " rm -rf $(path) " )
219+ println (w_io, " chmod -R u+w $(path) " )
220+ println (w_io, " rm -rf $(path) " )
215221 end
216222
217223 print (w_io, """
218- # Reboot the machine if we've been running for more than 24 hours
219- ts_boot=\$ (sysctl -n kern.boottime | cut -d" " -f4 | cut -d"," -f1)
220- ts_now=\$ (date +%s)
221-
222- if ((ts_now - ts_boot > 24*60*60)); then
223- sudo -n /sbin/shutdown -r now
224+ # Reboot the machine if we've been running for more than 24 hours
225+ ts_boot=\$ (sysctl -n kern.boottime | cut -d" " -f4 | cut -d"," -f1)
226+ ts_now=\$ (date +%s)
227+ if ((ts_now - ts_boot > 24*60*60)); then
228+ echo "Rebooting machine after 24 hours of uptime"
229+ sudo -n /sbin/shutdown -r now
230+ break
231+ fi
232+ """ )
224233
225- # Give the system the time to shut down,
226- # preventing a new job from getting picked up
227- sleep 30
228- fi
234+ print (w_io, """
235+ # If the agent returned with code 255, that indicated a
236+ # graceful termination, so exit the loop
237+ if [ \$ ret -eq 255 ]; then
238+ echo "Stopping service after graceful termination"
239+ break
240+ fi
241+ done
242+
243+ # Return success, which will result in the service _not_ restarting
244+ exit 0
229245 """ )
230246 end
231247
@@ -236,7 +252,7 @@ function generate_launchctl_script(io::IO, brg::BuildkiteRunnerGroup;
236252 env = build_seatbelt_env (temp_path, cache_path),
237253 cwd = joinpath (cache_path, " build" ),
238254 logpath = joinpath (cache_path, " agent.log" ),
239- keepalive = true ,
255+ keepalive = (; SuccessfulExit = false ) ,
240256 )
241257 write (io, lctl_config)
242258end
0 commit comments