Skip to content

Commit

Permalink
feat(fxcore): Added core tasks system
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkinox committed Mar 11, 2025
1 parent 1fe4277 commit 72be8c7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
8 changes: 4 additions & 4 deletions fxcore/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,16 @@ func withHandlers(coreServer *echo.Echo, p FxCoreParam) (*echo.Echo, error) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("cannot close request body: %v", err.Error()))
}

Check warning on line 305 in fxcore/module.go

View check run for this annotation

Codecov / codecov/patch

fxcore/module.go#L300-L305

Added lines #L300 - L305 were not covered by tests

res, err := p.TaskRegistry.Run(ctx, name, input)
if err != nil {
res := p.TaskRegistry.Run(ctx, name, input)
if res.Success == false {
logger.Error().Err(err).Str("task", name).Msg("task execution error")

return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("task execution error: %v", err.Error()))
return c.JSON(http.StatusInternalServerError, res)
}

Check warning on line 312 in fxcore/module.go

View check run for this annotation

Codecov / codecov/patch

fxcore/module.go#L307-L312

Added lines #L307 - L312 were not covered by tests

logger.Info().Str("task", name).Msg("task execution success")

return c.String(http.StatusOK, string(res))
return c.JSON(http.StatusOK, res)

Check warning on line 316 in fxcore/module.go

View check run for this annotation

Codecov / codecov/patch

fxcore/module.go#L314-L316

Added lines #L314 - L316 were not covered by tests
})

coreServer.Logger.Debug("registered tasks handler")

Check warning on line 319 in fxcore/module.go

View check run for this annotation

Codecov / codecov/patch

fxcore/module.go#L319

Added line #L319 was not covered by tests
Expand Down
15 changes: 12 additions & 3 deletions fxcore/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import (
"go.uber.org/fx"
)

type TaskResult struct {
Success bool `json:"success"`
Message string `json:"message"`
Metadata map[string]any `json:"metadata,omitempty"`
}

type Task interface {
Name() string
Run(ctx context.Context, input []byte) ([]byte, error)
Run(ctx context.Context, input []byte) TaskResult
}

type TaskRegistry struct {
Expand Down Expand Up @@ -42,10 +48,13 @@ func (r *TaskRegistry) Names() []string {
return names

Check warning on line 48 in fxcore/task.go

View check run for this annotation

Codecov / codecov/patch

fxcore/task.go#L42-L48

Added lines #L42 - L48 were not covered by tests
}

func (r *TaskRegistry) Run(ctx context.Context, name string, input []byte) ([]byte, error) {
func (r *TaskRegistry) Run(ctx context.Context, name string, input []byte) TaskResult {
task, ok := r.tasks[name]
if !ok {
return nil, fmt.Errorf("task %s not found in registry", name)
return TaskResult{
Success: false,
Message: fmt.Sprintf("task %s not found", name),
}
}

Check warning on line 58 in fxcore/task.go

View check run for this annotation

Codecov / codecov/patch

fxcore/task.go#L51-L58

Added lines #L51 - L58 were not covered by tests

return task.Run(ctx, input)

Check warning on line 60 in fxcore/task.go

View check run for this annotation

Codecov / codecov/patch

fxcore/task.go#L60

Added line #L60 was not covered by tests
Expand Down

0 comments on commit 72be8c7

Please sign in to comment.