× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Shutdown a Pharo image using an OS signal

Sebastian Sastre
Feb 10, 2015
<p>Sometimes you need to control how a worker image starts and stops right from the operative system. You my do it from the terminal but the really important use for this comes with automation and orchestration.</p> <p>In airflowing, the Pharo worker images start and stop automatically and on command from the OS and in other to shutdown, the images use the technique I'm describing here.</p> <p>To do thins you need 3 things:</p> <ol> <li>A VM process listening to the TERM signal</li> <li>A helper object to hold that VM process</li> <li>A reaction to handle that signal</li> </ol> <p>The helper object usually instantiates on image start and holds a VM process running in the lowest priority. On start it will listen for the TERM signal:</p> <pre>Helper&gt;&gt;makeStopHook "Answers the process that hooks to the OS signal that makes this worker to shutdown when the VM process receives a TERM signal from the OS." ^ [|semaphore| semaphore := OSProcess accessor forwardSigTerm. semaphore wait. self onTerminationSignal] forkAt: Processor systemBackgroundPriority named: 'Image TERM'.</pre> <p> </p> <p>Then you need to implement the reaction to handle that OS signal, and since you probably want a clean shutdown <em>without</em> preserving state, you can use:</p> <pre>Helper&gt;&gt;onTerminationSignal "The process for the VM of this image has received a TERM signal from the OS. React accordingly" self log: 'That''s all folks. This worker is shutting down. Bye bye...' level:#messages. OSProcess accessor restoreSigTerm. SmalltalkImage current snapshot: false andQuit: true.</pre> <p> </p> <h3>Bonus</h3> <p>Here is what you do to stop the VM process on that image so you can do a clean exit on any services it might be providing:</p> <p> </p> <pre>Helper&gt;&gt;stopSignalProcesses self isOnUnixLikeOS ifFalse:[^nil]. self log: 'Stopping stop hook...' level:#messages. stopHook ifNotNil: [|value| value := stopHook. stopHook := nil. OSProcess accessor restoreSigTerm. value].</pre>
comments powered by Disqus