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
5 changes: 2 additions & 3 deletions apiclient/Valkyrie/Execute a script.bru
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ post {
body:json {
{

"code": "print('2nd minute')",
"language": "python",
"cron_expression": "*/2 * * * *"
"code": "from time import sleep\n\nfor i in range(30):\n sleep(1)\n print(\"Hello\")",
"language": "python"
}
}
2 changes: 1 addition & 1 deletion build/package/nix/odin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
buildGoModule rec {
pname = "odin";
version = "0.0.1";
vendorHash = "sha256-g+YA2d4tuAtGazjtNiIyyaWbJfnZXMeHk7e8EDr+uUw=";
vendorHash = "sha256-JE1JuOGw2mV0WRESdHw+TnsA4UzHVrtNadnoWmPwvOg=";

src = ../../..;

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/deepakdinesh1123/valkyrie
go 1.22.0

require (
github.com/adhocore/gronx v1.19.0
github.com/containers/podman/v5 v5.2.0
github.com/docker/docker v27.1.1+incompatible
github.com/exaring/otelpgx v0.6.2
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1o
github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
github.com/adhocore/gronx v1.19.0 h1:GrEvNMPDwXND+YFadCyFVQPC+/xxoGJaQzu+duNf6aU=
github.com/adhocore/gronx v1.19.0/go.mod h1:7oUY1WAU8rEJWmAxXR2DN0JaO4gi9khSgKjiRypqteg=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down
68 changes: 10 additions & 58 deletions internal/odin/db/job.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions internal/odin/db/migrations/000001_init.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ create table exec_request (

create table jobs (
id bigint primary key default nextval('jobs_id_seq'),
cron_expression text,
last_scheduled_at timestamptz default null,
next_run_at timestamptz default null,
created_at timestamptz not null default now(),
updated_at timestamptz,
exec_request_id int references exec_request on delete set null,
status TEXT NOT NULL CHECK (status IN ('pending', 'scheduled', 'completed', 'failed', 'cancelled')) DEFAULT 'pending',
retries int default 0,
max_retries int default 0
max_retries int default 5
);

create sequence job_runs_id_seq as bigint;
Expand Down
17 changes: 7 additions & 10 deletions internal/odin/db/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion internal/odin/db/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 4 additions & 17 deletions internal/odin/db/queries/job.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
create table jobs (
id bigint primary key default nextval('jobs_id_seq'),
inserted_at timestamptz not null default now(),
cron_expression text,
last_scheduled_at timestamptz default null,
next_run_at timestamptz default null,
created_at timestamptz not null default now(),
updated_at timestamptz,
exec_request_id int references exec_request on delete set null,
status TEXT NOT NULL CHECK (status IN ('pending', 'scheduled', 'completed', 'failed', 'cancelled')) DEFAULT 'pending',
retries int default 0,
max_retries int default 0
max_retries int default 5
);

create table job_runs (
Expand All @@ -28,7 +24,7 @@ where id = (
select id from jobs
where
status = 'pending'
and next_run_at <= now()
and retries < max_retries
order by
id asc
for update skip locked
Expand All @@ -38,9 +34,9 @@ returning *;

-- name: InsertJob :one
insert into jobs
(cron_expression, exec_request_id, last_scheduled_at, next_run_at, max_retries)
(exec_request_id, max_retries)
values
($1, $2, $3, $4, $5)
($1, $2)
returning *;

-- name: UpdateJobCompleted :exec
Expand All @@ -50,15 +46,6 @@ set
updated_at = now()
where id = $1 AND status = 'scheduled';

-- name: UpdateJobSchedule :exec
update jobs
set
status = 'pending',
last_scheduled_at = $2,
next_run_at = $3,
updated_at = now()
where id = $1 AND status = 'completed';

-- name: InsertJobRun :one
insert into job_runs
(job_id, worker_id, started_at, finished_at, exec_request_id, logs)
Expand Down
31 changes: 0 additions & 31 deletions internal/odin/db/tx_add_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package db

import (
"context"
"fmt"
"log"
"time"

"github.com/adhocore/gronx"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
)
Expand All @@ -18,7 +15,6 @@ type AddJobTxParams struct {
Args string
Path string
ProgrammingLanguage string
CronExpression string
MaxRetries int
}

Expand All @@ -33,10 +29,8 @@ func (s *SQLStore) AddJobTx(ctx context.Context, arg AddJobTxParams) (AddJobTxRe
execReq, err := s.GetExecRequestByHash(ctx, arg.Hash)
var execId int32
if err != nil {
log.Printf("GetExecRequestByHash error: %v", err)
switch err {
case pgx.ErrNoRows:
log.Println("InsertExecRequest")
execId, err = s.InsertExecRequest(ctx, InsertExecRequestParams{
Code: arg.Code,
Flake: arg.Flake,
Expand All @@ -58,32 +52,7 @@ func (s *SQLStore) AddJobTx(ctx context.Context, arg AddJobTxParams) (AddJobTxRe

var jobParams InsertJobParams
jobParams.ExecRequestID = pgtype.Int4{Int32: execId, Valid: true}
jobParams.LastScheduledAt = pgtype.Timestamptz{
Time: time.Now(),
Valid: true,
}
jobParams.MaxRetries = pgtype.Int4{Int32: int32(arg.MaxRetries), Valid: true}
if arg.CronExpression == "" {
jobParams.NextRunAt = pgtype.Timestamptz{
Time: time.Now(),
Valid: true,
}
} else {
gron := gronx.New()
if !gron.IsValid(arg.CronExpression) {
return fmt.Errorf("invalid cron expression: %s", arg.CronExpression)
}
jobParams.CronExpression = pgtype.Text{String: arg.CronExpression, Valid: true}
nextRunAt, err := gronx.NextTick(arg.CronExpression, true)
if err != nil {
log.Printf("NextTick error: %v", err)
return err
}
jobParams.NextRunAt = pgtype.Timestamptz{
Time: nextRunAt,
Valid: true,
}
}
job, err := s.InsertJob(ctx, jobParams)
if err != nil {
log.Printf("InsertJob error: %v", err)
Expand Down
Loading