generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: timeline events for scheduled cron jobs (#2860)
Adds an event to the timeline when a cron job is scheduled. No other cron events are needed since the rest will be covered by async events. <img width="1222" alt="Screenshot 2024-10-02 at 10 51 45 AM" src="https://github.com/user-attachments/assets/3f62f6c2-5429-4c56-9e9f-71b88570e9b9"> --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
74c415b
commit e534f24
Showing
24 changed files
with
1,132 additions
and
533 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
6 changes: 6 additions & 0 deletions
6
backend/controller/sql/schema/20240926231955_timeline_async_call_event_types.sql
This file contains 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,6 @@ | ||
-- migrate:up | ||
|
||
ALTER TYPE event_type ADD VALUE IF NOT EXISTS 'cron_scheduled'; | ||
|
||
-- migrate:down | ||
|
This file contains 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,76 @@ | ||
package timeline | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/alecthomas/types/optional" | ||
|
||
ftlencryption "github.com/TBD54566975/ftl/backend/controller/encryption/api" | ||
"github.com/TBD54566975/ftl/backend/controller/timeline/internal/sql" | ||
"github.com/TBD54566975/ftl/backend/libdal" | ||
"github.com/TBD54566975/ftl/internal/model" | ||
"github.com/TBD54566975/ftl/internal/schema" | ||
) | ||
|
||
type CronScheduledEvent struct { | ||
ID int64 | ||
Duration time.Duration | ||
CronScheduled | ||
} | ||
|
||
func (e *CronScheduledEvent) GetID() int64 { return e.ID } | ||
func (e *CronScheduledEvent) event() {} | ||
|
||
type CronScheduled struct { | ||
DeploymentKey model.DeploymentKey | ||
Verb schema.Ref | ||
|
||
Time time.Time | ||
ScheduledAt time.Time | ||
Schedule string | ||
Error optional.Option[string] | ||
} | ||
|
||
func (*CronScheduled) inEvent() {} | ||
|
||
type eventCronScheduledJSON struct { | ||
DurationMS int64 `json:"duration_ms"` | ||
ScheduledAt time.Time `json:"scheduled_at"` | ||
Schedule string `json:"schedule"` | ||
Error optional.Option[string] `json:"error,omitempty"` | ||
} | ||
|
||
func (s *Service) insertCronScheduledEvent(ctx context.Context, querier sql.Querier, event *CronScheduled) error { | ||
cronJSON := eventCronScheduledJSON{ | ||
DurationMS: time.Since(event.Time).Milliseconds(), | ||
ScheduledAt: event.ScheduledAt, | ||
Schedule: event.Schedule, | ||
Error: event.Error, | ||
} | ||
|
||
data, err := json.Marshal(cronJSON) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal cron JSON: %w", err) | ||
} | ||
|
||
var payload ftlencryption.EncryptedTimelineColumn | ||
err = s.encryption.EncryptJSON(json.RawMessage(data), &payload) | ||
if err != nil { | ||
return fmt.Errorf("failed to encrypt cron JSON: %w", err) | ||
} | ||
|
||
err = libdal.TranslatePGError(querier.InsertTimelineCronScheduledEvent(ctx, sql.InsertTimelineCronScheduledEventParams{ | ||
DeploymentKey: event.DeploymentKey, | ||
TimeStamp: event.Time, | ||
Module: event.Verb.Module, | ||
Verb: event.Verb.Name, | ||
Payload: payload, | ||
})) | ||
if err != nil { | ||
return fmt.Errorf("failed to insert cron event: %w", err) | ||
} | ||
return err | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Oops, something went wrong.