In this case, I am writing a simple application that monitors a job that it has finished. To converse API, I do not want to use polling, but streams API. How should I use it?
The stream api Events - HTTP API | Nomad | HashiCorp Developer tells me that:
If the requested index is no longer in the buffer the stream will start at the next available index.
That basically means it is useless as it doesn’t repeat the existing current state to the application. Which means, that basically for every application I write using stream API I have to basically create three threads of execution:
- listener thread
- that receives events from other threads and actually does the work
- stream thread
- receives events from the Nomad stream API and forwards to listeners thread
- replay thread
- downloads current state in Nomad
- forwards to listeners thread
The “replay thread” has to be started after stream thread, so that “listener thread” receives all the possible data - not only changes but also current state. Additionally, “stream thread” has to buffer all data until “replay thread” finishes - so that “changes” to Nomad state are transferred after the state is known.
Is there a “stream API” endpoint that not only lists new events that come to Nomad, but also replays the current state inside Nomad? I would imagine, If I request stream API from a namespace, first all jobs from this namespace will be sent, then if a job changes the updates will be sent. Is there a simple solution to this problem?
Currently, written apps work much better by periodically polling Nomad database, by downloading all the allocations, evaluations and jobs inside a database, doing joins and working on it. It would be much easier if the “stream” API could replay the current state.