Skip to content

Commit

Permalink
carve off async_calls for cron and future dependency, use sqlc.embed …
Browse files Browse the repository at this point in the history
…to dedupe code
  • Loading branch information
safeer committed Aug 19, 2024
1 parent fb999da commit c196bb6
Show file tree
Hide file tree
Showing 15 changed files with 355 additions and 3,082 deletions.
2 changes: 1 addition & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ init-db:

# Regenerate SQLC code (requires init-db to be run first)
build-sqlc:
@mk backend/controller/sql/{db.go,models.go,querier.go,queries.sql.go} backend/controller/cronjobs/sql/{db.go,models.go,querier.go,queries.sql.go} common/configuration/sql/{db.go,models.go,querier.go,queries.sql.go} : backend/controller/sql/queries.sql backend/controller/cronjobs/sql/queries.sql common/configuration/sql/queries.sql backend/controller/sql/schema sqlc.yaml -- "just init-db && sqlc generate"
@mk backend/controller/sql/{db.go,models.go,querier.go,queries.sql.go} backend/controller/cronjobs/sql/{db.go,models.go,querier.go,queries.sql.go} common/configuration/sql/{db.go,models.go,querier.go,queries.sql.go} : backend/controller/sql/queries.sql backend/controller/sql/async_queries.sql backend/controller/cronjobs/sql/queries.sql common/configuration/sql/queries.sql backend/controller/sql/schema sqlc.yaml -- "just init-db && sqlc generate"

# Build the ZIP files that are embedded in the FTL release binaries
build-zips: build-kt-runtime
Expand Down
34 changes: 12 additions & 22 deletions backend/controller/cronjobs/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,15 @@ func (t *Tx) Rollback(ctx context.Context) error {
return nil
}

func cronJobFromGetByKeyRow(row sql.GetCronJobByKeyRow) model.CronJob {
func cronJobFromRow(c sql.CronJob, d sql.Deployment) model.CronJob {
return model.CronJob{
Key: row.Key,
DeploymentKey: row.DeploymentKey,
Verb: schema.Ref{Module: row.Module, Name: row.Verb},
Schedule: row.Schedule,
StartTime: row.StartTime,
NextExecution: row.NextExecution,
LastExecution: row.LastExecution,
}
}

func cronJobFromGetUnscheduledRow(row sql.GetUnscheduledCronJobsRow) model.CronJob {
return model.CronJob{
Key: row.Key,
DeploymentKey: row.DeploymentKey,
Verb: schema.Ref{Module: row.Module, Name: row.Verb},
Schedule: row.Schedule,
StartTime: row.StartTime,
NextExecution: row.NextExecution,
LastExecution: row.LastExecution,
Key: c.Key,
DeploymentKey: d.Key,
Verb: schema.Ref{Module: c.ModuleName, Name: c.Verb},
Schedule: c.Schedule,
StartTime: c.StartTime,
NextExecution: c.NextExecution,
LastExecution: c.LastExecution,
}
}

Expand All @@ -95,7 +83,9 @@ func (d *DAL) GetUnscheduledCronJobs(ctx context.Context, startTime time.Time) (
if err != nil {
return nil, fmt.Errorf("failed to get cron jobs: %w", dalerrs.TranslatePGError(err))
}
return slices.Map(rows, cronJobFromGetUnscheduledRow), nil
return slices.Map(rows, func(r sql.GetUnscheduledCronJobsRow) model.CronJob {
return cronJobFromRow(r.CronJob, r.Deployment)
}), nil
}

// GetCronJobByKey returns a cron job by its key
Expand All @@ -104,5 +94,5 @@ func (d *DAL) GetCronJobByKey(ctx context.Context, key model.CronJobKey) (model.
if err != nil {
return model.CronJob{}, fmt.Errorf("failed to get cron job %q: %w", key, dalerrs.TranslatePGError(err))
}
return cronJobFromGetByKeyRow(row), nil
return cronJobFromRow(row.CronJob, row.Deployment), nil
}
97 changes: 97 additions & 0 deletions backend/controller/cronjobs/sql/async_queries.sql.go

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

42 changes: 0 additions & 42 deletions backend/controller/cronjobs/sql/models.go

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

109 changes: 0 additions & 109 deletions backend/controller/cronjobs/sql/querier.go

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

13 changes: 2 additions & 11 deletions backend/controller/cronjobs/sql/queries.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- name: GetUnscheduledCronJobs :many
SELECT j.key as key, d.key as deployment_key, j.module_name as module, j.verb, j.schedule, j.start_time, j.next_execution, j.last_execution
SELECT sqlc.embed(j), sqlc.embed(d)
FROM cron_jobs j
INNER JOIN deployments d on j.deployment_id = d.id
WHERE d.min_replicas > 0
Expand All @@ -16,17 +16,8 @@ WHERE d.min_replicas > 0
)
FOR UPDATE SKIP LOCKED;

-- name: IsCronJobPending :one
SELECT EXISTS (
SELECT 1
FROM async_calls ac
WHERE ac.cron_job_key = sqlc.arg('key')::cron_job_key
AND ac.scheduled_at > sqlc.arg('start_time')::TIMESTAMPTZ
AND ac.state = 'pending'
) AS pending;

-- name: GetCronJobByKey :one
SELECT j.key as key, d.key as deployment_key, j.module_name as module, j.verb, j.schedule, j.start_time, j.next_execution, j.last_execution
SELECT sqlc.embed(j), sqlc.embed(d)
FROM cron_jobs j
INNER JOIN deployments d on j.deployment_id = d.id
WHERE j.key = sqlc.arg('key')::cron_job_key
Expand Down
Loading

0 comments on commit c196bb6

Please sign in to comment.