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: support async calls in the database layer (#1371)
The initial use case is for FSM support, but this will be the framework we use for receiving PubSub events, callbacks, possibly cron jobs, etc. Basically any form of asynchronous call.
- Loading branch information
1 parent
5990462
commit fdd90eb
Showing
15 changed files
with
506 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package dal | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/TBD54566975/ftl/backend/controller/sql" | ||
"github.com/TBD54566975/ftl/backend/schema" | ||
) | ||
|
||
// SendFSMEvent sends an event to an executing instance of an FSM. | ||
// | ||
// If the instance doesn't exist a new one will be created. | ||
// | ||
// [name] is the name of the state machine to execute, [executionKey] is the | ||
// unique identifier for this execution of the FSM. | ||
// | ||
// Returns ErrConflict if the state machine is already executing. | ||
// | ||
// Note: this does not actually call the FSM, it just enqueues an async call for | ||
// future execution. | ||
// | ||
// Note: no validation of the FSM is performed. | ||
func (d *DAL) SendFSMEvent(ctx context.Context, name, executionKey, destinationState string, verb schema.Ref, request json.RawMessage) error { | ||
_, err := d.db.SendFSMEvent(ctx, sql.SendFSMEventParams{ | ||
Key: executionKey, | ||
Name: name, | ||
State: destinationState, | ||
Verb: verb.String(), | ||
Request: request, | ||
}) | ||
return translatePGError(err) | ||
} | ||
|
||
// AcquireAsyncCall acquires a pending async call to execute. | ||
// | ||
// Returns ErrNotFound if there are no async calls to acquire. | ||
func (d *DAL) AcquireAsyncCall(ctx context.Context) (*Lease, error) { | ||
ttl := time.Second * 5 | ||
row, err := d.db.AcquireAsyncCall(ctx, ttl) | ||
if err != nil { | ||
err = translatePGError(err) | ||
// We get a NULL constraint violation if there are no async calls to acquire, so translate it to ErrNotFound. | ||
if errors.Is(err, ErrConstraint) { | ||
return nil, fmt.Errorf("no pending async calls: %w", ErrNotFound) | ||
} | ||
return nil, err | ||
} | ||
return d.newLease(ctx, row.LeaseKey, row.LeaseIdempotencyKey, ttl), nil | ||
} |
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,34 @@ | ||
package dal | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
|
||
"github.com/TBD54566975/ftl/backend/controller/sql/sqltest" | ||
"github.com/TBD54566975/ftl/backend/schema" | ||
"github.com/TBD54566975/ftl/internal/log" | ||
) | ||
|
||
func TestSendFSMEvent(t *testing.T) { | ||
ctx := log.ContextWithNewDefaultLogger(context.Background()) | ||
conn := sqltest.OpenForTesting(ctx, t) | ||
dal, err := New(ctx, conn) | ||
assert.NoError(t, err) | ||
|
||
_, err = dal.AcquireAsyncCall(ctx) | ||
assert.IsError(t, err, ErrNotFound) | ||
|
||
err = dal.SendFSMEvent(ctx, "test", "test", "state", schema.Ref{Module: "module", Name: "verb"}, []byte(`{}`)) | ||
assert.NoError(t, err) | ||
|
||
lease, err := dal.AcquireAsyncCall(ctx) | ||
assert.NoError(t, err) | ||
t.Cleanup(func() { | ||
err := lease.Release() | ||
assert.NoError(t, err) | ||
}) | ||
|
||
assert.HasPrefix(t, lease.String(), "/system/async_call/1:") | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.