Skip to content

Commit

Permalink
Rename to go-service
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Kaupat committed Jun 9, 2023
1 parent 18b5e26 commit 834d773
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 37 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# go Services
# go Service

A package to manage background services in go applications.

Expand Down Expand Up @@ -28,8 +28,8 @@ type Runner interface {

Service struct boilerplate. Initer is optional (see below).
```
var _ services.Runner = &MyService{}
var _ services.Initer = &MyService{}
var _ service.Runner = &MyService{}
var _ service.Initer = &MyService{}
type MyService struct {
// Whatever is needed in context of the service
Expand All @@ -54,7 +54,7 @@ func (s *MyService) Run(ctx context.Context) error {
And register them inside a container:

```
c := services.NewContainer() // or use services.Default()
c := service.NewContainer() // or use service.Default()
c.Register(s1)
```

Expand All @@ -64,9 +64,9 @@ Service names must be unique inside a single container.
There is also a builder pattern if you prefer not to implement the interface yourself:

```
c := services.NewContainer()
c := service.NewContainer()
services.New("My Service").Run(func(ctx context.Context) error {
service.New("My Service").Run(func(ctx context.Context) error {
// Implement your service here. Try to keep it running, only return fatal errors.
<-ctx.Done()
// Gracefully shut down your service here
Expand All @@ -78,8 +78,8 @@ There is also a builder pattern if you prefer not to implement the interface you
If you just want to register a single function as service you can use the following helper.

```
services.Default().Register(services.WithFunc(init, run))
services.Default().Register(services.WithRunFunc(run))
service.Default().Register(service.WithFunc(init, run))
service.Default().Register(service.WithRunFunc(run))
```

Service names are derived from the function name via reflection.
Expand Down Expand Up @@ -118,7 +118,7 @@ To change this name you can implement the `fmt.Stringer` interface.
## Service initialization

Before any `Run()` method gets called,
optional `Init()` methods from the `services.Initer` interface are executed sequentially
optional `Init()` methods from the `service.Initer` interface are executed sequentially
in oder of service registration.

```
Expand All @@ -133,7 +133,7 @@ type Initer interface {
Or use the builder:

```
services.New("My Service").
service.New("My Service").
Init(func(ctx context.Context) error {
return nil
}).
Expand Down
2 changes: 1 addition & 1 deletion builder.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package services
package service

import (
"context"
Expand Down
8 changes: 4 additions & 4 deletions builder_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package services_test
package service_test

import (
"context"
"github.com/niondir/go-services"
"github.com/niondir/go-service"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

func TestServiceBuilder(t *testing.T) {
c := services.NewContainer()
c := service.NewContainer()

initialized := false
run := false
stopped := false

services.New("My Service").
service.New("My Service").
Init(func(ctx context.Context) error {
initialized = true
return nil
Expand Down
2 changes: 1 addition & 1 deletion func.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package services
package service

import (
"context"
Expand Down
12 changes: 6 additions & 6 deletions func_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package services_test
package service_test

import (
"context"
"github.com/niondir/go-services"
"github.com/niondir/go-service"
"github.com/stretchr/testify/require"
"testing"
"time"
)

func TestWithRunFunc(t *testing.T) {
c := services.NewContainer()
c := service.NewContainer()
started := false
stopped := false

Expand All @@ -22,7 +22,7 @@ func TestWithRunFunc(t *testing.T) {
return nil
}

c.Register(services.WithRunFunc(anon))
c.Register(service.WithRunFunc(anon))

ctx, cancel := context.WithCancel(context.Background())
err := c.StartAll(ctx)
Expand All @@ -40,7 +40,7 @@ func TestWithFunc(t *testing.T) {
started := false
stopped := false

c := services.NewContainer()
c := service.NewContainer()
init := func(ctx context.Context) error {
initialized = true
t.Logf("service initialized")
Expand All @@ -55,7 +55,7 @@ func TestWithFunc(t *testing.T) {
return nil
}

c.Register(services.WithFunc(init, run))
c.Register(service.WithFunc(init, run))

ctx, cancel := context.WithCancel(context.Background())
err := c.StartAll(ctx)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/niondir/go-services
module github.com/niondir/go-service

go 1.18

Expand Down
2 changes: 1 addition & 1 deletion interface.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package services
package service

import "context"

Expand Down
2 changes: 1 addition & 1 deletion logging.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package services
package service

type Logger interface {
}
2 changes: 1 addition & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// All services have to implement the Runner interface. Run() is blocking and only returns when the service stops working.
//
// All services inside one container are started and stopped together. If one service fails, all are stopped.
package services
package service

import (
"context"
Expand Down
22 changes: 11 additions & 11 deletions service_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package services_test
package service_test

import (
"context"
"fmt"
"github.com/niondir/go-services"
"github.com/niondir/go-service"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"time"
)

var _ services.Initer = &testService{}
var _ services.Runner = &testService{}
var _ service.Initer = &testService{}
var _ service.Runner = &testService{}
var _ fmt.Stringer = testService{}

// testService is a service that tracks it's state to be checked in tests
Expand Down Expand Up @@ -118,7 +118,7 @@ func assertServiceNeverStarted(t *testing.T, s *testService) {
}

func TestStartAndStopWithContext(t *testing.T) {
c := services.NewContainer()
c := service.NewContainer()
s1 := &testService{
Name: "s1",
}
Expand All @@ -135,7 +135,7 @@ func TestStartAndStopWithContext(t *testing.T) {
}

func TestStartAndStopWithContext_timeout(t *testing.T) {
c := services.NewContainer()
c := service.NewContainer()
s1 := &testService{
Name: "s1",
}
Expand All @@ -151,7 +151,7 @@ func TestStartAndStopWithContext_timeout(t *testing.T) {

// Start and Stop multiple services (happy path)
func TestStartAndStop(t *testing.T) {
c := services.NewContainer()
c := service.NewContainer()
s1 := &testService{
Name: "s1",
}
Expand All @@ -174,7 +174,7 @@ func TestStartAndStop(t *testing.T) {

// Start 3 services, the second will just return but the other two will keep running
func TestServiceCanReturnWithoutError(t *testing.T) {
c := services.NewContainer()
c := service.NewContainer()
s1 := &testService{
Name: "s1",
}
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestServiceCanReturnWithoutError(t *testing.T) {

// Start 3 services, the second fails during init, none should run
func TestStopWhenInitFails(t *testing.T) {
c := services.NewContainer()
c := service.NewContainer()
s1 := &testService{
Name: "s1",
}
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestStopWhenInitFails(t *testing.T) {

// Start 3 services, the second fails during run
func TestStopWhenRunFails(t *testing.T) {
c := services.NewContainer()
c := service.NewContainer()
s1 := &testService{
Name: "s1",
}
Expand Down Expand Up @@ -284,7 +284,7 @@ func TestStopWhenRunFails(t *testing.T) {

// Start 3 services, the second fails after run
func TestErrorOnShutdown(t *testing.T) {
c := services.NewContainer()
c := service.NewContainer()
s1 := &testService{
Name: "s1",
}
Expand Down

0 comments on commit 834d773

Please sign in to comment.