 		containerPromise.catch ->
 			fetch(app)
 			.then (imageInfo) ->
-				utils.mixpanelTrack('Application install', app)
-				logSystemEvent('Installing application ' + app.imageId)
+				logSystemEvent(logTypes.installApp, app)
 				updateDeviceState(status: 'Installing')
 
 				ports = {}

					'/var/run/docker.sock:/run/docker.sock'
				]
			)
			# Bluebird throws OperationalError for errors resulting in the normal execution of a promisified function.
			.catch Promise.OperationalError, (err) ->
				# Report the message from the original cause to the user.
				message = err.cause.json ? err.cause.message ? err.message
				logSystemEvent("Unable to start application #{app.imageId} due to:\n#{message}")
				# And rethrow the error, to be handled later as necessary.
				throw err
			.then ->
				updateDeviceState(commit: app.commit)
				logger.attach(app)
	.tap ->
		utils.mixpanelTrack('Application start', app.imageId)
		logSystemEvent('Starting application ' + app.imageId)
	.finally ->
		updateDeviceState(status: 'Idle')


		channel: "device-#{uuid.value}-logs"
	)

logTypes =
	stopApp:
		eventName: 'Application kill'
		humanName: 'Killing application'
	stopAppSuccess:
		eventName: 'Application stop'
		humanName: 'Killed application'

	downloadApp:
		eventName: 'Application download'
		humanName: 'Downloading application'
	downloadAppSuccess:
		eventName: 'Application downloaded'
		humanName: 'Downloaded application'

	installApp:
		eventName: 'Application install'
		humanName: 'Installing application'

	startApp:
		eventName: 'Application start'
		humanName: 'Starting application'
	startAppSuccess:
		eventName: 'Application started'
		humanName: 'Started application'
	startAppError:
		eventName: 'Application started'
		humanName: 'Failed to start application'

	updateApp:
		eventName: 'Application update'
		humanName: 'Updating application'

logSystemEvent = (logType, app, err) ->
	message = "#{logType.humanName} '#{app.imageId}'"
	if err?
		# Report the message from the original cause to the user.
		errMessage = err.cause.json ? err.cause.message ? err.message
		message += " due to '#{errMessage}'"
	logger.log({ message, isSystem: true })
	utils.mixpanelTrack(logType.eventName, {app, err})
	return

kill = (app) ->
	logSystemEvent(logTypes.stopApp, app)
	updateDeviceState(status: 'Stopping')
	container = docker.getContainer(app.containerId)
	console.log('Stopping and deleting container:', container)
