-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72fda00
commit d06448e
Showing
10 changed files
with
117 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// +build windows | ||
|
||
package service | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |