ConsumerSupervisor and managing state for the workers

Currently I have a GenServer that is responsible for accessing an external api and this access requires an auth token to be sent along. This is no problem, I obtain the token in the init callback and save it in the state that gets passed around. The first time a request happens where the token has expired, the GenServer stops and then a supervisor restarts it and everything is happy again.

Now I am moving this over to use GenStage as this better fits my needs and I am using a ConsumerSupervisor to dynamically bring up workers as and when I need them. However I have hit one small road block which is how to manage the state passed into the workers in a similar way. To obtain the token initially looks straight forward as I could just do this in the supervisor start_link and delay starting the supervision. However when one of my workers crash with an expired token I am unsure how to recover from this and effectivily reset the state used for new workers that are started in the future. So far the best potential option I see is using ets to store the state and each worker can access the token from there instead of having it passed in automatically.

2 Likes