Skip to content

Commit dc043ca

Browse files
committed
Update: Accept user-provided context.Context to build DBOSContext
1 parent 7c5b71a commit dc043ca

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

cmd/dbos/utils.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"log/slog"
@@ -74,13 +75,14 @@ func getDBURL(_ *cobra.Command) (string, error) {
7475
}
7576

7677
// createDBOSContext creates a new DBOS context with the provided database URL
77-
func createDBOSContext(dbURL string) (dbos.DBOSContext, error) {
78+
func createDBOSContext(dbURL string, userContext context.Context) (dbos.DBOSContext, error) {
7879
appName := "dbos-cli"
7980

8081
ctx, err := dbos.NewDBOSContext(dbos.Config{
8182
DatabaseURL: dbURL,
8283
AppName: appName,
8384
Logger: initLogger(slog.LevelError),
85+
Context: userContext,
8486
})
8587
if err != nil {
8688
return nil, fmt.Errorf("failed to create DBOS context: %w", err)

dbos/dbos.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ const (
3232
// Config holds configuration parameters for initializing a DBOS context.
3333
// DatabaseURL and AppName are required.
3434
type Config struct {
35-
DatabaseURL string // PostgreSQL connection string (required)
36-
AppName string // Application name for identification (required)
37-
Logger *slog.Logger // Custom logger instance (defaults to a new slog logger)
38-
AdminServer bool // Enable Transact admin HTTP server (disabled by default)
39-
AdminServerPort int // Port for the admin HTTP server (default: 3001)
40-
ConductorURL string // DBOS conductor service URL (optional)
41-
ConductorAPIKey string // DBOS conductor API key (optional)
42-
ApplicationVersion string // Application version (optional, overridden by DBOS__APPVERSION env var)
43-
ExecutorID string // Executor ID (optional, overridden by DBOS__VMID env var)
35+
DatabaseURL string // PostgreSQL connection string (required)
36+
AppName string // Application name for identification (required)
37+
Logger *slog.Logger // Custom logger instance (defaults to a new slog logger)
38+
AdminServer bool // Enable Transact admin HTTP server (disabled by default)
39+
AdminServerPort int // Port for the admin HTTP server (default: 3001)
40+
ConductorURL string // DBOS conductor service URL (optional)
41+
ConductorAPIKey string // DBOS conductor API key (optional)
42+
ApplicationVersion string // Application version (optional, overridden by DBOS__APPVERSION env var)
43+
ExecutorID string // Executor ID (optional, overridden by DBOS__VMID env var)
44+
Context context.Context // User Context
4445
}
4546

4647
// processConfig enforces mandatory fields and applies defaults.
@@ -294,7 +295,13 @@ func (c *dbosContext) GetApplicationID() string {
294295
// log.Fatal(err)
295296
// }
296297
func NewDBOSContext(inputConfig Config) (DBOSContext, error) {
297-
ctx, cancelFunc := context.WithCancelCause(context.Background())
298+
var baseCtx context.Context
299+
if inputConfig.Context != nil {
300+
baseCtx = inputConfig.Context
301+
} else {
302+
baseCtx = context.Background()
303+
}
304+
ctx, cancelFunc := context.WithCancelCause(baseCtx)
298305
initExecutor := &dbosContext{
299306
workflowsWg: &sync.WaitGroup{},
300307
ctx: ctx,

0 commit comments

Comments
 (0)