Decaf cleanup: remove unnecessary returns

This commit is contained in:
Eric Mc Sween 2020-09-02 16:58:41 -04:00
parent 08be54a43e
commit 99648341e2

View file

@ -8,7 +8,6 @@
// Fix any style issues and re-enable lint. // Fix any style issues and re-enable lint.
/* /*
* decaffeinate suggestions: * decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS103: Rewrite code to no longer use __guard__ * DS103: Rewrite code to no longer use __guard__
* DS205: Consider reworking code to avoid use of IIFEs * DS205: Consider reworking code to avoid use of IIFEs
* DS207: Consider shorter variations of null checks * DS207: Consider shorter variations of null checks
@ -120,13 +119,11 @@ module.exports = DockerRunner = {
{ err: error, project_id }, { err: error, project_id },
'error running container so destroying and retrying' 'error running container so destroying and retrying'
) )
return DockerRunner.destroyContainer(name, null, true, function ( DockerRunner.destroyContainer(name, null, true, function (error) {
error
) {
if (error != null) { if (error != null) {
return callback(error) return callback(error)
} }
return DockerRunner._runAndWaitForContainer( DockerRunner._runAndWaitForContainer(
options, options,
volumes, volumes,
timeout, timeout,
@ -134,12 +131,13 @@ module.exports = DockerRunner = {
) )
}) })
} else { } else {
return callback(error, output) callback(error, output)
} }
}) })
// pass back the container name to allow it to be killed
return name return name
}, // pass back the container name to allow it to be killed },
kill(container_id, callback) { kill(container_id, callback) {
if (callback == null) { if (callback == null) {
@ -147,7 +145,7 @@ module.exports = DockerRunner = {
} }
logger.log({ container_id }, 'sending kill signal to container') logger.log({ container_id }, 'sending kill signal to container')
const container = dockerode.getContainer(container_id) const container = dockerode.getContainer(container_id)
return container.kill(function (error) { container.kill(function (error) {
if ( if (
error != null && error != null &&
__guardMethod__( __guardMethod__(
@ -164,9 +162,9 @@ module.exports = DockerRunner = {
} }
if (error != null) { if (error != null) {
logger.error({ err: error, container_id }, 'error killing container') logger.error({ err: error, container_id }, 'error killing container')
return callback(error) callback(error)
} else { } else {
return callback() callback()
} }
}) })
}, },
@ -178,7 +176,7 @@ module.exports = DockerRunner = {
const callback = function (...args) { const callback = function (...args) {
_callback(...args) _callback(...args)
// Only call the callback once // Only call the callback once
return (_callback = function () {}) _callback = function () {}
} }
const { name } = options const { name } = options
@ -189,7 +187,7 @@ module.exports = DockerRunner = {
const callbackIfFinished = function () { const callbackIfFinished = function () {
if (streamEnded && containerReturned) { if (streamEnded && containerReturned) {
return callback(null, output) callback(null, output)
} }
} }
@ -199,10 +197,10 @@ module.exports = DockerRunner = {
} }
output = _output output = _output
streamEnded = true streamEnded = true
return callbackIfFinished() callbackIfFinished()
} }
return DockerRunner.startContainer( DockerRunner.startContainer(
options, options,
volumes, volumes,
attachStreamHandler, attachStreamHandler,
@ -211,7 +209,7 @@ module.exports = DockerRunner = {
return callback(error) return callback(error)
} }
return DockerRunner.waitForContainer(name, timeout, function ( DockerRunner.waitForContainer(name, timeout, function (
error, error,
exitCode exitCode
) { ) {
@ -237,7 +235,7 @@ module.exports = DockerRunner = {
(x) => (x.SecurityOpt = null) (x) => (x.SecurityOpt = null)
) // small log line ) // small log line
logger.log({ err, exitCode, options }, 'docker container has exited') logger.log({ err, exitCode, options }, 'docker container has exited')
return callbackIfFinished() callbackIfFinished()
}) })
} }
) )
@ -364,7 +362,7 @@ module.exports = DockerRunner = {
}, },
startContainer(options, volumes, attachStreamHandler, callback) { startContainer(options, volumes, attachStreamHandler, callback) {
return LockManager.runWithLock( LockManager.runWithLock(
options.name, options.name,
(releaseLock) => (releaseLock) =>
// Check that volumes exist before starting the container. // Check that volumes exist before starting the container.
@ -375,7 +373,7 @@ module.exports = DockerRunner = {
if (err != null) { if (err != null) {
return releaseLock(err) return releaseLock(err)
} }
return DockerRunner._startContainer( DockerRunner._startContainer(
options, options,
volumes, volumes,
attachStreamHandler, attachStreamHandler,
@ -405,13 +403,13 @@ module.exports = DockerRunner = {
if (!(stats != null ? stats.isDirectory() : undefined)) { if (!(stats != null ? stats.isDirectory() : undefined)) {
return cb(DockerRunner.ERR_NOT_DIRECTORY) return cb(DockerRunner.ERR_NOT_DIRECTORY)
} }
return cb() cb()
}) })
const jobs = [] const jobs = []
for (const vol in volumes) { for (const vol in volumes) {
;((vol) => jobs.push((cb) => checkVolume(vol, cb)))(vol) ;((vol) => jobs.push((cb) => checkVolume(vol, cb)))(vol)
} }
return async.series(jobs, callback) async.series(jobs, callback)
}, },
_startContainer(options, volumes, attachStreamHandler, callback) { _startContainer(options, volumes, attachStreamHandler, callback) {
@ -429,7 +427,7 @@ module.exports = DockerRunner = {
if (error != null) { if (error != null) {
return callback(error) return callback(error)
} }
return startExistingContainer() startExistingContainer()
}) })
var startExistingContainer = () => var startExistingContainer = () =>
DockerRunner.attachToContainer( DockerRunner.attachToContainer(
@ -439,37 +437,37 @@ module.exports = DockerRunner = {
if (error != null) { if (error != null) {
return callback(error) return callback(error)
} }
return container.start(function (error) { container.start(function (error) {
if ( if (
error != null && error != null &&
(error != null ? error.statusCode : undefined) !== 304 (error != null ? error.statusCode : undefined) !== 304
) { ) {
// already running // already running
return callback(error) callback(error)
} else { } else {
return callback() callback()
} }
}) })
} }
) )
return container.inspect(function (error, stats) { container.inspect(function (error, stats) {
if ((error != null ? error.statusCode : undefined) === 404) { if ((error != null ? error.statusCode : undefined) === 404) {
return createAndStartContainer() createAndStartContainer()
} else if (error != null) { } else if (error != null) {
logger.err( logger.err(
{ container_name: name, error }, { container_name: name, error },
'unable to inspect container to start' 'unable to inspect container to start'
) )
return callback(error) callback(error)
} else { } else {
return startExistingContainer() startExistingContainer()
} }
}) })
}, },
attachToContainer(containerId, attachStreamHandler, attachStartCallback) { attachToContainer(containerId, attachStreamHandler, attachStartCallback) {
const container = dockerode.getContainer(containerId) const container = dockerode.getContainer(containerId)
return container.attach({ stdout: 1, stderr: 1, stream: 1 }, function ( container.attach({ stdout: 1, stderr: 1, stream: 1 }, function (
error, error,
stream stream
) { ) {
@ -495,7 +493,7 @@ module.exports = DockerRunner = {
return return
} }
if (this.data.length < MAX_OUTPUT) { if (this.data.length < MAX_OUTPUT) {
return (this.data += data) this.data += data
} else { } else {
logger.error( logger.error(
{ {
@ -506,7 +504,7 @@ module.exports = DockerRunner = {
`${name} exceeds max size` `${name} exceeds max size`
) )
this.data += `(...truncated at ${MAX_OUTPUT} chars...)` this.data += `(...truncated at ${MAX_OUTPUT} chars...)`
return (this.overflowed = true) this.overflowed = true
} }
} }
// kill container if too much output // kill container if too much output
@ -526,7 +524,7 @@ module.exports = DockerRunner = {
) )
) )
return stream.on('end', () => stream.on('end', () =>
attachStreamHandler(null, { stdout: stdout.data, stderr: stderr.data }) attachStreamHandler(null, { stdout: stdout.data, stderr: stderr.data })
) )
}) })
@ -539,7 +537,7 @@ module.exports = DockerRunner = {
const callback = function (...args) { const callback = function (...args) {
_callback(...args) _callback(...args)
// Only call the callback once // Only call the callback once
return (_callback = function () {}) _callback = function () {}
} }
const container = dockerode.getContainer(containerId) const container = dockerode.getContainer(containerId)
@ -551,11 +549,11 @@ module.exports = DockerRunner = {
{ container_id: containerId }, { container_id: containerId },
'timeout reached, killing container' 'timeout reached, killing container'
) )
return container.kill(function () {}) container.kill(function () {})
}, timeout) }, timeout)
logger.log({ container_id: containerId }, 'waiting for docker container') logger.log({ container_id: containerId }, 'waiting for docker container')
return container.wait(function (error, res) { container.wait(function (error, res) {
if (error != null) { if (error != null) {
clearTimeout(timeoutId) clearTimeout(timeoutId)
logger.error( logger.error(
@ -568,14 +566,14 @@ module.exports = DockerRunner = {
logger.log({ containerId }, 'docker container timed out') logger.log({ containerId }, 'docker container timed out')
error = DockerRunner.ERR_TIMED_OUT error = DockerRunner.ERR_TIMED_OUT
error.timedout = true error.timedout = true
return callback(error) callback(error)
} else { } else {
clearTimeout(timeoutId) clearTimeout(timeoutId)
logger.log( logger.log(
{ container_id: containerId, exitCode: res.StatusCode }, { container_id: containerId, exitCode: res.StatusCode },
'docker container returned' 'docker container returned'
) )
return callback(null, res.StatusCode) callback(null, res.StatusCode)
} }
}) })
}, },
@ -590,7 +588,7 @@ module.exports = DockerRunner = {
if (callback == null) { if (callback == null) {
callback = function (error) {} callback = function (error) {}
} }
return LockManager.runWithLock( LockManager.runWithLock(
containerName, containerName,
(releaseLock) => (releaseLock) =>
DockerRunner._destroyContainer( DockerRunner._destroyContainer(
@ -608,7 +606,7 @@ module.exports = DockerRunner = {
} }
logger.log({ container_id: containerId }, 'destroying docker container') logger.log({ container_id: containerId }, 'destroying docker container')
const container = dockerode.getContainer(containerId) const container = dockerode.getContainer(containerId)
return container.remove({ force: shouldForce === true }, function (error) { container.remove({ force: shouldForce === true }, function (error) {
if ( if (
error != null && error != null &&
(error != null ? error.statusCode : undefined) === 404 (error != null ? error.statusCode : undefined) === 404
@ -627,7 +625,7 @@ module.exports = DockerRunner = {
} else { } else {
logger.log({ container_id: containerId }, 'destroyed container') logger.log({ container_id: containerId }, 'destroyed container')
} }
return callback(error) callback(error)
}) })
}, },
@ -652,17 +650,14 @@ module.exports = DockerRunner = {
{ containerName: name, created, now, age, maxAge, ttl }, { containerName: name, created, now, age, maxAge, ttl },
'checking whether to destroy container' 'checking whether to destroy container'
) )
return callback(null, name, container.Id, ttl) callback(null, name, container.Id, ttl)
}, },
destroyOldContainers(callback) { destroyOldContainers(callback) {
if (callback == null) { if (callback == null) {
callback = function (error) {} callback = function (error) {}
} }
return dockerode.listContainers({ all: true }, function ( dockerode.listContainers({ all: true }, function (error, containers) {
error,
containers
) {
if (error != null) { if (error != null) {
return callback(error) return callback(error)
} }
@ -679,7 +674,7 @@ module.exports = DockerRunner = {
// strip the / prefix // strip the / prefix
// the LockManager uses the plain container name // the LockManager uses the plain container name
name = name.slice(1) name = name.slice(1)
return jobs.push((cb) => jobs.push((cb) =>
DockerRunner.destroyContainer(name, id, false, () => cb()) DockerRunner.destroyContainer(name, id, false, () => cb())
) )
} }
@ -687,7 +682,7 @@ module.exports = DockerRunner = {
} }
// Ignore errors because some containers get stuck but // Ignore errors because some containers get stuck but
// will be destroyed next time // will be destroyed next time
return async.series(jobs, callback) async.series(jobs, callback)
}) })
}, },