All files / urkel/lib/radix lockfile.js

67.21% Statements 41/61
37.5% Branches 6/16
64.28% Functions 9/14
68.96% Lines 40/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142                1x 1x               5x   5x 5x 5x 5x 5x 5x 5x   5x       6x 6x       12x 12x   12x                               12x 12x 12x             12x   12x             12x 12x       12x   12x   12x 12x       12x 12x 12x                       12x   12x 12x   12x       12x       12x 12x                       12x 12x                       1x  
/*!
 * lockfile.js - lock file
 * Copyright (c) 2018, Christopher Jeffrey (MIT License).
 * https://github.com/handshake-org/urkel
 */
 
'use strict';
 
const assert = require('bsert');
const Path = require('path');
 
/**
 * LockFile
 */
 
class LockFile {
  constructor(fs, prefix) {
    assert(fs && typeof fs.write === 'function');
 
    this.fs = fs;
    this.file = '';
    this.attempts = 4;
    this.interval = 1500;
    this.retry = 1000;
    this.stale = 3500;
    this.timer = null;
 
    this.rename(prefix);
  }
 
  rename(prefix) {
    assert(typeof prefix === 'string');
    this.file = Path.resolve(prefix, 'lock');
  }
 
  async open() {
    for (let i = 0; i < this.attempts; i++) {
      const stat = await this.stat();
 
      Iif (stat) {
        const now = Date.now();
        const mtime = Math.round(stat.mtimeMs);
 
        // Somebody changed their system clock.
        if (mtime >= now + 10 * 1000) {
          await this.unlink();
          continue;
        }
 
        if (now < mtime + this.stale) {
          await this.wait();
          continue;
        }
      }
 
      try {
        Eif (!stat)
          await this.touch();
        else
          await this.update();
      } catch (e) {
        continue;
      }
 
      this.start();
 
      return;
    }
 
    throw new Error(`Could not acquire lock for: ${this.file}.`);
  }
 
  async close() {
    this.stop();
    return this.unlink();
  }
 
  start() {
    assert(this.timer == null);
 
    this.timer = setInterval(() => this.iterate(), this.interval);
 
    Eif (this.timer.unref)
      this.timer.unref();
  }
 
  stop() {
    assert(this.timer != null);
    clearInterval(this.timer);
    this.timer = null;
  }
 
  async iterate() {
    try {
      await this.update();
    } catch (e) {
      ;
    }
  }
 
  async stat() {
    let stat = null;
 
    try {
      stat = await this.fs.stat(this.file);
    } catch (e) {
      Iif (e.code !== 'ENOENT')
        throw e;
    }
 
    return stat;
  }
 
  async unlink() {
    try {
      await this.fs.unlink(this.file);
    } catch (e) {
      if (e.code !== 'ENOENT')
        throw e;
    }
  }
 
  async wait() {
    return new Promise(r => setTimeout(r, this.retry));
  }
 
  async touch() {
    const fd = await this.fs.open(this.file, 'wx', 0o640);
    return this.fs.close(fd);
  }
 
  async update() {
    return this.fs.truncate(this.file, 0);
  }
}
 
/*
 * Expose
 */
 
module.exports = LockFile;