Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add concurrency safety #99

Merged
merged 3 commits into from
Jun 12, 2018
Merged

feat: add concurrency safety #99

merged 3 commits into from
Jun 12, 2018

Conversation

eseliger
Copy link
Collaborator

Closes #58

@codecov
Copy link

codecov bot commented May 10, 2018

Codecov Report

Merging #99 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #99      +/-   ##
==========================================
+ Coverage   99.75%   99.78%   +0.02%     
==========================================
  Files           5        5              
  Lines         409      460      +51     
  Branches       70       79       +9     
==========================================
+ Hits          408      459      +51     
  Misses          1        1
Impacted Files Coverage Δ
src/migration.ts 100% <100%> (ø) ⬆️
src/adapter.ts 100% <100%> (ø) ⬆️
src/adapters/postgres.ts 100% <100%> (ø) ⬆️
src/git.ts 100% <100%> (ø) ⬆️
src/index.ts 99.28% <100%> (+0.08%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f9d042d...2f091e1. Read the comment docs.

src/adapter.ts Outdated

public waitForPending(logger: Logger): Promise<boolean> {
let wasPending = false
return new Promise<boolean>(async (resolve, reject) => {
Copy link
Owner

Choose a reason for hiding this comment

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

making the Promise resolver async will swallow any exception that happen within it

src/adapter.ts Outdated
let wasPending = false
return new Promise<boolean>(async (resolve, reject) => {
// fail after 10 min
const timeout = setTimeout(() => reject(new PendingMigrationTimedOutError()), 1000 * 60 * 10)
Copy link
Owner

Choose a reason for hiding this comment

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

If you just want to fail the Promise after 10min, I would use Promise.race() to race the timeout Promise against the operation Promise

public async finishMigrationTask(task: Task): Promise<void> {
const { rows } = await this.client.query(SQL`
UPDATE merkel_meta SET "applied_at" = ${task.appliedAt}, "head" = ${
task.head ? task.head.sha1 : null
Copy link
Owner

Choose a reason for hiding this comment

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

Save these in variables so it doesn't get wrapped ugly?

src/cli.ts Outdated
const tasks = await Promise.all(
argv.migrations!.map(async name => {
const task = new Task({ type, migration: new Migration(name), head })
await adapter.beginMigrationTask(task)
Copy link
Owner

Choose a reason for hiding this comment

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

Are these run in parallel intentionally? Won't they output log statements etc?

logger.log(' Success\n')
while (true) {
logger.log(this.toString())
const tasks = this.newCommits.reduce<Task[]>((prev, next) => prev.concat(next.tasks), [])
Copy link
Owner

Choose a reason for hiding this comment

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

Why can't this be done anymore in a nested for loop?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it is below, but we need all of them before the migrations to create the pending entries

Copy link
Owner

Choose a reason for hiding this comment

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

wouldn't that be possible with a nested for loop too?

await adapter.beginMigrationTask({ ...status.newCommits[0].tasks[0], head: await getHead() } as Task)
let shouldHaveFinished = false
// begin execution, this should wait
const promise = new Promise<void>(async (resolve, reject) => {
Copy link
Owner

Choose a reason for hiding this comment

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

This swallows exceptions, you can make an async IIFE

src/adapter.ts Outdated
(async (): Promise<boolean> => {
// fail after 10 min
let interval: NodeJS.Timer | undefined
while (true) {
Copy link
Owner

Choose a reason for hiding this comment

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

I don't see an exit condition for this loop

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

there is only the return on l 60
don you think inf retries are too many? 😁

Copy link
Owner

Choose a reason for hiding this comment

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

But what in the case of the timeout? This loop would keep going

SET "applied_at" = ${task.appliedAt},
"head" = ${head},
"commit" = ${commit}
WHERE "id" = ${task.id}
Copy link
Owner

Choose a reason for hiding this comment

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

align statements like in other queries?

public async finishMigrationTask(task: Task): Promise<void> {
const head = task.head ? task.head.sha1 : null
const commit = task.commit ? task.commit.sha1 : null
const { rows } = await this.client.query(SQL`
Copy link
Owner

Choose a reason for hiding this comment

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

rows is unused

src/adapter.ts Outdated
@@ -14,6 +16,11 @@ export class UnsupportedDialectError extends Error {
}
}

export class PendingMigrationTimedOutError extends Error {
/* istanbul ignore next */
public name = 'PendingMigrationTimedOutError'
Copy link
Owner

Choose a reason for hiding this comment

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

should be a string literal type and readonly

})
if (!answer.continue) {
process.exit(0)
while (true) {
Copy link
Owner

Choose a reason for hiding this comment

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

Should this retry an infinite amount of times?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hmm do you think it should fail?

src/cli.ts Outdated
}
// create pending tasks
for (const task of tasks) {
await adapter.beginMigrationTask(task)
Copy link
Owner

Choose a reason for hiding this comment

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

Between this and waitForPendingMigrations(), could another process have started the same migration?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hm yes, I added some failure recovery

@eseliger eseliger merged commit a1f3f65 into master Jun 12, 2018
@eseliger eseliger deleted the pending-migrations branch June 12, 2018 03:30
@felixfbecker
Copy link
Owner

🎉 This PR is included in version 0.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants