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

Refactor [github] token persistence, again #1906

Merged
merged 8 commits into from
Aug 19, 2018
Merged

Refactor [github] token persistence, again #1906

merged 8 commits into from
Aug 19, 2018

Conversation

paulmelnikow
Copy link
Member

@paulmelnikow paulmelnikow commented Aug 12, 2018

Instead of saving tokens on a timer, save them when they change. Use EventEmitter to keep the components loosely coupled.

This is easier to reason about, much easier to test, and better supports adapting to backends which support atomic operations.

This replaces json-autosave, which was a bit difficult to read and also hard to test, with fsos, the lower-level utility it’s built on.

The most important goal is to provide a different token-persistence backend and I don't want to get too sidetracked by the rest of the slog that is #1205. There are no tests that everything is wired together correctly, nor any that cover the token acceptor, so I suppose I could either add a relatively wide-bracket functional test here, or test this manually (at least for the moment).

Ref: #1848

Todo:

  • Send errors to Sentry
  • Token removal test

Save the tokens things change instead of on a timer. Use EventEmitter to keep the components loosely coupled.

This is easier to reason about, much easier to test, and better supports adapting to backends which support atomic operations.

This switches from json-autosave, which was a bit difficult to read and also hard to test, to fsos, the lower-level utility it’s built on.
@paulmelnikow paulmelnikow added the core Server, BaseService, GitHub auth, Shared helpers label Aug 12, 2018
@shields-ci
Copy link

Messages
📖

✨ Thanks for your contribution to Shields, @paulmelnikow!

Generated by 🚫 dangerJS

Copy link
Member

@PyvesB PyvesB left a comment

Choose a reason for hiding this comment

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

This is definitely an improvement, it's easier to reason with this observer-style pattern. 👍

@@ -54,8 +44,8 @@ describe('Token persistence', function() {
})
})

context('when shutting down', function() {
it('writes added tokens to the file', async function() {
context('when tokens are added', function() {
Copy link
Member

Choose a reason for hiding this comment

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

Shall we add a test or two for when tokens are removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, makes sense.

@@ -110,6 +113,7 @@ function setRoutes(server) {
}, 10000)
}
addGithubToken(data.token)
emitter.emit('token-added', data.token)
Copy link
Member

@PyvesB PyvesB Aug 12, 2018

Choose a reason for hiding this comment

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

The listeners aren't actually using the token that is being passed around (as they call getAllTokenIds to get the whole list), but it probably doesn't hurt either to include it in the emitted event.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea… and the next iteration of this, with a redis backend, will use that. I believe Redis can do inserts and deletes instead of rewriting the whole thing.

@paulmelnikow
Copy link
Member Author

@PyvesB Can I get your 👍 on this?

@PyvesB
Copy link
Member

PyvesB commented Aug 17, 2018

@paulmelnikow well I believe there's still that pending comment about the token removal test. But if you no longer think it's necessary, I'm happy to sign this off. 😉

@paulmelnikow
Copy link
Member Author

Ah, right! Sorry, I missed that when I was reading it just now.

@paulmelnikow
Copy link
Member Author

@PyvesB updated!

Tests are failing because of #1938.

clock.restore()
await sleep(200)
githubAuth.rmGithubToken(toRemove)
await persistence.noteTokenAdded(toRemove)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be noteTokenRemoved?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah thanks, I caught that in #1939 but forgot to fix it here.

@paulmelnikow paulmelnikow merged commit b10a6a4 into badges:master Aug 19, 2018
@shields-deployment
Copy link

This pull request was merged to master branch. Now this change is waiting for deployment.
Deploys usually happen every few weeks. After deployment changes are copied to gh-pages branch.

This badge displays deployment status:

@paulmelnikow paulmelnikow deleted the save-tokens-when-changed branch August 19, 2018 03:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Server, BaseService, GitHub auth, Shared helpers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants