Commit af5d05ed authored by Panagiotis Skarvelis's avatar Panagiotis Skarvelis
Browse files

allow custom lockfile

parent 6b649575
......@@ -3,5 +3,5 @@
* @param seqfile
* @returns number
*/
declare const sequence: (seqfile?: string) => Promise<number>;
declare const sequence: (seqfile?: string, lockfile?: string) => Promise<number>;
export default sequence;
import fs from 'fs';
const LOCK_FILE_PATH = "/tmp/sequence";
const LOCK_FILE_PATH = process.env.LOCK_FILE_PATH || "/tmp/sequence";
const SEQUENCE_FILE = process.env.SEQUENCE_FILE || "/tmp/sequence";
const TIMEOUT_LOCK_FILE = 1000;
const lockFile = (path, timeout = 0) => {
//if the file exists for long time have to delete it
// because probably something is broken on some node..
if (timeout++ > TIMEOUT_LOCK_FILE)
// this works but may be not the best solution
if (timeout++ > TIMEOUT_LOCK_FILE) {
"1000 times loop, try to unlock";
unlockFile(path).catch(() => { console.log("unlockFile error"); });
}
;
const lockPath = `${path}.lock`;
return fs.promises.open(lockPath, fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_RDWR).catch(() => lockFile(path));
return fs.promises.open(lockPath, fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_RDWR).catch(() => lockFile(path, timeout));
};
const unlockFile = (path) => {
const lockPath = `${path}.lock`;
......@@ -19,17 +23,20 @@ const unlockFile = (path) => {
* @param seqfile
* @returns number
*/
const sequence = async (seqfile = "") => {
const sequence = async (seqfile = "", lockfile = "") => {
let SEQF = SEQUENCE_FILE;
let LFP = LOCK_FILE_PATH;
if (seqfile !== "")
SEQF = seqfile;
if (lockfile !== "")
LFP = lockfile;
if (!fs.existsSync(SEQF)) {
fs.writeFileSync(SEQF, "0");
}
const abla = await lockFile(LOCK_FILE_PATH);
const abla = await lockFile(LFP);
let seq = ~~fs.readFileSync(SEQF); //Read as integer ~~ is synonimus for parseInt
fs.writeFileSync(SEQF, "" + ++seq);
const oubla = await unlockFile(LOCK_FILE_PATH);
const oubla = await unlockFile(LFP);
return seq;
};
export default sequence;
import fs from 'fs';
const LOCK_FILE_PATH="/tmp/sequence"
const LOCK_FILE_PATH=process.env.LOCK_FILE_PATH || "/tmp/sequence"
const SEQUENCE_FILE = process.env.SEQUENCE_FILE || "/tmp/sequence"
const TIMEOUT_LOCK_FILE = 1000;
const lockFile = (path:string,timeout:number=0):any => {
//if the file exists for long time have to delete it
// because probably something is broken on some node..
if (timeout++>TIMEOUT_LOCK_FILE) unlockFile(path).catch(()=>{console.log("unlockFile error")});
// this works but may be not the best solution
if (timeout++>TIMEOUT_LOCK_FILE) { "1000 times loop, try to unlock"; unlockFile(path).catch(()=>{console.log("unlockFile error")})};
const lockPath = `${path}.lock`
return fs.promises.open(lockPath, fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_RDWR).catch(() => lockFile(path))
return fs.promises.open(lockPath, fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_RDWR).catch(() => lockFile(path,timeout))
}
const unlockFile = (path:string):any => {
const lockPath = `${path}.lock`
......@@ -21,19 +22,22 @@ const unlockFile = (path:string):any => {
* @param seqfile
* @returns number
*/
const sequence = async (seqfile:string="") =>{
const sequence = async (seqfile:string="",lockfile:string="") =>{
let SEQF = SEQUENCE_FILE;
let LFP = LOCK_FILE_PATH;
if (seqfile!=="") SEQF = seqfile;
if (lockfile!=="") LFP = lockfile;
if (!fs.existsSync(SEQF)) {
fs.writeFileSync(SEQF, "0")
}
const abla = await lockFile(LOCK_FILE_PATH);
const abla = await lockFile(LFP);
let seq:number = ~~fs.readFileSync(SEQF); //Read as integer ~~ is synonimus for parseInt
fs.writeFileSync(SEQF, "" + ++seq);
const oubla = await unlockFile(LOCK_FILE_PATH);
const oubla = await unlockFile(LFP);
return seq;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment