Skip to content

Commit

Permalink
fix(core): emit reconnect on non-channel error in dataset copy job li…
Browse files Browse the repository at this point in the history
…stener (#3167)

* fix(core): emit reconnect on non-channel error in job listener

* fix(core): keep progress on reconnect

* fix(core): use object form when printing debug events

Co-authored-by: Espen Hovlandsdal <espen@hovlandsdal.com>
  • Loading branch information
j33ty and rexxars committed Mar 28, 2022
1 parent bf66218 commit d891474
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions packages/@sanity/core/src/commands/dataset/copyDatasetCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,65 +22,79 @@ Examples

const progress = (url) => {
return new Observable((observer) => {
const progressSource = new EventSource(url)
let progressSource = new EventSource(url)
let stopped = false

function onError(error) {
if (progressSource) {
progressSource.close()
}

debug(`Error received: ${error}`)
if (stopped) {
return
}
observer.next({type: 'reconnect'})
progressSource = new EventSource(url)
}

function onChannelError(error) {
stopped = true
progressSource.close()
observer.error(error)
}

function onMessage(event) {
const data = JSON.parse(event.data)
if (data.state === 'failed') {
debug(`Job failed. Data: ${event}`)
debug('Job failed. Data: %o', event)
observer.error(event)
} else if (data.state === 'completed') {
debug(`Job succeeded. Data: ${event}`)
debug('Job succeeded. Data: %o', event)
onComplete()
} else {
debug(`Job progressed. Data: ${event}`)
debug(`Job progressed. Data: %o`, event)
observer.next(data)
}
}

function onComplete() {
progressSource.removeEventListener('error', onError)
progressSource.removeEventListener('channelError', onError)
progressSource.removeEventListener('channel_error', onChannelError)
progressSource.removeEventListener('job', onMessage)
progressSource.removeEventListener('done', onComplete)
progressSource.close()
observer.complete()
}

progressSource.addEventListener('error', onError)
progressSource.addEventListener('channelError', onError)
progressSource.addEventListener('channel_error', onChannelError)
progressSource.addEventListener('job', onMessage)
progressSource.addEventListener('done', onComplete)
})
}

const followProgress = (jobId, client, output) => {
const spinner = output
.spinner({
text: `Copy in progress: 0%`,
})
.start()
let currentProgress = 0

const spinner = output.spinner({}).start()
const listenUrl = client.getUrl(`jobs/${jobId}/listen`)

debug(`Listening to ${listenUrl}`)

progress(listenUrl).subscribe({
next: (event) => {
const eventProgress = event.progress ? event.progress : 0
if (typeof event.progress === 'number') {
currentProgress = event.progress
}

spinner.text = `Copy in progress: ${eventProgress}%`
spinner.text = `Copy in progress: ${currentProgress}%`
},
error: () => {
spinner.fail('There was an error copying the dataset.')
error: (err) => {
spinner.fail(`There was an error copying the dataset: ${err.message}`)
},
complete: () => {
spinner.succeed(`Copy finished.`)
spinner.succeed('Copy finished.')
},
})
}
Expand Down Expand Up @@ -152,7 +166,7 @@ export default {
)

if (flags.detach) {
output.print(`Copy initiated.`)
output.print('Copy initiated.')
output.print(
`\nRun:\n\n sanity dataset copy --attach ${response.jobId}\n\nto watch attach`
)
Expand Down

3 comments on commit d891474

@vercel
Copy link

@vercel vercel bot commented on d891474 Mar 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

studio-workshop – ./dev/workshop

studio-workshop.sanity.build
studio-workshop-git-next.sanity.build

@vercel
Copy link

@vercel vercel bot commented on d891474 Mar 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio-git-next.sanity.build
test-studio.sanity.build

@vercel
Copy link

@vercel vercel bot commented on d891474 Mar 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

perf-studio – ./

perf-studio.sanity.build
perf-studio-git-next.sanity.build

Please sign in to comment.