When to use —sup for an application

I‘m trying to understand if I need a supervision tree for a library I‘m developing.

I am writing a wrapper for a JSON API. It is some basic abstraction for what this service offers and used Mojito internally to fetch results and parse them to a struct.

This library will be used un a phoenix application.

Do I need to create a mix project with —sup or not? What should be my thought process when I‘m writing a library?

Do you spawn processes (GenServer, Task, raw, etc) or are you just providing functions that needs to be called?

If you spawn you should supervise unless you ask your user to get them into their supervision tree (as ecto does).

Otherwise you don’t need a supervision tree, as you do not have anything to supervise.

2 Likes

Mojito, which I am using - spawns. Does that count?

You need a supervision tree, if the application itself spawns processes, where it’s responsible for their lifecycles. If on the other hand your application only defines processes to be started/managed by the user then it doesn’t need a supervision tree on its own. The latter is often needed if processes need runtime user input to be started, but should still be statically defined and not run as children of a dynamic supervisor.

If you take ecto as an example:

The repo is always a userland process, not started/manged by ecto (the application). If that would be the only thing ecto needs, its application wouldn’t need a supervision tree.

But there is Ecto.Application and it starts a supervision tree, because ecto uses a registry where repos are registered in. This one is not needed or wanted to be managed by users of ecto, so the ecto application starts/manages the registry. It also can be started without needing user input.

3 Likes

Thanks guys!

I could be wrong but I think mojito will bring in its own supervision tree.

You are correct. I was not sure if that meant that should also create a supervision tree. Now I know - no, only if my own library spawns processes and not some other libraries that I am using.