Skip to content

Commit 8edddf8

Browse files
authored
Merge pull request #4 from an-prata/stop-comand
Stop comand
2 parents d00d0dd + c70b992 commit 8edddf8

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

daemon/callbacks.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ func (r ReloadSignal) String() string {
2121

2222
func (r ReloadSignal) Signal() {}
2323

24+
// Represent a signal originating at a daemmon command and sent through a
25+
// channel by the stop callback
26+
type StopSignal struct{}
27+
28+
func (r StopSignal) String() string {
29+
return "Stop"
30+
}
31+
32+
func (r StopSignal) Signal() {}
33+
2434
// Returns a function that will sent the `server.Restart` constant through the
2535
// given channel when called.
2636
func GetRestartCallback(serverCommandChan chan server.ServerThreadCommand) DaemonCommandCallback {
@@ -39,6 +49,15 @@ func GetReloadCallback(signalChan chan os.Signal) DaemonCommandCallback {
3949
}
4050
}
4151

52+
// Returns a function that will send a `StopSignal` through the given channel
53+
// when called.
54+
func GetStopCallback(signalChan chan os.Signal) DaemonCommandCallback {
55+
return func(_ DaemonCommandArg) error {
56+
signalChan <- StopSignal{}
57+
return nil
58+
}
59+
}
60+
4261
// Returns a function, that when called, will modify the given log's recording
4362
// log level to match its parameters.
4463
func GetLogPrintCallback(log *logger.Log) DaemonCommandCallback {

daemon/commands.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,26 @@ func CmdRestart(socket net.Conn, log *logger.Log, arg bool) {
115115
log.LogInfo("Restarted!")
116116
}
117117
}
118+
119+
// Sends the stop command to the daemon through the provided socket.
120+
//
121+
// This function is intended as the end of execution for the command it
122+
// represents and will therefore perform I/O operations, output to the user, and
123+
// indicate errors only though these means.
124+
func CmdStop(socket net.Conn, log *logger.Log, arg bool) {
125+
if !arg {
126+
return
127+
}
128+
129+
log.LogInfo("Stopping webby...")
130+
131+
var buf [1]byte
132+
socket.Write(append([]byte(Stop), 0))
133+
socket.Read(buf[:])
134+
135+
if DaemonCommandSuccess(buf[0]) != Success {
136+
log.LogErr("Could not stop webby")
137+
} else {
138+
log.LogInfo("Stopped!")
139+
}
140+
}

daemon/listener.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ const (
3030
// Reloads the configuration file and then restarts.
3131
Reload = "reload"
3232

33+
// Stops the current daemon.
34+
Stop = "stop"
35+
3336
// Sets the log level for recording logs to file. Should interperet its
3437
// argument to be the desired log level.
3538
LogRecord = "log-record"

daemon/proc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Start:
6666
commandListener, err := NewDaemonListener(map[DaemonCommand]DaemonCommandCallback{
6767
Restart: GetRestartCallback(serverCommandChan),
6868
Reload: GetReloadCallback(signalChan),
69+
Stop: GetStopCallback(signalChan),
6970
LogRecord: GetLogRecordCallback(&log),
7071
LogPrint: GetLogPrintCallback(&log),
7172
}, log)

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ func main() {
1616
var daemonProc bool
1717
var reload bool
1818
var restart bool
19+
var stop bool
1920
var logRecord string
2021
var logPrint string
2122

2223
flag.BoolVar(&daemonProc, "daemon", false, "runs the webby server daemon process rather than behaving like a control application")
2324
flag.BoolVar(&reload, "reload", false, "reloads the configuration file and then restarts, this will reset log levels")
2425
flag.BoolVar(&restart, "restart", false, "restarts the webby HTTP server, rescanning directories")
26+
flag.BoolVar(&stop, "stop", false, "stops the running daemon")
2527
flag.StringVar(&logRecord, "log-record", "", "sets the log level to record to file, defaults to 'All'")
2628
flag.StringVar(&logPrint, "log-print", "", "sets the log level to print to standard out, defaults to 'All'")
2729

@@ -46,4 +48,5 @@ func main() {
4648
daemon.CmdSetLogPrintLevel(socket, &log, logPrint)
4749
daemon.CmdRestart(socket, &log, restart)
4850
daemon.CmdReload(socket, &log, reload)
51+
daemon.CmdStop(socket, &log, stop)
4952
}

0 commit comments

Comments
 (0)