FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
code-server/src/node/heart.ts at asher/enable-webkit · coder/code-server · GitHub
coder
code-server
Repository navigation
Code
Issues
143
(143)
Pull requests
12
(12)
Discussions
Actions
Projects
Security and quality
1
(1)
Insights
Expand file tree
Breadcrumbs
code-server
/
src
/
node
/
heart.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
82 lines (73 loc) · 2.12 KB
Breadcrumbs
code-server
/
src
/
node
/
heart.ts
Copy path
File metadata and controls
82 lines (73 loc) · 2.12 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
{
logger
}
from
"@coder/logger"
import
{
promises
as
fs
}
from
"fs"
import
{
Emitter
}
from
"../common/emitter"
/**
* Provides a heartbeat using a local file to indicate activity.
*/
export
class
Heart
{
private
heartbeatTimer
?:
NodeJS
.
Timeout
private
heartbeatInterval
=
60000
public
lastHeartbeat
=
0
private
readonly
_onChange
=
new
Emitter
<
"alive"
|
"expired"
|
"unknown"
>
(
)
readonly
onChange
=
this
.
_onChange
.
event
private
state
:
"alive"
|
"expired"
|
"unknown"
=
"expired"
public
constructor
(
private
readonly
heartbeatPath
:
string
,
private
readonly
isActive
:
(
)
=>
Promise
<
boolean
>
,
)
{
this
.
beat
=
this
.
beat
.
bind
(
this
)
this
.
alive
=
this
.
alive
.
bind
(
this
)
}
private
setState
(
state
:
typeof
this
.
state
)
{
if
(
this
.
state
!==
state
)
{
this
.
state
=
state
this
.
_onChange
.
emit
(
this
.
state
)
}
}
public
alive
(
)
:
boolean
{
const
now
=
Date
.
now
(
)
return
now
-
this
.
lastHeartbeat
<
this
.
heartbeatInterval
}
/**
* Write to the heartbeat file if we haven't already done so within the
* timeout and start or reset a timer that keeps running as long as there is
* activity. Failures are logged as warnings.
*/
public
async
beat
(
)
:
Promise
<
void
>
{
if
(
this
.
alive
(
)
)
{
this
.
setState
(
"alive"
)
return
}
logger
.
debug
(
"heartbeat"
)
this
.
lastHeartbeat
=
Date
.
now
(
)
if
(
typeof
this
.
heartbeatTimer
!==
"undefined"
)
{
clearTimeout
(
this
.
heartbeatTimer
)
}
this
.
heartbeatTimer
=
setTimeout
(
async
(
)
=>
{
try
{
if
(
await
this
.
isActive
(
)
)
{
this
.
beat
(
)
}
else
{
this
.
setState
(
"expired"
)
}
}
catch
(
error
:
unknown
)
{
logger
.
warn
(
(
error
as
Error
)
.
message
)
this
.
setState
(
"unknown"
)
}
}
,
this
.
heartbeatInterval
)
this
.
setState
(
"alive"
)
try
{
return
await
fs
.
writeFile
(
this
.
heartbeatPath
,
""
)
}
catch
(
error
:
any
)
{
logger
.
warn
(
error
.
message
)
}
}
/**
* Call to clear any heartbeatTimer for shutdown.
*/
public
dispose
(
)
:
void
{
if
(
typeof
this
.
heartbeatTimer
!==
"undefined"
)
{
clearTimeout
(
this
.
heartbeatTimer
)
}
}
}
Back
|
FazBrowse Home
|
New Git URL