Skip to content

Commit

Permalink
finish rename, cleanup logfile
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasng committed Dec 8, 2019
1 parent 5421b6d commit 41c60fd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
run: |
$env:GOOS="windows"
$env:GOARCH="amd64"
go build -o bin/service-wrapper-amd64.exe
go build -o bin/service-shark-amd64.exe
- name: upload artifacts
uses: actions/upload-artifact@master
with:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/matthiasng/service-shark?sort=semver)
![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/matthiasng/service-shark/build/master)
[![codecov](https://codecov.io/gh/matthiasng/service-shark/branch/master/graph/badge.svg)](https://codecov.io/gh/matthiasng/service-shark)
[![Go Report Card](https://goreportcard.com/badge/github.com/matthiasng/service-wrapper)](https://goreportcard.com/report/github.com/matthiasng/service-wrapper)
[![Go Report Card](https://goreportcard.com/badge/github.com/matthiasng/service-shark)](https://goreportcard.com/report/github.com/matthiasng/service-shark)

Service Shark can be used to to host any executable as a Windows service.

Expand Down
1 change: 1 addition & 0 deletions cli/arguments.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cli

// Arguments container
type Arguments struct {
Name string
WorkingDirectory string
Expand Down
30 changes: 12 additions & 18 deletions command/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ import (
)

type Host struct {
Arguments cli.Arguments
cmd *exec.Cmd
logFile *os.File
quitSignal chan struct{}
quitCompleted chan struct{}
Arguments cli.Arguments
cmd *exec.Cmd
logFile *os.File
}

func (h *Host) init(env service.Environment) error {
Expand Down Expand Up @@ -46,38 +44,34 @@ func (h *Host) init(env service.Environment) error {
return h.cmd.Start()
}

// Start prepares logging and executes the command
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{})

go func() {
<-h.quitSignal
_ = h.cmd.Process.Kill()
close(h.quitCompleted)
}()

go func() {
err = h.cmd.Wait()
h.cleanup()
env.ExitService(err) // service command stopped -> service error
}()

return nil
}

// Stop kills the command and closes the log file
func (h *Host) Stop() error {
close(h.quitSignal)
<-h.quitCompleted

_ = h.logFile.Close()
_ = h.cmd.Process.Kill()
h.cleanup()

return nil
}

func (h *Host) cleanup() {
_ = h.logFile.Close()
}

func (h *Host) Name() string {
return h.Arguments.Name
}

0 comments on commit 41c60fd

Please sign in to comment.