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

implement job listing #117

Merged
merged 5 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
PR feedback refactoring
  • Loading branch information
bgentry committed Jan 14, 2024
commit 2a198f5d9f87ae49821c7d8ff04474b39fdc01e7
6 changes: 3 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1445,17 +1445,17 @@ func Test_Client_JobList(t *testing.T) {
jobRow3 := dbsqlc.JobRowFromInternal(job3)
jobRow5 := dbsqlc.JobRowFromInternal(job5)

jobs, err := client.JobList(ctx, NewJobListParams().After(JobListPaginationCursorFromJob(jobRow1)))
jobs, err := client.JobList(ctx, NewJobListParams().After(JobListCursorFromJob(jobRow1)))
require.NoError(t, err)
require.Len(t, jobs, 1)
require.Equal(t, job2.ID, jobs[0].ID)

jobs, err = client.JobList(ctx, NewJobListParams().State(rivertype.JobStateRunning).After(JobListPaginationCursorFromJob(jobRow3)))
jobs, err = client.JobList(ctx, NewJobListParams().State(rivertype.JobStateRunning).After(JobListCursorFromJob(jobRow3)))
require.NoError(t, err)
require.Len(t, jobs, 1)
require.Equal(t, job4.ID, jobs[0].ID)

jobs, err = client.JobList(ctx, NewJobListParams().State(rivertype.JobStateCompleted).After(JobListPaginationCursorFromJob(jobRow5)))
jobs, err = client.JobList(ctx, NewJobListParams().State(rivertype.JobStateCompleted).After(JobListCursorFromJob(jobRow5)))
require.NoError(t, err)
require.Len(t, jobs, 1)
require.Equal(t, job6.ID, jobs[0].ID)
Expand Down
14 changes: 8 additions & 6 deletions internal/dbsqlc/job_list.go → internal/db/job_list.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dbsqlc
package db
bgentry marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
Expand All @@ -7,6 +7,8 @@ import (
"strings"

"github.com/jackc/pgx/v5"

"github.com/riverqueue/river/internal/dbsqlc"
)

const jobList = `-- name: JobList :many
Expand Down Expand Up @@ -35,15 +37,15 @@ type JobListOrderBy struct {
}

type JobListParams struct {
State JobState
State dbsqlc.JobState
Priorities []int16
Conditions string
OrderBy []JobListOrderBy
NamedArgs map[string]any
LimitCount int32
}

func (q *Queries) JobList(ctx context.Context, db DBTX, arg JobListParams) ([]*RiverJob, error) {
func JobList(ctx context.Context, tx pgx.Tx, arg JobListParams) ([]*dbsqlc.RiverJob, error) {
namedArgs := make(pgx.NamedArgs)
for k, v := range arg.NamedArgs {
namedArgs[k] = v
Expand Down Expand Up @@ -81,14 +83,14 @@ func (q *Queries) JobList(ctx context.Context, db DBTX, arg JobListParams) ([]*R
}

query := fmt.Sprintf(jobList, conditions, orderByBuilder.String())
rows, err := db.Query(ctx, query, namedArgs)
rows, err := tx.Query(ctx, query, namedArgs)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*RiverJob
var items []*dbsqlc.RiverJob
for rows.Next() {
var i RiverJob
var i dbsqlc.RiverJob
if err := rows.Scan(
&i.ID,
&i.Args,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dbsqlc
package db

import (
"context"
Expand All @@ -7,6 +7,7 @@ import (
"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/require"

"github.com/riverqueue/river/internal/dbsqlc"
"github.com/riverqueue/river/internal/riverinternaltest"
)

Expand All @@ -18,10 +19,9 @@ func TestJobList(t *testing.T) {

ctx := context.Background()
tx := riverinternaltest.TestTx(ctx, t)
queries := New()

_, err := queries.JobList(ctx, tx, JobListParams{
State: JobStateCompleted,
_, err := JobList(ctx, tx, JobListParams{
State: dbsqlc.JobStateCompleted,
LimitCount: 1,
OrderBy: []JobListOrderBy{{Expr: "id", Order: SortOrderAsc}},
})
Expand All @@ -33,12 +33,11 @@ func TestJobList(t *testing.T) {

ctx := context.Background()
tx := riverinternaltest.TestTx(ctx, t)
queries := New()

_, err := queries.JobList(ctx, tx, JobListParams{
_, err := JobList(ctx, tx, JobListParams{
Conditions: "queue = 'test' AND priority = 1 AND args->>'foo' = @foo",
NamedArgs: pgx.NamedArgs{"foo": "bar"},
State: JobStateCompleted,
State: dbsqlc.JobStateCompleted,
LimitCount: 1,
OrderBy: []JobListOrderBy{{Expr: "id", Order: SortOrderAsc}},
})
Expand Down
9 changes: 5 additions & 4 deletions internal/dbadapter/db_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/jackc/pgx/v5"

"github.com/riverqueue/river/internal/baseservice"
"github.com/riverqueue/river/internal/db"
"github.com/riverqueue/river/internal/dbsqlc"
"github.com/riverqueue/river/internal/notifier"
"github.com/riverqueue/river/internal/util/dbutil"
Expand Down Expand Up @@ -441,11 +442,11 @@ func (a *StandardAdapter) JobListTx(ctx context.Context, tx pgx.Tx, params JobLi

var conditionsBuilder strings.Builder

orderBy := make([]dbsqlc.JobListOrderBy, len(params.OrderBy))
orderBy := make([]db.JobListOrderBy, len(params.OrderBy))
for i, o := range params.OrderBy {
orderBy[i] = dbsqlc.JobListOrderBy{
orderBy[i] = db.JobListOrderBy{
Expr: o.Expr,
Order: dbsqlc.SortOrder(o.Order),
Order: db.SortOrder(o.Order),
}
}

Expand All @@ -466,7 +467,7 @@ func (a *StandardAdapter) JobListTx(ctx context.Context, tx pgx.Tx, params JobLi
conditionsBuilder.WriteString(params.Conditions)
}

jobs, err := a.queries.JobList(ctx, tx, dbsqlc.JobListParams{
jobs, err := db.JobList(ctx, tx, db.JobListParams{
Conditions: conditionsBuilder.String(),
LimitCount: params.LimitCount,
NamedArgs: namedArgs,
Expand Down
20 changes: 10 additions & 10 deletions job_list_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import (
"github.com/riverqueue/river/rivertype"
)

// JobListPaginationCursor is used to specify a starting point for a paginated
// JobListCursor is used to specify a starting point for a paginated
// job list query.
type JobListPaginationCursor struct {
type JobListCursor struct {
id int64
kind string
queue string
time time.Time
}

// JobListPaginationCursorFromJob creates a JobListPaginationCursor from a JobRow.
func JobListPaginationCursorFromJob(job *rivertype.JobRow) *JobListPaginationCursor {
return &JobListPaginationCursor{
// JobListCursorFromJob creates a JobListCursor from a JobRow.
func JobListCursorFromJob(job *rivertype.JobRow) *JobListCursor {
return &JobListCursor{
id: job.ID,
kind: job.Kind,
queue: job.Queue,
Expand All @@ -33,7 +33,7 @@ func JobListPaginationCursorFromJob(job *rivertype.JobRow) *JobListPaginationCur

// UnmarshalText implements encoding.TextUnmarshaler to decode the cursor from
// a previously marshaled string.
func (c *JobListPaginationCursor) UnmarshalText(text []byte) error {
func (c *JobListCursor) UnmarshalText(text []byte) error {
dst := make([]byte, base64.StdEncoding.DecodedLen(len(text)))
n, err := base64.StdEncoding.Decode(dst, text)
if err != nil {
Expand All @@ -45,7 +45,7 @@ func (c *JobListPaginationCursor) UnmarshalText(text []byte) error {
if err := json.Unmarshal(dst, &wrapperValue); err != nil {
return err
}
*c = JobListPaginationCursor{
*c = JobListCursor{
id: wrapperValue.ID,
kind: wrapperValue.Kind,
queue: wrapperValue.Queue,
Expand All @@ -56,7 +56,7 @@ func (c *JobListPaginationCursor) UnmarshalText(text []byte) error {

// MarshalText implements encoding.TextMarshaler to encode the cursor as an
// opaque string.
func (c JobListPaginationCursor) MarshalText() ([]byte, error) {
func (c JobListCursor) MarshalText() ([]byte, error) {
wrapperValue := jobListPaginationCursorJSON{
ID: c.id,
Kind: c.kind,
Expand Down Expand Up @@ -104,7 +104,7 @@ const (
//
// params := NewJobListParams().OrderBy(JobListOrderByTime, SortOrderAsc).First(100)
type JobListParams struct {
after *JobListPaginationCursor
after *JobListCursor
paginationCount int32
queues []string
sortField JobListOrderByField
Expand Down Expand Up @@ -188,7 +188,7 @@ func (p *JobListParams) toDBParams() (*dbadapter.JobListParams, error) {

// After returns an updated filter set that will only return jobs
// after the given cursor.
func (p *JobListParams) After(cursor *JobListPaginationCursor) *JobListParams {
func (p *JobListParams) After(cursor *JobListCursor) *JobListParams {
result := p.copy()
result.after = cursor
return result
Expand Down
16 changes: 8 additions & 8 deletions job_list_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/riverqueue/river/rivertype"
)

func Test_JobListPaginationCursor_JobListPaginationCursorFromJob(t *testing.T) {
func Test_JobListCursor_JobListCursorFromJob(t *testing.T) {
t.Parallel()

for i, state := range []rivertype.JobState{
Expand All @@ -35,7 +35,7 @@ func Test_JobListPaginationCursor_JobListPaginationCursorFromJob(t *testing.T) {
ScheduledAt: now.Add(-10 * time.Second),
}

cursor := JobListPaginationCursorFromJob(jobRow)
cursor := JobListCursorFromJob(jobRow)
require.Equal(t, jobRow.ID, cursor.id)
require.Equal(t, jobRow.Kind, cursor.kind)
require.Equal(t, jobRow.Queue, cursor.queue)
Expand Down Expand Up @@ -65,7 +65,7 @@ func Test_JobListPaginationCursor_JobListPaginationCursorFromJob(t *testing.T) {
ScheduledAt: now.Add(-10 * time.Second),
}

cursor := JobListPaginationCursorFromJob(jobRow)
cursor := JobListCursorFromJob(jobRow)
require.Equal(t, jobRow.ID, cursor.id)
require.Equal(t, jobRow.Kind, cursor.kind)
require.Equal(t, jobRow.Queue, cursor.queue)
Expand All @@ -87,7 +87,7 @@ func Test_JobListPaginationCursor_JobListPaginationCursorFromJob(t *testing.T) {
ScheduledAt: now.Add(-10 * time.Second),
}

cursor := JobListPaginationCursorFromJob(jobRow)
cursor := JobListCursorFromJob(jobRow)
require.Equal(t, jobRow.ID, cursor.id)
require.Equal(t, jobRow.Kind, cursor.kind)
require.Equal(t, jobRow.Queue, cursor.queue)
Expand All @@ -107,22 +107,22 @@ func Test_JobListPaginationCursor_JobListPaginationCursorFromJob(t *testing.T) {
ScheduledAt: now.Add(-10 * time.Second),
}

cursor := JobListPaginationCursorFromJob(jobRow)
cursor := JobListCursorFromJob(jobRow)
require.Equal(t, jobRow.ID, cursor.id)
require.Equal(t, jobRow.Kind, cursor.kind)
require.Equal(t, jobRow.Queue, cursor.queue)
require.Equal(t, jobRow.CreatedAt, cursor.time)
})
}

func Test_JobListPaginationCursor_MarshalJSON(t *testing.T) {
func Test_JobListCursor_MarshalJSON(t *testing.T) {
t.Parallel()

t.Run("CanMarshalAndUnmarshal", func(t *testing.T) {
t.Parallel()

now := time.Now().UTC()
params := &JobListPaginationCursor{
params := &JobListCursor{
id: 4,
kind: "test_kind",
queue: "test_queue",
Expand All @@ -133,7 +133,7 @@ func Test_JobListPaginationCursor_MarshalJSON(t *testing.T) {
require.NoError(t, err)
require.NotEqual(t, "", text)

unmarshaledParams := &JobListPaginationCursor{}
unmarshaledParams := &JobListCursor{}
require.NoError(t, json.Unmarshal(text, unmarshaledParams))

require.Equal(t, params, unmarshaledParams)
Expand Down