Reputation: 1437
I'm building a command line utility in Elixir and packaging it as an escript. I need to run some clean-up tasks when the executable exits.
My understanding of an escript is that it starts the Application each time it receives input from stdIn. Am I correct that the Application should exit once it has completed processing the input?
My app does link a supervisor that monitors a GenServer, but from what I can tell it restarts every time the app receives new input from stdIn.
According to the Application Module Docs you can implement the stop/1 callback in your Application module. I am doing this but the callback never fires.
How can I execute clean-up tasks in an escript app?
Upvotes: 1
Views: 308
Reputation: 6059
The OTP application is started when you start the escript, and is running for as long as the script (OS process) is running, and that is until the main function returns. Once the main function is done, the OS process simply stops.
To explicitly cleanup the application, you can call Application.stop/1
at the end of the main function. This call is synchronous and returns after the entire supervision tree is terminated. Keep in mind that the default shutdown strategy is to wait for max 5 seconds for a process to gracefully terminate, so if you expect a longer cleanup in some processes, you need to revisit child specs used in your supervisor(s).
Upvotes: 3