Skip to content

Instantly share code, notes, and snippets.

@podviaznikov
Last active June 1, 2017 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podviaznikov/a66e26b08209336b1da798b2ef213f73 to your computer and use it in GitHub Desktop.
Save podviaznikov/a66e26b08209336b1da798b2ef213f73 to your computer and use it in GitHub Desktop.
error-handling
// ideally those should be sent in parallel
// failure of one shouldn't impact failure of others
function sendNotifications (data, instance, container) {
try {
slack.sendNotification(data, instance, container)
} catch (err) {
// retry sending a message
slack.sendNotification(data, instance, container)
}
try {
github.sendPullRequestComment(data, instance, container)
} catch (err) {
// retry sending a message
github.sendPullRequestComment(data, instance, container)
}
try {
websockets.sendUpdateToClients(data, instance, container)
} catch (err) {
// retry sending a message
websockets.sendUpdateToClients(data, instance, container)
}
}
function createAndStartInstance (data) {
try {
const instance = db.saveInstanceData(data)
const host = orchestrator.createHostIfNeeded(data)
const networkData = network.setupNetworkIfNeeded(data)
const image = docker.buildImage(data, host)
const build = db.saveBuildData(data, image, host)
const dockerContainer = docker.createAndRunContainer(data, build)
const container = db.saveContainerData(data, dockerContainer, instance)
const containerIP = network.attachContainerToNetwork(networkData, container)
const networkData = db.saveNetworkData(container, ip)
sendNotifications(data, instance, container)
} catch(err) {
if (err instanceof InstanceDBError) {
websockets.sendErrorToClients("Cannot create an instance")
return
}
if (err instanceof HostCreationError) {
websockets.sendErrorToClients("Cannot create host")
return
}
if (err instanceof NetworkCreationError) {
websockets.sendErrorToClients("Cannot create a network")
// delete host, since we cannot use it
try {
orchestrator.deleteHost(host)
} catch(err) {
// TODO if deletion failed we want to retry
}
}
if (err instanceof BuildImageError) {
websockets.sendErrorToClients("Cannot build an image")
return
}
if (err instanceof BuildDBError) {
websockets.sendErrorToClients("Cannot save build data into database")
return
}
if (err instanceof ContainerCreateError) {
websockets.sendErrorToClients("Cannot create and start container")
return
}
if (err instanceof ContainerDBError) {
websockets.sendErrorToClients("Cannot save conatiner data into database")
// delete running container. It's broken now.
try {
docker.removeContainer(container)
} catch (err) {
// TODO what are we going to do if this failed
}
return
}
if (err instanceof NetworkAttachError) {
// retry attaching container to the network
try {
const containerIP = network.attachContainerToNetwork(network)
const networkData = db.saveNetworkData(container, ip)
sendNotifications(data, instance, container)
} catch(err) {
// if retry failed send error message
websockets.sendErrorToClients("Cannot attach container to the network")
// delete running container. It's broken now.
try {
docker.removeContainer(container)
} catch (err) {
// TODO what are we going to do if this failed
}
}
}
if (err instanceof NetworkDataDBError) {
// if retry failed send error message
websockets.sendErrorToClients("Cannot save IP for new container into the DB")
try {
network.removeIPFromNetwork(ip)
} catch(err) {
// TODO handle this use case, otherwise network is invalid
}
// delete running container. It's broken now.
try {
docker.removeContainer(container)
} catch (err) {
// TODO what are we going to do if this failed
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment