Skip to content

Commit

Permalink
feat(Listener): Add timelimit option
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Aug 4, 2020
1 parent e1b0097 commit 8d33475
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
32 changes: 26 additions & 6 deletions src/base/Listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,32 @@ export abstract class Listener extends Executor {
*/
public readonly timeout: number = 0

/**
* Maximum time that the process will run.
*/
public readonly timelimit: number = 0

/**
* The UNIX timestamp when the listener was `start`ed.
*/
private started = 0

/**
* The UNIX timestamp of the last call to `dispatch`
* or `notified`.
*/
private heartbeat = 0

public constructor(family = 'li', servers: Server[] = [], timeout = 0) {
public constructor(
family = 'li',
servers: Server[] = [],
timeout = 0,
timelimit = 0
) {
super(family)
this.servers = servers
this.timeout = timeout
this.timelimit = timelimit
}

/**
Expand Down Expand Up @@ -137,14 +153,18 @@ export abstract class Listener extends Executor {
process.on('SIGINT', stop)
process.on('SIGTERM', stop)

this.started = Date.now()
this.heartbeat = Date.now()
if (this.timeout > 0) {
if (this.timeout > 0 || this.timelimit > 0) {
const interval = setInterval((): void => {
const duration = (Date.now() - this.heartbeat) / 1000
console.log(duration, this.heartbeat, this.timeout)
if (duration > this.timeout) {
const now = Date.now()
const sinceHeartbeat = (now - this.heartbeat) / 1000
const sinceStarted = (now - this.started) / 1000
if (sinceHeartbeat > this.timeout || sinceStarted > this.timelimit) {
clearInterval(interval)
log.info(`Timed out after ${Math.round(duration)}s`)
if (sinceHeartbeat > this.timeout)
log.info(`Timed out after ${Math.round(sinceHeartbeat)}s`)
else log.info(`Time limit reached after ${Math.round(sinceStarted)}s`)
stop()
}
}, 1000)
Expand Down
5 changes: 3 additions & 2 deletions src/base/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ export class Manager extends Listener {
servers: Server[] = [],
delegator: Delegator = new Delegator([new Worker()]),
queuer: Queuer = new Queuer(),
timeout = 0
timeout = 0,
timelimit = 0
) {
super('ma', servers, timeout)
super('ma', servers, timeout, timelimit)
this.delegator = delegator
this.queuer = queuer
}
Expand Down
4 changes: 2 additions & 2 deletions src/cli/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ export async function init(config: Config): Promise<Listener> {
// Configure the queue
const queuer = new Queuer(config)

const { timeout } = config
return new Manager(servers, delegator, queuer, timeout)
const { timeout, timelimit } = config
return new Manager(servers, delegator, queuer, timeout, timelimit)
}
10 changes: 8 additions & 2 deletions src/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@
"default": false
},
"timeout": {
"description": "Seconds of inactivity after which the process should stop.",
"$comment": "Zero means no timeout.",
"description": "Duration of inactivity after which the process should stop.",
"$comment": "Seconds. Zero means no timeout.",
"type": "number",
"default": 0
},
"timelimit": {
"description": "Maximum duration for the process.",
"$comment": "Seconds. Zero means no time limit.",
"type": "number",
"default": 0
},
Expand Down
11 changes: 9 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,19 @@ export class Config {
ws: boolean | string = false

/**
* Seconds of inactivity after which the process should stop.
* Duration of inactivity after which the process should stop.
*
* Zero means no timeout.
* Seconds. Zero means no timeout.
*/
timeout = 0

/**
* Maximum duration for the process.
*
* Seconds. Zero means no time limit.
*/
timelimit = 0

/**
* List of peer addresses.
*
Expand Down

0 comments on commit 8d33475

Please sign in to comment.