mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-21 05:15:17 +02:00
Reap dead devices using timeouts. Previously, if a provider died without being able to clean up properly, the device would stay as a ghost.
This commit is contained in:
@@ -7,6 +7,10 @@ var wireutil = require('../wire/util')
|
||||
|
||||
var dbapi = Object.create(null)
|
||||
|
||||
dbapi.close = function(options) {
|
||||
return db.close(options)
|
||||
}
|
||||
|
||||
dbapi.saveUserAfterLogin = function(user) {
|
||||
return db.run(r.table('users').get(user.email).update({
|
||||
name: user.name
|
||||
@@ -56,6 +60,7 @@ dbapi.saveDevice = function(serial, device) {
|
||||
, status: device.status
|
||||
, statusChangedAt: r.now()
|
||||
, createdAt: r.now()
|
||||
, lastHeartbeatAt: r.now()
|
||||
}
|
||||
, {
|
||||
upsert: true
|
||||
@@ -84,6 +89,7 @@ dbapi.unsetDeviceOwner = function(serial, owner) {
|
||||
dbapi.setDeviceAbsent = function(serial) {
|
||||
return db.run(r.table('devices').get(serial).update({
|
||||
present: false
|
||||
, lastHeartbeatAt: null
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -114,4 +120,26 @@ dbapi.loadDevice = function(serial) {
|
||||
return db.run(r.table('devices').get(serial))
|
||||
}
|
||||
|
||||
dbapi.updateDeviceHeartbeat = function(serial) {
|
||||
return db.run(
|
||||
r.table('devices').get(serial).update({
|
||||
lastHeartbeatAt: r.now()
|
||||
})
|
||||
, {
|
||||
noreply: true
|
||||
, durability: 'soft'
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
dbapi.getDeadDevices = function(timeout) {
|
||||
return db.run(
|
||||
r.table('devices')
|
||||
.between(null, r.now().sub(timeout / 1000), {
|
||||
index: 'lastHeartbeatAt'
|
||||
})
|
||||
.pluck('serial')
|
||||
)
|
||||
}
|
||||
|
||||
module.exports = dbapi
|
||||
|
||||
@@ -33,8 +33,15 @@ db.connect = (function() {
|
||||
}
|
||||
})()
|
||||
|
||||
// Close connection, we don't really care if it hasn't been created yet or not
|
||||
db.close = function() {
|
||||
return db.connect().then(function(conn) {
|
||||
return rutil.close(conn)
|
||||
})
|
||||
}
|
||||
|
||||
// Small utility for running queries without having to acquire a connection
|
||||
db.run = function(q) {
|
||||
db.run = function(q, options) {
|
||||
return db.connect().then(function(conn) {
|
||||
return rutil.run(conn, q)
|
||||
})
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
var r = require('rethinkdb')
|
||||
|
||||
module.exports = {
|
||||
users: {
|
||||
primaryKey: 'email'
|
||||
@@ -8,6 +10,7 @@ module.exports = {
|
||||
ownerEmail: function(device) {
|
||||
return device('owner')('email')
|
||||
}
|
||||
, lastHeartbeatAt: null
|
||||
}
|
||||
}
|
||||
, logs: {
|
||||
|
||||
Reference in New Issue
Block a user