Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
augustctl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open-source
augustctl
Commits
9d433092
Verified
Commit
9d433092
authored
8 years ago
by
Erick Hitter
Browse files
Options
Downloads
Patches
Plain Diff
Cache clearing and different durations for different endpoints
parent
61e0cfa7
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
server.js
+85
-58
85 additions, 58 deletions
server.js
with
85 additions
and
58 deletions
server.js
+
85
−
58
View file @
9d433092
...
@@ -5,7 +5,7 @@ var express = require( 'express' );
...
@@ -5,7 +5,7 @@ var express = require( 'express' );
var
morgan
=
require
(
'
morgan
'
);
var
morgan
=
require
(
'
morgan
'
);
var
await
=
require
(
'
asyncawait/await
'
);
var
await
=
require
(
'
asyncawait/await
'
);
var
async
=
require
(
'
asyncawait/async
'
);
var
async
=
require
(
'
asyncawait/async
'
);
var
apicache
=
require
(
'
apicache
'
).
options
(
{
defaultDuration
:
30
000
}
);
var
apicache
=
require
(
'
apicache
'
).
options
(
{
defaultDuration
:
15
000
}
);
var
cache
=
apicache
.
middleware
;
var
cache
=
apicache
.
middleware
;
var
config
=
require
(
process
.
env
.
AUGUSTCTL_CONFIG
||
'
./config.json
'
);
var
config
=
require
(
process
.
env
.
AUGUSTCTL_CONFIG
||
'
./config.json
'
);
...
@@ -25,86 +25,113 @@ var ret = {
...
@@ -25,86 +25,113 @@ var ret = {
'
msg
'
:
''
'
msg
'
:
''
};
};
// Endpoint to perform all lock actions
// Endpoint to check lock status
app
.
get
(
'
/api/:lock_action/:lock_name
'
,
cache
(
'
5 seconds
'
),
function
(
req
,
res
)
{
app
.
get
(
'
/api/status/:lock_name
'
,
cache
(
'
15 seconds
'
),
function
(
req
,
res
)
{
// Parse allowed request arguments
var
lock
=
app
.
get
(
'
lock
'
+
req
.
params
.
lock_name
);
if
(
!
lock
)
{
clear_caches
(
req
.
params
.
lock_name
);
res
.
sendStatus
(
400
);
return
;
}
// Suspendable functions to check lock's status
var
actionFunction
=
async
(
function
()
{
var
status
=
await
(
lock
.
status
()
);
ret
.
ret
=
status
;
ret
.
status
=
statusStringtoInt
(
status
);
ret
.
msg
=
"
Status checked successfully.
"
;
lock
.
disconnect
();
res
.
json
(
ret
);
}
);
// Perform requested action
lock
.
connect
().
then
(
actionFunction
).
catch
(
function
(
err
)
{
console
.
error
(
err
);
lock
.
disconnect
();
clear_caches
(
req
.
params
.
lock_name
);
res
.
sendStatus
(
500
);
}
);
}
);
// Endpoint to change lock state
app
.
get
(
'
/api/:lock_action(lock|unlock)/:lock_name
'
,
cache
(
'
5 seconds
'
),
function
(
req
,
res
)
{
// Parse allowed request arguments
// Parse allowed request arguments
var
action
=
req
.
params
.
lock_action
,
var
action
=
req
.
params
.
lock_action
,
allowedActions
=
[
'
unlock
'
,
'
lock
'
,
'
status
'
];
allowedActions
=
[
'
unlock
'
,
'
lock
'
];
if
(
-
1
===
allowedActions
.
indexOf
(
action
)
)
{
if
(
-
1
===
allowedActions
.
indexOf
(
action
)
)
{
res
.
sendStatus
(
400
);
res
.
sendStatus
(
400
);
return
;
return
;
}
}
var
lock
=
app
.
get
(
'
lock
'
+
req
.
params
.
lock_name
);
var
lockName
=
req
.
params
.
lock_name
,
lock
=
app
.
get
(
'
lock
'
+
lockName
);
if
(
!
lock
)
{
if
(
!
lock
)
{
clear_caches
(
lockName
);
res
.
sendStatus
(
400
);
res
.
sendStatus
(
400
);
return
;
return
;
}
}
// Suspendable functions to interact with lock based on requested action
// Suspendable functions to interact with lock based on requested action
if
(
'
status
'
===
action
)
{
var
actionFunction
=
async
(
function
()
{
// Checks lock's state and returns it
var
status
=
await
(
lock
.
status
()
);
var
actionFunction
=
async
(
function
()
{
var
status
=
await
(
lock
.
status
()
),
if
(
'
lock
'
===
action
&&
'
unlocked
'
===
status
)
{
statusInt
=
-
1
;
var
cmd
=
await
(
lock
.
forceLock
()
);
if
(
'
locked
'
===
status
)
{
ret
.
ret
=
'
locked
'
;
statusInt
=
0
;
ret
.
status
=
0
;
}
else
if
(
'
unlocked
'
===
status
)
{
ret
.
msg
=
'
Locked as requested.
'
;
statusInt
=
1
;
}
else
if
(
'
unlock
'
===
action
&&
'
locked
'
===
status
)
{
}
var
cmd
=
await
(
lock
.
forceUnlock
()
);
ret
.
ret
=
'
unlocked
'
;
ret
.
status
=
1
;
ret
.
msg
=
'
Unlocked as requested.
'
;
}
else
{
ret
.
ret
=
status
;
ret
.
ret
=
status
;
ret
.
status
=
statusInt
;
ret
.
status
=
statusStringtoInt
(
status
);
ret
.
msg
=
"
Status checked successfully.
"
;
ret
.
msg
=
"
No change made. Lock was already '
"
+
status
+
"
'.
"
;
}
lock
.
disconnect
();
res
.
json
(
ret
);
lock
.
disconnect
();
}
);
clear_caches
(
lockName
);
}
else
{
res
.
json
(
ret
);
// Locks or unlocks a requested lock, if not already in that state
}
);
var
actionFunction
=
async
(
function
()
{
var
status
=
await
(
lock
.
status
()
);
if
(
'
lock
'
===
action
&&
'
unlocked
'
===
status
)
{
var
cmd
=
await
(
lock
.
forceLock
()
);
ret
.
ret
=
'
locked
'
;
ret
.
status
=
0
;
ret
.
msg
=
'
Locked as requested.
'
;
}
else
if
(
'
unlock
'
===
action
&&
'
locked
'
===
status
)
{
var
cmd
=
await
(
lock
.
forceUnlock
()
);
ret
.
ret
=
'
unlocked
'
;
ret
.
status
=
1
;
ret
.
msg
=
'
Unlocked as requested.
'
;
}
else
{
var
statusInt
=
-
1
;
if
(
'
locked
'
===
status
)
{
statusInt
=
0
;
}
else
if
(
'
unlocked
'
===
status
)
{
statusInt
=
1
;
}
ret
.
ret
=
status
;
ret
.
status
=
statusInt
;
ret
.
msg
=
"
No change made. Lock was already '
"
+
status
+
"
'.
"
;
}
lock
.
disconnect
();
res
.
json
(
ret
);
}
);
}
// Perform requested action
// Perform requested action
lock
.
connect
().
then
(
actionFunction
).
catch
(
function
(
err
)
{
lock
.
connect
().
then
(
actionFunction
).
catch
(
function
(
err
)
{
console
.
error
(
err
);
console
.
error
(
err
);
lock
.
disconnect
();
lock
.
disconnect
();
clear_caches
(
lockName
);
res
.
sendStatus
(
500
);
res
.
sendStatus
(
500
);
}
);
}
);
}
);
}
);
// Convert named status to integer representation
function
statusStringtoInt
(
status
)
{
var
statusInt
=
-
1
;
if
(
'
locked
'
===
status
)
{
statusInt
=
0
;
}
else
if
(
'
unlocked
'
===
status
)
{
statusInt
=
1
;
}
return
statusInt
;
}
// Clear cached routes
function
clear_caches
(
lockName
)
{
apicache
.
clear
(
'
/api/status/
'
+
lockName
);
apicache
.
clear
(
'
/api/lock/
'
+
lockName
);
apicache
.
clear
(
'
/api/unlock/
'
+
lockName
);
return
true
;
}
// Parse lock configurations
// Parse lock configurations
Object
.
keys
(
config
).
forEach
(
function
(
lockName
)
{
Object
.
keys
(
config
).
forEach
(
function
(
lockName
)
{
var
lockConfig
=
config
[
lockName
];
var
lockConfig
=
config
[
lockName
];
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment