Skip to content

Commit a823cb6

Browse files
committed
feat: restart experiment when disconnect or clock is 23:59:59
1 parent 2363655 commit a823cb6

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

packages/simulator/lib/vehicles/truck.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Truck extends Vehicle {
5454
if (this.cargo.indexOf(this.booking) > -1)
5555
return info('ERR: Already picked up', this.id, this.booking.id)
5656

57-
debug('Pickup cargo', this.id, this.booking.id)
57+
info('Pickup cargo', this.id, this.booking.id)
5858
// this.cargo = [...this.cargo, this.booking?.passenger]
5959
this.cargo.push(this.booking)
6060
this.cargoEvents.next(this)

packages/simulator/web/routes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { saveParameters } = require('../lib/fileUtils')
33
const { info } = require('../lib/log')
44
const { defaultEmitters, ignoreWelcomeMessage } = require('../config')
55
const cookie = require('cookie')
6+
const moment = require('moment')
67

78
function subscribe(experiment, socket) {
89
return [
@@ -29,6 +30,10 @@ function subscribe(experiment, socket) {
2930
function start(socket) {
3031
const experiment = engine.createExperiment({ defaultEmitters })
3132
experiment.subscriptions = subscribe(experiment, socket)
33+
experiment.virtualTime.waitUntil(moment().endOf('day').valueOf()).then(() => {
34+
info('Experiment finished. Restarting...')
35+
process.kill(process.pid, 'SIGUSR2')
36+
})
3237
socket.data.experiment = experiment
3338
}
3439

@@ -70,9 +75,11 @@ function register(io) {
7075

7176
socket.on('disconnect', (reason) => {
7277
info('Client disconnected', reason, 'shutting down experiment in 60s')
78+
7379
clearTimeout(socket.data.timeout)
7480
socket.data.timeout = setTimeout(() => {
7581
info('Shutting down experiment')
82+
process.kill(process.pid, 'SIGUSR2')
7683
socket.data.experiment.subscriptions.map((e) => e.unsubscribe())
7784
}, 60_000)
7885
})

0 commit comments

Comments
 (0)