refactor(repository): improve usability of repository #1114
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR consolidates the repository methods which had become fractured so they're all defined in a single package. It moves the buffers and the scheduler repositories into the same package as other repository methods. It also introduces 3 patterns for recent issues we've seen while developing complex features:
Transactions may need to be shared between methods that are defined on separate repositories. For example, to get transactional safety for workflow run versus step run state as part of Bugfix - stalled worklowruns #1085, we need to run queries against step runs which were previous in the
StepRunRepository
. The solution in that PR (and elsewhere in the repo) was to passStepRunRepository
as a field dependency ofWorkflowRunRepository
.There is now a
sharedRepository
which can be used to share underlying methods and transactions. As an example, I've modified theDeferredStepRunEvent
(previously called from WorkflowRunRepository) to:Each repository should embed
*sharedRepository
. This also makes initializing repositories a bit cleaner, as we don't need to pass the same validator, logger, or pgxpool around everywhere.Similar to the above, buffers are now defined on the
sharedRepository
, which gives us a single place where we need to track buffers and ensure they're cleaned up. They can also be called across multiple separate repositories. I've made the additional change of changing the buffer signature fromBuffItem
, which previously returned achan
with a result or error, and modified the signature to the following:The reasoning here is that these are the only 2 patterns that have been used in the repository for buffering items, and dealing with multiple channels and context passed back to the caller is incredibly error-prone (I found a few instances of results not being awaited when they should have been). This also makes it much easier for a buffered write to respect a context timeout (we were previously setting a timer and adding an additional case for timeouts when awaiting results).
Callback patterns -- there are several instances where we've wanted to hook into a pub/sub message or other type of callback inside of repository methods ( this was a challenge with Feat skip states for workflows #1075 ), but we've been hesitant to do this because we don't want to pass a pub/sub dependency through to the repository.
There's now an easy way to define callbacks on repository methods via the following:
The callbacks are utilizing the same underlying
Do
method as previously, which is non-blocking and only logs errors. We may have to tweak signatures here if we want a failed pre-commit hook to fail the transaction.Type of change