Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Framework: allow the execution of code only once

See original GitHub issue

Feature 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:

  1. 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)
  1. 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:closed
  • Created 3 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

scottinetcommented, Apr 1, 2021

After discussing this issue with Adrien, we settled for the solution detailed here:

scottinetcommented, Mar 4, 2021

well, then it’s the kuzzle.doOnce solution, only renamed kuzzle.deploy (which sounds better indeed) 😁

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I ensure a piece of code runs only once?
In a Von Neumann Architecture computer, memory is where your program's instructions and data are stored. If you want to make code only...
Read more >
java - Is is possible to make a method execute only once?
I worked on a code base in C that did something similar by shifting a pointer to a global void nil() method. Seems...
Read more >
3 Ways to Run Code Once at Application Startup in ASP.NET ...
Run the Code Directly from Program or Startup Classes. Probably the most obvious and straightforward way to execute a piece of code only...
Read more >
Function that can be called only once in JavaScript
The user will be able to perform the operation of the function only once. We are going to do that with the help...
Read more >
AWS | SWF | FAQs -
The AWS SDK for Java includes the AWS Flow Framework. Q: I want to ensure that there is only one execution for each...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found