title: NodeJS Semaphore module: how does it work?
body: <p>So here I am with my CRON call to a nodeJS application. Currently there is no guarantee that my script will not be runned several times at a given moment ; I want the script to end whenever it is already running.</p>
  
  <p><a href="https://en.wikipedia.org/wiki/Semaphore_%28programming%29" rel="nofollow">Semaphores</a> are the usual way to do it. As I'm fairly new to <a href="https://en.wikipedia.org/wiki/Node.js" rel="nofollow">NodeJS</a>, I wanted to study and implement an existing module for that, if possible.</p>
  
  <p>So I found <a href="https://www.npmjs.com/package/semaphore" rel="nofollow">semaphore for node</a>, seemingly the most popular pre-made solution for node. However the documentation is extremely sparse, and studying the code gives me a headache:</p>
  
  <p><a href="https://github.com/abrkn/semaphore.js/blob/master/lib/semaphore.js" rel="nofollow">https://github.com/abrkn/semaphore.js/blob/master/lib/semaphore.js</a></p>
  
  <p>I thought the module would use some kind of file to know if the process is already running, however it uses <code>global</code> to store that information. How is that? That would certainly prevent two sub-scripts to run at the same time in a single CRON call, but that wouldn't prevent two different calls to CRON to execute, would it?</p>
  
  <p>Or is it possible that <code>global</code> is shared between all running NodeJS tasks?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I have to precise that this app is not an HTTP application, it is just a collection of CRONed scripts. So maybe that semaphore module isn't even the right direction to take, since it should apparently be used with <code>createServer()</code>.</p>