Skip to content

Commit

Permalink
feat: Provided worker template
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkinox committed Jan 24, 2024
1 parent b26d7f8 commit 6f933b1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
<!-- TOC -->
* [Overview](#overview)
* [Documentation](#documentation)
* [Getting started](#getting-started)
* [Installation](#installation)
* [With gonew](#with-gonew)
* [With GitHub](#with-github)
* [Usage](#usage)
* [Template contents](#template-contents)
* [Contents](#contents)
* [Layout](#layout)
* [Makefile](#makefile)
<!-- TOC -->
Expand All @@ -25,7 +26,9 @@ This template provides:
- a ready to use [dev environment](docker-compose.yaml), based on [Air](https://github.com/cosmtrek/air) (for live reloading)
- some examples of [worker](internal/worker/example.go) and [test](internal/worker/example_test.go) to get started

See the [Yokai documentation](https://ankorstore.github.io/yokai) for more details.
## Documentation

See [Yokai documentation](https://ankorstore.github.io/yokai).

## Getting started

Expand Down Expand Up @@ -63,7 +66,7 @@ To see the [provided example worker](internal/worker/example.go) in action, simp
make logs
```

## Template contents
## Contents

### Layout

Expand All @@ -74,7 +77,7 @@ This template is following the [standard go project layout](https://github.com/g
- `internal/`:
- `worker/`: worker and test examples
- `bootstrap.go`: bootstrap (modules, lifecycles, etc)
- `services.go`: services definition
- `services.go`: services registration

### Makefile

Expand Down
12 changes: 8 additions & 4 deletions internal/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@ import (
"go.uber.org/fx"
)

func init() {
RootDir = fxcore.RootDir(1)
}

// RootDir is the application root directory.
var RootDir string

// Bootstrapper can be used to load modules, options, services and bootstraps your application.
var Bootstrapper = fxcore.NewBootstrapper().WithOptions(
// modules
fxworker.FxWorkerModule,
// services
ProvideServices(),
)

func init() {
RootDir = fxcore.RootDir(1)
}

// Run starts the application, with a provided [context.Context].
func Run(ctx context.Context) {
Bootstrapper.WithContext(ctx).RunApp()
}

// RunTest starts the application in test mode, with an optional list of [fx.Option].
func RunTest(tb testing.TB, options ...fx.Option) {
tb.Helper()

Expand Down
1 change: 1 addition & 0 deletions internal/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go.uber.org/fx"
)

// ProvideServices is used to register the application services.
func ProvideServices() fx.Option {
return fx.Options(
fxworker.AsWorker(worker.NewExampleWorker),
Expand Down
5 changes: 5 additions & 0 deletions internal/worker/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@ import (
"github.com/ankorstore/yokai/worker"
)

// ExampleWorker is an example worker.
type ExampleWorker struct {
config *config.Config
}

// NewExampleWorker returns a new [ExampleWorker].
func NewExampleWorker(config *config.Config) *ExampleWorker {
return &ExampleWorker{
config: config,
}
}

// Name returns the [ExampleWorker] name.
func (w *ExampleWorker) Name() string {
return "example-worker"
}

// Run executes the [ExampleWorker].
func (w *ExampleWorker) Run(ctx context.Context) error {
logger := worker.CtxLogger(ctx)

Expand All @@ -34,6 +38,7 @@ func (w *ExampleWorker) Run(ctx context.Context) error {
default:
logger.Info().Msg("running")

// The sleep interval can be configured in the application config files.
time.Sleep(time.Duration(w.config.GetFloat64("config.example-worker.interval")) * time.Second)
}
}
Expand Down
1 change: 0 additions & 1 deletion internal/worker/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func TestExampleWorker(t *testing.T) {
var logBuffer logtest.TestLogBuffer
var metricsRegistry *prometheus.Registry

// test app
internal.RunTest(
t,
fx.Populate(
Expand Down

0 comments on commit 6f933b1

Please sign in to comment.