Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/guregu/null"
"github.com/jmoiron/sqlx"
"github.com/joomcode/errorx"
"github.com/lib/pq"
)

func getNextJob(tx *sqlx.Tx, queueNames []string) (*Job, error) {
Expand Down Expand Up @@ -40,6 +41,19 @@ func getNextJob(tx *sqlx.Tx, queueNames []string) (*Job, error) {
}
}

func count(execer DB, queueNames []string) (int64, error) {
var count int64
if err := execer.QueryRow(`
SELECT count(*) FROM pgq_jobs
WHERE
queue_name = ANY($1)
AND run_after < $2
AND ran_at IS NULL;`, pq.Array(queueNames), time.Now()).Scan(&count); err != nil {
return -1, errorx.Decorate(err, "could not count jobs")
}
return count, nil
}

// DB is anything with the DB methods on it that we need. (like a DB or a Tx)
type DB interface {
Exec(string, ...interface{}) (sql.Result, error)
Expand Down
5 changes: 5 additions & 0 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ func (worker *Worker) Run() error {
}
}

// Count returns the number of pending jobs for the given queues
func (worker *Worker) Count(queueNames ...string) (int64, error) {
return count(worker.db, queueNames)
}

func (worker *Worker) getQueueNames() []string {
names := []string{}
now := time.Now()
Expand Down