-
Notifications
You must be signed in to change notification settings - Fork 0
CROSSLINK-287 Add scheduler #596
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
Open
JanisSaldabols
wants to merge
6
commits into
main
Choose a base branch
from
CROSSLINK-287
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6988c62
CROSSLINK-287 Add scheduler
JanisSaldabols fc6e039
CROSSLINK-287 Fix copilot comments
JanisSaldabols f011eb0
CROSSLINK-287 Work on PR comments
JanisSaldabols f6e2bf5
CROSSLINK-287 Fix test
JanisSaldabols 898281e
CROSSLINK-287 Fix copilot comments
JanisSaldabols fdbadc6
CROSSLINK-287 Check nil before trying closing connection
JanisSaldabols File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,2 @@ | ||
| DROP INDEX IF EXISTS idx_scheduled_task_run_at; | ||
| DROP TABLE IF EXISTS scheduled_task; |
This file contains hidden or 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,14 @@ | ||
| CREATE TABLE scheduled_task | ||
| ( | ||
| id TEXT PRIMARY KEY, | ||
| event_name TEXT NOT NULL, | ||
| cron_expr TEXT NOT NULL, | ||
| payload JSONB, | ||
| run_at TIMESTAMPTZ, | ||
| status TEXT NOT NULL DEFAULT 'pending', | ||
| created_at TIMESTAMPTZ NOT NULL DEFAULT now(), | ||
| updated_at TIMESTAMPTZ, | ||
| FOREIGN KEY (event_name) REFERENCES event_config (event_name) | ||
| ); | ||
|
|
||
| CREATE INDEX idx_scheduled_task_run_at ON scheduled_task (run_at) WHERE status = 'pending' AND run_at IS NOT NULL; |
This file contains hidden or 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,9 @@ | ||
| package sched_db | ||
|
|
||
| type ScheduledTaskStatus string | ||
|
|
||
| const ( | ||
| ScheduledTaskStatusPending ScheduledTaskStatus = "pending" | ||
| ScheduledTaskStatusRunning ScheduledTaskStatus = "running" | ||
| ScheduledTaskStatusStopped ScheduledTaskStatus = "stopped" | ||
| ) |
This file contains hidden or 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,86 @@ | ||
| package sched_db | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/indexdata/crosslink/broker/common" | ||
| "github.com/indexdata/crosslink/broker/repo" | ||
| "github.com/jackc/pgx/v5/pgtype" | ||
| "github.com/jackc/pgx/v5/pgxpool" | ||
| ) | ||
|
|
||
| const SchedulerChannel = "crosslink_sched_channel" | ||
|
|
||
| type SchedRepo interface { | ||
| repo.Transactional[SchedRepo] | ||
| SaveScheduledTask(ctx common.ExtendedContext, params SaveScheduledTaskParams) (ScheduledTask, error) | ||
| ClaimNextScheduledTask(ctx common.ExtendedContext) (ScheduledTask, error) | ||
| GetNextRunAt(ctx common.ExtendedContext) (pgtype.Timestamptz, error) | ||
| GetStuckRunningTasks(ctx common.ExtendedContext, stuckAfter time.Duration) ([]ScheduledTask, error) | ||
| } | ||
|
|
||
| type PgSchedRepo struct { | ||
| repo.PgBaseRepo[SchedRepo] | ||
| queries Queries | ||
| } | ||
|
|
||
| // WithTxFunc delegates transaction handling to PgBaseRepo. | ||
| func (r *PgSchedRepo) WithTxFunc(ctx common.ExtendedContext, fn func(SchedRepo) error) error { | ||
| return r.PgBaseRepo.WithTxFunc(ctx, r, fn) | ||
| } | ||
|
|
||
| // CreateWithPgBaseRepo creates a derived repo bound to the provided tx-aware base. | ||
| func (r *PgSchedRepo) CreateWithPgBaseRepo(base *repo.PgBaseRepo[SchedRepo]) SchedRepo { | ||
| derived := new(PgSchedRepo) | ||
| derived.PgBaseRepo = *base | ||
| return derived | ||
| } | ||
|
|
||
| // CreateSkdRepo creates a new SchedRepo backed by the given connection pool. | ||
| func CreateSkdRepo(dbPool *pgxpool.Pool) SchedRepo { | ||
| r := new(PgSchedRepo) | ||
| r.Pool = dbPool | ||
| return r | ||
| } | ||
|
|
||
|
Comment on lines
+39
to
+45
|
||
| func (r *PgSchedRepo) SaveScheduledTask(ctx common.ExtendedContext, params SaveScheduledTaskParams) (ScheduledTask, error) { | ||
| row, err := r.queries.SaveScheduledTask(ctx, r.GetConnOrTx(), params) | ||
| if err == nil { | ||
| r.notify(ctx) | ||
| } | ||
| return row.ScheduledTask, err | ||
| } | ||
|
|
||
| func (r *PgSchedRepo) ClaimNextScheduledTask(ctx common.ExtendedContext) (ScheduledTask, error) { | ||
| row, err := r.queries.ClaimNextScheduledTask(ctx, r.GetConnOrTx()) | ||
| return row.ScheduledTask, err | ||
| } | ||
|
|
||
| func (r *PgSchedRepo) GetNextRunAt(ctx common.ExtendedContext) (pgtype.Timestamptz, error) { | ||
| return r.queries.GetNextRunAt(ctx, r.GetConnOrTx()) | ||
| } | ||
|
|
||
| // GetStuckRunningTasks returns tasks that have been in 'running' state for | ||
| // longer than stuckAfter, indicating they were claimed but never completed. | ||
| func (r *PgSchedRepo) GetStuckRunningTasks(ctx common.ExtendedContext, stuckAfter time.Duration) ([]ScheduledTask, error) { | ||
| rows, err := r.queries.GetStuckRunningTasks(ctx, r.GetConnOrTx(), pgDuration(stuckAfter)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| tasks := make([]ScheduledTask, 0, len(rows)) | ||
| for _, row := range rows { | ||
| tasks = append(tasks, row.ScheduledTask) | ||
| } | ||
| return tasks, nil | ||
| } | ||
|
|
||
| func pgDuration(d time.Duration) pgtype.Interval { | ||
| return pgtype.Interval{Microseconds: d.Microseconds(), Valid: true} | ||
| } | ||
|
|
||
| func (r *PgSchedRepo) notify(ctx common.ExtendedContext) { | ||
| _, err := r.GetConnOrTx().Exec(ctx, "NOTIFY "+SchedulerChannel) | ||
| if err != nil { | ||
| ctx.Logger().Error("failed to notify scheduler channel", "channel", SchedulerChannel, "error", err) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.