Skip to content

Commit

Permalink
naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Krieger authored and matthiasng committed Dec 11, 2019
1 parent 72fda00 commit d06448e
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 78 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ go build -o service-shark.exe main.go
Bind argument to environment variable with "env:{VAR_NAME}".
```

Exampe
Example
```
service-shark.exe -name MyService -workdir C:/MyService -cmd java -- -jar MyProg.jar -Xmx1G -myArg "env:TEST_VALUE"
```
Service Shark will run ``java`` with ``-jar MyProg.jar -Xmx1G -myArg "123"`` from ``C:/MyService``.

See [example/test-example-service.ps1](./example/test-example-service.ps1) for a complate example.
See [example/test-example-service.ps1](./example/test-example-service.ps1) for a complete example.
1 change: 1 addition & 0 deletions command/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/matthiasng/service-shark/service"
)

// Host implements the service.Program interface to run any command as service
type Host struct {
Arguments cli.Arguments
cmd *exec.Cmd
Expand Down
11 changes: 11 additions & 0 deletions service/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package service

// Environment contains information about the environment
// your application is running in.
type Environment interface {
// IsWindowsService reports whether the program is running as a Windows Service.
IsWindowsService() bool

// ExitService can be used to signal a service crash. Service will exit with a user define error code 3.
ExitService(error)
}
19 changes: 19 additions & 0 deletions service/program.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package service

// Program interface contains Start and Stop methods which are called
// when the service is started and stopped.
//
// The Start methods must be non-blocking.
//
// Implement this interface and pass it to the Run function to start your program.
type Program interface {
// Start must be non-blocking.
Start(Environment) error

// Stop is called in response to syscall.SIGINT, syscall.SIGTERM, or when a
// Windows Service is stopped.
Stop() error

// Name returns the service name.
Name() string
}
40 changes: 40 additions & 0 deletions service/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package service

import (
"os/signal"

wsvc "golang.org/x/sys/windows/svc"
)

// Create variables for svc and signal functions so we can mock them in tests
var svcIsAnInteractiveSession = wsvc.IsAnInteractiveSession
var svcRun = wsvc.Run
var signalNotify = signal.Notify

// Run runs an implementation of the Service interface.
//
// Run will block until the Windows Service is stopped or Ctrl+C is pressed if
// running from the console.
//
// Stopping the Windows Service and Ctrl+C will call the Service's Stop method to
// initiate a graceful shutdown.
//
// Note that WM_CLOSE is not handled (end task) and the Service's Stop method will
// not be called.
func Run(program Program) error {
var err error

interactive, err := svcIsAnInteractiveSession()
if err != nil {
return err
}

ws := &windowsService{
name: program.Name(),
svc: program,
isInteractive: interactive,
signalErrChan: make(chan error, 1),
}

return ws.run()
}
29 changes: 0 additions & 29 deletions service/service.go

This file was deleted.

46 changes: 1 addition & 45 deletions service/service_windows.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,15 @@
// +build windows

package service

import (
"os"
"os/signal"
"path/filepath"
"syscall"

wsvc "golang.org/x/sys/windows/svc"
)

// Create variables for svc and signal functions so we can mock them in tests
var svcIsAnInteractiveSession = wsvc.IsAnInteractiveSession
var svcRun = wsvc.Run
var signalNotify = signal.Notify

// FixWorkingDirectory changes the working directory to the exeutable directory.
// The working directory for a Windows Service is C:\Windows\System32 ...
func FixWorkingDirectory() error {
dir := filepath.Dir(os.Args[0])
return os.Chdir(dir)
}

// Run runs an implementation of the Service interface.
//
// Run will block until the Windows Service is stopped or Ctrl+C is pressed if
// running from the console.
//
// Stopping the Windows Service and Ctrl+C will call the Service's Stop method to
// initiate a graceful shutdown.
//
// Note that WM_CLOSE is not handled (end task) and the Service's Stop method will
// not be called.
func Run(service Service) error {
var err error

interactive, err := svcIsAnInteractiveSession()
if err != nil {
return err
}

ws := &windowsService{
name: service.Name(),
svc: service,
isInteractive: interactive,
signalErrChan: make(chan error, 1),
}

return ws.run()
}

type windowsService struct {
name string
svc Service
svc Program
isInteractive bool
signalErrChan chan error
serviceErr error
Expand Down
2 changes: 0 additions & 2 deletions service/service_windows_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build windows

package service

import (
Expand Down
13 changes: 13 additions & 0 deletions service/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package service

import (
"os"
"path/filepath"
)

// FixWorkingDirectory changes the working directory to the exeutable directory.
// The working directory for a Windows Service is C:\Windows\System32 ...
func FixWorkingDirectory() error {
dir := filepath.Dir(os.Args[0])
return os.Chdir(dir)
}
30 changes: 30 additions & 0 deletions service/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package service

import (
"os"
"os/user"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func Test_FixWorkingDirectory(t *testing.T) {
require := require.New(t)

expectedDir := filepath.Dir(os.Args[0])

usr, err := user.Current()
require.NoError(err)
require.NotEqual(expectedDir, usr.HomeDir)

err = os.Chdir(usr.HomeDir)
require.NoError(err)

err = FixWorkingDirectory()
require.NoError(err)

newDir, err := os.Getwd()
require.NoError(err)
require.Equal(expectedDir, newDir)
}

0 comments on commit d06448e

Please sign in to comment.