Framework: allow the execution of code only once
See original GitHub issueFeature Description
When an application starts for the first time, it’s commonplace for it to have some init tasks to perform, such as setting up the storage mappings, perhaps a few structures to build, some data to fetch from distant APIs, and so on.
There is no easy way at the moment to ask Kuzzle to execute something “only once”, meaning once per environment, whatever the number of Kuzzle nodes started on a cluster, and never executed again as long as the environment isn’t entirely reinstalled.
Example Use Case
Loading mappings into Elasticsearch to set up a storage environment.
Possible Solutions
The technical details themselves are pretty standard: use mutexes to prevent concurrent executions if multiple nodes are executed at the same time, and store a key into Redis once the task has been successfully executed to prevent further attempts at it.
Now, we have 2 possible ways of proposing that feature to Kuzzle users:
- Add a new
app.init
method: this is an optional method. If set, its content will be executed only once, during the entire lifetime of an environment
- Pros: easier to use and to document
- Cons: means that new init tasks cannot be added on an existing environment without executing all other tasks (and will need some kind of “start the init phase again” mechanism)
- Add a new method (proposal:
kuzzle.doOnce('id', <handler function>)
), that will execute the handler attached to the provided identifier only once per environment, during its lifetime
- Pros: tasks can be added and dropped on the fly on an existing environnement, during its lifetime (more flexible)
- Cons: less intuitive than a
app.init
method
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (11 by maintainers)
Top GitHub Comments
After discussing this issue with Adrien, we settled for the solution detailed here: https://github.com/kuzzleio/kuzzle/issues/1999#issuecomment-792600212
well, then it’s the
kuzzle.doOnce
solution, only renamedkuzzle.deploy
(which sounds better indeed) 😁