mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-18 08:03:30 +02:00
fix bug on taking control of a device: set a lock for exclusive access (#650)
* fix bug on taking control of a device: set a lock for exclusive access Signed-off-by: Denis barbaron <denis.barbaron@orange.com> * update the locking function Signed-off-by: Denis barbaron <denis.barbaron@orange.com> --------- Signed-off-by: Denis barbaron <denis.barbaron@orange.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright © 2019 contains code contributed by Orange SA, authors: Denis Barbaron - Licensed under the Apache license 2.0
|
||||
* Copyright © 2019,2023 contains code contributed by Orange SA, authors: Denis Barbaron - Licensed under the Apache license 2.0
|
||||
**/
|
||||
|
||||
var util = require('util')
|
||||
@@ -18,6 +18,7 @@ var wirerouter = require('../../../wire/router')
|
||||
|
||||
const apiutil = require('../../../util/apiutil')
|
||||
const jwtutil = require('../../../util/jwtutil')
|
||||
const lockutil = require('../../../util/lockutil')
|
||||
|
||||
var log = logger.createLogger('api:controllers:user')
|
||||
|
||||
@@ -128,16 +129,12 @@ function addUserDevice(req, res) {
|
||||
var serial = req.hasOwnProperty('body') ? req.body.serial : req.swagger.params.serial.value
|
||||
var timeout = req.hasOwnProperty('body') ? req.body.timeout ||
|
||||
null : req.swagger.params.timeout.value || null
|
||||
const lock = {}
|
||||
|
||||
dbapi.loadDevice(req.user.groups.subscribed, serial)
|
||||
.then(function(cursor) {
|
||||
cursor.next(function(err, device) {
|
||||
if (err) {
|
||||
return res.status(404).json({
|
||||
success: false
|
||||
, description: 'Device not found'
|
||||
})
|
||||
}
|
||||
lockutil.lockGenericDevice(req, res, lock, dbapi.lockDeviceByCurrent)
|
||||
.then(function(lockingSuccessed) {
|
||||
if (lockingSuccessed) {
|
||||
const device = lock.device
|
||||
|
||||
datautil.normalize(device, req.user)
|
||||
if (!deviceutil.isAddable(device, req.user)) {
|
||||
@@ -193,14 +190,14 @@ function addUserDevice(req, res) {
|
||||
)
|
||||
)
|
||||
])
|
||||
})
|
||||
}
|
||||
return false
|
||||
})
|
||||
.catch(function(err) {
|
||||
log.error('Failed to load device "%s": ', req.params.serial, err.stack)
|
||||
res.status(500).json({
|
||||
success: false
|
||||
, description: 'Internal Server Error'
|
||||
})
|
||||
apiutil.internalError(res, `Failed to take control of ${serial} device: `, err.stack)
|
||||
})
|
||||
.finally(function() {
|
||||
lockutil.unlockDevice(lock)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user