Skip to content

Commit

Permalink
service handling
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasng committed Dec 6, 2019
1 parent 391dc1a commit 942b0f0
Show file tree
Hide file tree
Showing 7 changed files with 543 additions and 668 deletions.
17 changes: 10 additions & 7 deletions command/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Host struct {
quitCompleted chan struct{}
}

func (h *Host) Init(env service.Environment) error {
func (h *Host) init(env service.Environment) error {
h.cmd = exec.Command(h.CmdConfig.Name, h.CmdConfig.Arguments...)

if env.IsWindowsService() {
Expand All @@ -33,9 +33,7 @@ func (h *Host) Init(env service.Environment) error {
return err
}

name := "" // #todo

logFileName := fmt.Sprintf("%s_%s.log", name, time.Now().Format("02-01-2006_15-04-05"))
logFileName := fmt.Sprintf("%s_%s.log", env.Name(), time.Now().Format("02-01-2006_15-04-05"))
logFilePath := path.Join(h.LogDirecotry, logFileName)

logFile, err := os.Create(logFilePath)
Expand All @@ -53,7 +51,12 @@ func (h *Host) Init(env service.Environment) error {
return h.cmd.Start()
}

func (h *Host) Start() error {
func (h *Host) Start(env service.Environment) error {
err := h.init(env)
if err != nil {
return err
}

h.quitSignal = make(chan struct{})
h.quitCompleted = make(chan struct{})

Expand All @@ -64,8 +67,8 @@ func (h *Host) Start() error {
}()

go func() {
_ = h.cmd.Wait()
os.Exit(1) // service command stopped -> stop service
err = h.cmd.Wait()
env.ExitService(err) // service command stopped -> service error
}()

return nil
Expand Down
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/matthiasng/service-shark/service"
)

// #todo working directory

// parser := argparse.NewParser("service-wrapper", `Run a "-command" with "-arguments" as service`)

// serviceName := parser.String("n", "name", &argparse.Options{
Expand Down Expand Up @@ -37,16 +39,16 @@ import (
func main() {
host := command.Host{
CmdConfig: command.Config{
Name: "powershell",
//Name: "C:/Program Files/PowerShell/7-preview/preview/pwsh-preview.cmd",
//Name: "powershell",
Name: "C:/Program Files/PowerShell/7-preview/preview/pwsh-preview.cmd",
Arguments: []string{
`P:\_dev\projects\service-shark\example\test-service.ps1`,
},
},
LogDirecotry: "c:/tmp",
}

if err := service.Run(&host); err != nil {
log.Fatal(err)
if err := service.Run(&host, "todo"); err != nil {
log.Fatalf("[error] - %v", err)
}
}
24 changes: 9 additions & 15 deletions service/service.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
package service

import "os/signal"

// Create variable signal.Notify function so we can mock it in tests
var signalNotify = signal.Notify

// Service interface contains Start and Stop methods which are called
// when the service is started and stopped. The Init method is called
// before the service is started, and after it's determined if the program
// is running as a Windows Service.
// when the service is started and stopped.
//
// The Start and Init methods must be non-blocking.
// The Start methods must be non-blocking.
//
// Implement this interface and pass it to the Run function to start your program.
type Service interface {
// Init is called before the program/service is started and after it's
// determined if the program is running as a Windows Service. This method must
// be non-blocking.
Init(Environment) error

// Start is called after Init. This method must be non-blocking.
Start() error
Start(Environment) error

// Stop is called in response to syscall.SIGINT, syscall.SIGTERM, or when a
// Windows Service is stopped.
Expand All @@ -32,4 +20,10 @@ type Service interface {
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)

// Name returns the service name.
Name() string
}
37 changes: 0 additions & 37 deletions service/service_other.go

This file was deleted.

72 changes: 0 additions & 72 deletions service/service_other_test.go

This file was deleted.

Loading

0 comments on commit 942b0f0

Please sign in to comment.