Skip to content
This repository has been archived by the owner on Nov 14, 2019. It is now read-only.

Better dev exit #98

Merged
merged 3 commits into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions src/commands/service/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,12 @@ export default class ServiceDev extends Command {
const service = await ServiceCreate.run([JSON.stringify(definition)])
const envs = (flags.env || []).reduce((prev, value) => [...prev, '--env', value], [] as string[])
const instance = await ServiceStart.run([service.hash, ...envs])
await ServiceLog.run([instance.hash])
const stream = await ServiceLog.run([instance.hash])

process.on('SIGINT', async () => {
try {
await ServiceStop.run([instance.hash])
await ServiceDelete.run([service.hash, '--confirm'])
} catch (error) {
this.error(error)
} finally {
process.exit(0)
}
process.once('SIGINT', async () => {
stream.destroy()
await ServiceStop.run([instance.hash])
await ServiceDelete.run([service.hash, '--confirm'])
})
}
}
22 changes: 17 additions & 5 deletions src/commands/service/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default class ServiceLogs extends Command {

async run() {
const {args, flags} = this.parse(ServiceLogs)
const streams: (() => any)[] = []

const dockerServices = await this.docker.service.list({
filters: {
Expand All @@ -70,13 +71,14 @@ export default class ServiceLogs extends Command {
follow: flags.follow,
tail: flags.tail && flags.tail >= 0 ? flags.tail : 'all'
}) as any)
streams.push(() => logs.destroy())
logs
.on('data', (buffer: Buffer) => parseLog(buffer).forEach(x => this.log(x)))
.on('error', (error: Error) => { throw error })
.on('error', (error: Error) => { this.warn('error in docker stream: ' + error.message) })
}

if (flags.results) {
this.api.execution.stream({
const results = this.api.execution.stream({
filter: {
instanceHash: args.INSTANCE_HASH,
statuses: [
Expand All @@ -86,19 +88,29 @@ export default class ServiceLogs extends Command {
taskKey: flags.task
}
})
streams.push(() => results.cancel())
results
.on('data', data => this.log(this.formatResult(data)))
.on('error', (error: Error) => { throw error })
.on('error', (error: Error) => { this.warn('error in result stream: ' + error.message) })
}

if (flags.events) {
this.api.event.stream({
const events = this.api.event.stream({
filter: {
instanceHash: args.INSTANCE_HASH,
key: flags.event,
}
})
streams.push(() => events.cancel())
events
.on('data', (data: any) => this.log(this.formatEvent(data)))
.on('error', (error: Error) => { throw error })
.on('error', (error: Error) => { this.warn('error in event stream: ' + error.message) })
}

return {
destroy: () => {
streams.forEach(s => s())
}
}
}

Expand Down