-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
125 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package river | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"time" | ||
|
||
"github.com/riverqueue/river/riverdriver" | ||
) | ||
|
||
// JobCompleteTx marks the job as completed as part of transaction tx. If tx is | ||
// rolled back, the completion will be as well. | ||
// | ||
// The function needs to know the type of the River database driver, which is | ||
// the same as the one in use by Client, but the other generic parameters can be | ||
// inferred. An invocation should generally look like: | ||
// | ||
// _, err := river.JobCompleteTx[*riverpgxv5.Driver](ctx, tx, job) | ||
// if err != nil { | ||
// // handle error | ||
// } | ||
// | ||
// Returns the updated, completed job. | ||
func JobCompleteTx[TDriver riverdriver.Driver[TTx], TTx any, TArgs JobArgs](ctx context.Context, tx TTx, job *Job[TArgs]) (*Job[TArgs], error) { | ||
if job.State != JobStateRunning { | ||
return nil, errors.New("job must be running") | ||
} | ||
|
||
var driver TDriver | ||
jobRow, err := driver.UnwrapExecutor(tx).JobSetStateIfRunning(ctx, riverdriver.JobSetStateCompleted(job.ID, time.Now())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
updatedJob := &Job[TArgs]{JobRow: jobRow} | ||
|
||
if err := json.Unmarshal(updatedJob.EncodedArgs, &updatedJob.Args); err != nil { | ||
return nil, err | ||
} | ||
|
||
return updatedJob, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package river | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/jackc/pgx/v5" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/riverqueue/river/internal/riverinternaltest" | ||
"github.com/riverqueue/river/internal/riverinternaltest/testfactory" | ||
"github.com/riverqueue/river/internal/util/ptrutil" | ||
"github.com/riverqueue/river/riverdriver" | ||
"github.com/riverqueue/river/riverdriver/riverpgxv5" | ||
) | ||
|
||
func TestJobCompleteTx(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx := context.Background() | ||
|
||
type JobArgs struct { | ||
JobArgsReflectKind[JobArgs] | ||
} | ||
|
||
type testBundle struct { | ||
exec riverdriver.Executor | ||
tx pgx.Tx | ||
} | ||
|
||
setup := func(t *testing.T) *testBundle { | ||
t.Helper() | ||
|
||
tx := riverinternaltest.TestTx(ctx, t) | ||
|
||
return &testBundle{ | ||
exec: riverpgxv5.New(nil).UnwrapExecutor(tx), | ||
tx: tx, | ||
} | ||
} | ||
|
||
t.Run("CompletesJob", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
bundle := setup(t) | ||
|
||
job := testfactory.Job(ctx, t, bundle.exec, &testfactory.JobOpts{ | ||
State: ptrutil.Ptr(JobStateRunning), | ||
}) | ||
|
||
completedJob, err := JobCompleteTx[*riverpgxv5.Driver](ctx, bundle.tx, &Job[JobArgs]{JobRow: job}) | ||
require.NoError(t, err) | ||
require.Equal(t, JobStateCompleted, completedJob.State) | ||
require.WithinDuration(t, time.Now(), *completedJob.FinalizedAt, 2*time.Second) | ||
|
||
updatedJob, err := bundle.exec.JobGetByID(ctx, job.ID) | ||
require.NoError(t, err) | ||
require.Equal(t, JobStateCompleted, updatedJob.State) | ||
}) | ||
|
||
t.Run("ErrorIfNotRunning", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
bundle := setup(t) | ||
|
||
job := testfactory.Job(ctx, t, bundle.exec, &testfactory.JobOpts{}) | ||
|
||
_, err := JobCompleteTx[*riverpgxv5.Driver](ctx, bundle.tx, &Job[JobArgs]{JobRow: job}) | ||
require.EqualError(t, err, "job must be running") | ||
}) | ||
} |