Starting/stopping loaded applications

I am creating a cluster with Erlang that has a leader/follower setup. I have 3 apps that I load in my rebar.config but I don’t start them if they are a follower. If a node starts up (or becomes the leader, it starts these loaded programs. If a node starts as a follower, the loaded apps are not started. If a node goes from a leader to a follower, it stops the loaded applications that are leader-only.

I noticed loaded applications do not appear with application:which_applications/0. They do appear with application:loaded_applications/0. If I the application is running and I run application:stop(app_name). it returns:

{error,{not_started,app_name}}

If I do application:stop or app_name_app:start(ok,ok) for an app that starts when I start my Erlang program, the application runs as expected.

So, starting/stopping the application via app_name_app:stop(ok). is useless then, and likewise I get the not_started when using the application module. So what then is the best way to start/stop unloaded apps? This is leaving processes continuing to run when I intend to stop them. Should I change my loaded apps to be more aware of their state? Or is there a better solution for stopping my apps when a node moves from a leader to a follower?

1 Like

My solution was to get my programs to sync up with the leader/follower state. I have a control state machine in the middle that upon entering the new state, sends a signal to the processes I want it to. I was able to make some workarounds and use some of my existing calls n such to make this easy and seamless.

1 Like