Skip to content

Commit

Permalink
added stream job delete capability
Browse files Browse the repository at this point in the history
  • Loading branch information
msuchacz-cll committed Dec 13, 2024
1 parent 6a0f4d5 commit aac3327
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
12 changes: 12 additions & 0 deletions core/services/job/job_orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,18 @@ func TestORM_DeleteJob_DeletesAssociatedRecords(t *testing.T) {
cltest.AssertCount(t, db, "jobs", 0)
})

t.Run("it creates and deletes records for stream jobs", func(t *testing.T) {
ctx := testutils.Context(t)
jb, err := testspecs.GenerateStreamSpecJob(testspecs.GenerateStreamSpec(testspecs.StreamSpecParams{Name: "Test-stream", StreamID: 1}))
require.NoError(t, err)
err = jobORM.CreateJob(ctx, &jb)
require.NoError(t, err)
cltest.AssertCount(t, db, "jobs", 1)
err = jobORM.DeleteJob(ctx, jb.ID, jb.Type)
require.NoError(t, err)
cltest.AssertCount(t, db, "jobs", 0)
})

t.Run("does not allow to delete external initiators if they have referencing external_initiator_webhook_specs", func(t *testing.T) {
// create new db because this will rollback transaction and poison it
db := pgtest.NewSqlxDB(t)
Expand Down
19 changes: 12 additions & 7 deletions core/services/job/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ func (o *orm) DeleteJob(ctx context.Context, id int32, jobType Type) error {
Workflow: `DELETE FROM workflow_specs WHERE id in (SELECT workflow_spec_id FROM deleted_jobs)`,
StandardCapabilities: `DELETE FROM standardcapabilities_specs WHERE id in (SELECT standard_capabilities_spec_id FROM deleted_jobs)`,
CCIP: `DELETE FROM ccip_specs WHERE id in (SELECT ccip_spec_id FROM deleted_jobs)`,
Stream: ``,
}
q, ok := queries[jobType]
if !ok {
Expand All @@ -757,7 +758,7 @@ func (o *orm) DeleteJob(ctx context.Context, id int32, jobType Type) error {
// and this query was taking ~40secs.
ctx, cancel := context.WithTimeout(sqlutil.WithoutDefaultTimeout(ctx), time.Minute)
defer cancel()
query := fmt.Sprintf(`
query := `
WITH deleted_jobs AS (
DELETE FROM jobs WHERE id = $1 RETURNING
id,
Expand All @@ -775,15 +776,19 @@ func (o *orm) DeleteJob(ctx context.Context, id int32, jobType Type) error {
gateway_spec_id,
workflow_spec_id,
standard_capabilities_spec_id,
ccip_spec_id
),
deleted_specific_specs AS (
%s
),
ccip_spec_id,
stream_id
),`
if len(q) > 0 {
query += fmt.Sprintf(`deleted_specific_specs AS (
%s
),)`, q)
}
query += `
deleted_job_pipeline_specs AS (
DELETE FROM job_pipeline_specs WHERE job_id IN (SELECT id FROM deleted_jobs) RETURNING pipeline_spec_id
)
DELETE FROM pipeline_specs WHERE id IN (SELECT pipeline_spec_id FROM deleted_job_pipeline_specs)`, q)
DELETE FROM pipeline_specs WHERE id IN (SELECT pipeline_spec_id FROM deleted_job_pipeline_specs)`
res, err := o.ds.ExecContext(ctx, query, id)
if err != nil {
return errors.Wrap(err, "DeleteJob failed to delete job")
Expand Down
7 changes: 7 additions & 0 deletions core/testdata/testspecs/v2_specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/google/uuid"
tomlTools "github.com/pelletier/go-toml"
"github.com/test-go/testify/require"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
Expand Down Expand Up @@ -883,6 +884,12 @@ ds -> ds_parse -> ds_multiply;
return StreamSpec{StreamSpecParams: params, toml: toml}
}

func GenerateStreamSpecJob(spec StreamSpec) (job.Job, error) {
jb := job.Job{ExternalJobID: uuid.New()}
err := tomlTools.Unmarshal([]byte(spec.Toml()), &jb)
return jb, err
}

// WorkflowJobSpec is a test helper that wraps both the TOML and job.Job representation of a workflow job spec
type WorkflowJobSpec struct {
toml string
Expand Down

0 comments on commit aac3327

Please sign in to comment.