Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
DigiGov-OSS
AuditRecordDB
Commits
aa496c85
Commit
aa496c85
authored
Jan 07, 2022
by
Panagiotis Skarvelis
Browse files
change to slow but stable sync
parent
3c169db2
Changes
4
Hide whitespace changes
Inline
Side-by-side
dist/interfaces/index.d.ts
View file @
aa496c85
...
...
@@ -19,3 +19,7 @@ export interface AuditEngine {
put
:
(
record
:
AuditRecord
)
=>
AuditRecord
|
null
;
get
:
(
auditTransactionId
:
string
)
=>
AuditRecord
|
null
;
}
export
declare
type
FS_ERROR
=
{
code
:
string
;
message
:
string
;
};
dist/lib/sequence.js
View file @
aa496c85
import
fs
from
'
fs
'
;
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
)
=>
{
const
LOCK_FILE_PATH
=
process
.
env
.
LOCK_FILE_PATH
||
"
/tmp/audit/sequence
"
;
const
SEQUENCE_FILE
=
process
.
env
.
SEQUENCE_FILE
||
"
/tmp/audit/sequence
"
;
const
TIMEOUT_LOCK_FILE
=
10000
;
const
delay
=
(
ms
)
=>
new
Promise
(
resolve
=>
setTimeout
(
resolve
,
ms
));
const
lockFile
=
async
(
path
,
timeout
=
0
)
=>
{
//if the file exists for long time have to delete it
// because probably something is broken on some node..
// 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
"
);
});
console
.
log
(
timeout
,
"
times loop, try to unlock
"
);
await
delay
(
1000
);
unlockFile
(
path
);
}
;
const
lockPath
=
`
${
path
}
.lock`
;
return
fs
.
promises
.
open
(
lockPath
,
fs
.
constants
.
O_CREAT
|
fs
.
constants
.
O_EXCL
|
fs
.
constants
.
O_RDWR
).
catch
(()
=>
lockFile
(
path
,
timeout
));
try
{
return
fs
.
openSync
(
lockPath
,
fs
.
constants
.
O_CREAT
|
fs
.
constants
.
O_EXCL
|
fs
.
constants
.
O_RDWR
);
}
catch
(
error
)
{
if
(
error
.
code
!==
"
EEXIST
"
)
console
.
log
(
error
.
message
);
return
lockFile
(
path
,
timeout
);
}
};
const
unlockFile
=
(
path
)
=>
{
const
unlockFile
=
async
(
path
,
timeout
=
0
)
=>
{
const
lockPath
=
`
${
path
}
.lock`
;
return
fs
.
promises
.
unlink
(
lockPath
).
catch
(()
=>
unlockFile
(
path
));
if
(
timeout
++
>
TIMEOUT_LOCK_FILE
)
{
console
.
log
(
timeout
,
"
times loop, unlock exit
"
);
return
;
}
;
try
{
return
fs
.
unlinkSync
(
lockPath
);
}
catch
(
error
)
{
if
(
error
.
code
===
"
ENOENT
"
)
{
return
;
}
else
{
console
.
log
(
error
.
message
);
await
delay
(
500
);
return
unlockFile
(
path
,
timeout
++
);
}
}
};
/**
*
...
...
@@ -33,10 +59,10 @@ const sequence = async (seqfile = "", lockfile = "") => {
if
(
!
fs
.
existsSync
(
SEQF
))
{
fs
.
writeFileSync
(
SEQF
,
"
0
"
);
}
const
abla
=
await
lockFile
(
LFP
);
const
abla
=
lockFile
(
LFP
);
let
seq
=
~~
fs
.
readFileSync
(
SEQF
);
//Read as integer ~~ is synonimus for parseInt
fs
.
writeFileSync
(
SEQF
,
""
+
++
seq
);
const
oubla
=
await
unlockFile
(
LFP
);
const
oubla
=
unlockFile
(
LFP
);
return
seq
;
};
export
default
sequence
;
src/interfaces/index.ts
View file @
aa496c85
...
...
@@ -19,4 +19,9 @@ export type AuditRecord = {
export
interface
AuditEngine
{
put
:
(
record
:
AuditRecord
)
=>
AuditRecord
|
null
;
get
:
(
auditTransactionId
:
string
)
=>
AuditRecord
|
null
;
}
export
type
FS_ERROR
=
{
code
:
string
,
message
:
string
}
\ No newline at end of file
src/lib/sequence.ts
View file @
aa496c85
import
fs
from
'
fs
'
;
import
{
FS_ERROR
}
from
'
../interfaces/index
'
;
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
=>
{
const
LOCK_FILE_PATH
=
process
.
env
.
LOCK_FILE_PATH
||
"
/tmp/audit/sequence
"
const
SEQUENCE_FILE
=
process
.
env
.
SEQUENCE_FILE
||
"
/tmp/audit/sequence
"
const
TIMEOUT_LOCK_FILE
=
10000
;
const
delay
=
(
ms
:
number
)
=>
new
Promise
(
resolve
=>
setTimeout
(
resolve
,
ms
))
const
lockFile
=
async
(
path
:
string
,
timeout
:
number
=
0
):
Promise
<
number
>
=>
{
//if the file exists for long time have to delete it
// because probably something is broken on some node..
// 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
"
)}
)};
if
(
timeout
++>
TIMEOUT_LOCK_FILE
)
{
console
.
log
(
timeout
,
"
times loop, try to unlock
"
);
await
delay
(
1000
);
unlockFile
(
path
)};
const
lockPath
=
`
${
path
}
.lock`
return
fs
.
promises
.
open
(
lockPath
,
fs
.
constants
.
O_CREAT
|
fs
.
constants
.
O_EXCL
|
fs
.
constants
.
O_RDWR
).
catch
(()
=>
lockFile
(
path
,
timeout
))
try
{
return
fs
.
openSync
(
lockPath
,
fs
.
constants
.
O_CREAT
|
fs
.
constants
.
O_EXCL
|
fs
.
constants
.
O_RDWR
)
}
catch
(
error
)
{
if
((
error
as
FS_ERROR
).
code
!==
"
EEXIST
"
)
console
.
log
((
error
as
FS_ERROR
).
message
);
return
lockFile
(
path
,
timeout
)
}
}
const
unlockFile
=
(
path
:
string
):
any
=>
{
const
unlockFile
=
async
(
path
:
string
,
timeout
:
number
=
0
):
Promise
<
void
>
=>
{
const
lockPath
=
`
${
path
}
.lock`
return
fs
.
promises
.
unlink
(
lockPath
).
catch
(()
=>
unlockFile
(
path
))
if
(
timeout
++>
TIMEOUT_LOCK_FILE
)
{
console
.
log
(
timeout
,
"
times loop, unlock exit
"
);
return
};
try
{
return
fs
.
unlinkSync
(
lockPath
)
}
catch
(
error
)
{
if
((
error
as
FS_ERROR
).
code
===
"
ENOENT
"
)
{
return
;
}
else
{
console
.
log
((
error
as
FS_ERROR
).
message
);
await
delay
(
500
);
return
unlockFile
(
path
,
timeout
++
);
}
}
}
/**
...
...
@@ -34,10 +56,10 @@ if (!fs.existsSync(SEQF)) {
fs
.
writeFileSync
(
SEQF
,
"
0
"
)
}
const
abla
=
await
lockFile
(
LFP
);
const
abla
=
lockFile
(
LFP
);
let
seq
:
number
=
~~
fs
.
readFileSync
(
SEQF
);
//Read as integer ~~ is synonimus for parseInt
fs
.
writeFileSync
(
SEQF
,
""
+
++
seq
);
const
oubla
=
await
unlockFile
(
LFP
);
const
oubla
=
unlockFile
(
LFP
);
return
seq
;
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment