Compare commits

...

5 Commits

Author SHA1 Message Date
seriousm4x 86ed3c1c08 fix: cross platform build 2023-10-19 19:34:18 +02:00
seriousm4x 92a142b5ee fix: import sys/windows 2023-10-19 19:02:13 +02:00
seriousm4x f351df6b88 add upsnap version to issue template 2023-10-19 18:56:11 +02:00
seriousm4x d50245ebaf capitalize logs 2023-10-19 18:54:15 +02:00
seriousm4x 2f3ad2fdcb fix: use SIGTERM, then SIGKILL to avoid zombies #266 2023-10-19 18:47:14 +02:00
8 changed files with 57 additions and 41 deletions
+11 -3
View File
@@ -2,9 +2,8 @@
name: "\U0001F577️ Bug report"
about: Create a report to help the project improve
title: "[BUG]"
labels: ''
assignees: ''
labels: ""
assignees: ""
---
**Describe the bug**
@@ -12,6 +11,7 @@ A clear and concise description of what the bug is.
**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
@@ -20,7 +20,15 @@ Steps to reproduce the behavior:
**Expected behavior**
A clear and concise description of what you expected to happen.
**UpSnap version**
```bash
$ upsnap --version
upsnap version 4.1.1
```
**Logs**
```
formatted logs here
```
+2 -2
View File
@@ -165,13 +165,13 @@ func RunWakeShutdown(app *pocketbase.PocketBase) {
func StopAll() {
if PingRunning {
logger.Info.Println("stopping ping cronjob")
logger.Info.Println("Stopping ping cronjob")
ctx := CronPing.Stop()
<-ctx.Done()
}
if WakeShutdownRunning {
logger.Info.Println("stopping wake/shutdown cronjob")
logger.Info.Println("Stopping wake/shutdown cronjob")
ctx := CronWakeShutdown.Stop()
<-ctx.Done()
}
+3 -5
View File
@@ -13,7 +13,7 @@ import (
)
func ShutdownDevice(device *models.Record) error {
logger.Info.Println("shutdown triggered for", device.GetString("name"))
logger.Info.Println("Shutdown triggered for", device.GetString("name"))
shutdown_cmd := device.GetString("shutdown_cmd")
if shutdown_cmd == "" {
return fmt.Errorf("%s: no shutdown_cmd definded", device.GetString("name"))
@@ -31,9 +31,10 @@ func ShutdownDevice(device *models.Record) error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
cmd := exec.CommandContext(ctx, shell, shell_arg, shutdown_cmd)
SetProcessAttributes(cmd)
logger.Debug.Println(cmd)
var stderr bytes.Buffer
cmd.Stderr = &stderr
@@ -56,7 +57,6 @@ func ShutdownDevice(device *models.Record) error {
if err := KillProcess(cmd.Process); err != nil {
logger.Error.Println(err)
}
cancel()
return fmt.Errorf("%s not offline after 2 minutes", device.GetString("name"))
}
isOnline := PingDevice(device)
@@ -64,7 +64,6 @@ func ShutdownDevice(device *models.Record) error {
if err := KillProcess(cmd.Process); err != nil {
logger.Error.Println(err)
}
cancel()
return nil
}
case err := <-done:
@@ -72,7 +71,6 @@ func ShutdownDevice(device *models.Record) error {
if err := KillProcess(cmd.Process); err != nil {
logger.Error.Println(err)
}
cancel()
return fmt.Errorf("%s", stderr.String())
}
}
-25
View File
@@ -1,25 +0,0 @@
package networking
import (
"os"
"os/exec"
"golang.org/x/sys/unix"
"github.com/seriousm4x/upsnap/logger"
)
// Set platform specifiy custom process attributes
func SetProcessAttributes(cmd *exec.Cmd) {
cmd.SysProcAttr = &unix.SysProcAttr{Setpgid: true}
}
// Kills child processes on Linux. Windows doesn't provide a direct way to kill child processes, so we kill just the main process.
func KillProcess(process *os.Process) error {
pgid, err := unix.Getpgid(process.Pid)
logger.Debug.Println(pgid)
if err != nil {
return err
}
return unix.Kill(-pgid, unix.SIGKILL)
}
+20 -4
View File
@@ -1,19 +1,35 @@
//go:build !linux
//go:build !windows
// +build !windows
package networking
import (
"os"
"os/exec"
"github.com/seriousm4x/upsnap/logger"
"golang.org/x/sys/unix"
)
// Set platform specifiy custom process attributes
func SetProcessAttributes(cmd *exec.Cmd) {}
func SetProcessAttributes(cmd *exec.Cmd) {
cmd.SysProcAttr = &unix.SysProcAttr{Setpgid: true}
}
// Kills child processes on Linux. Windows doesn't provide a direct way to kill child processes, so we kill just the main process.
func KillProcess(process *os.Process) error {
if err := process.Kill(); err != nil {
logger.Warning.Println("Your shutdown cmd didn't finish in 2 minutes. It will be killed.")
pgid, err := unix.Getpgid(process.Pid)
if err != nil {
return err
}
return nil
err = unix.Kill(-pgid, unix.SIGTERM)
if err != nil {
return unix.Kill(-pgid, unix.SIGKILL)
}
_, err = process.Wait()
return err
}
+19
View File
@@ -0,0 +1,19 @@
//go:build windows
// +build windows
package networking
import (
"os"
"os/exec"
"golang.org/x/sys/windows"
)
// Set platform specifiy custom process attributes
func SetProcessAttributes(cmd *exec.Cmd) {}
// Kills child processes on Linux. Windows doesn't provide a direct way to kill child processes, so we kill just the main process.
func KillProcess(process *os.Process) error {
return windows.GenerateConsoleCtrlEvent(windows.CTRL_BREAK_EVENT, uint32(process.Pid))
}
+1 -1
View File
@@ -16,7 +16,7 @@ type SolResponse struct {
}
func SleepDevice(device *models.Record) (SolResponse, error) {
logger.Info.Println("sleep triggered for", device.GetString("name"))
logger.Info.Println("Sleep triggered for", device.GetString("name"))
var solResp SolResponse
var url string
+1 -1
View File
@@ -9,7 +9,7 @@ import (
)
func WakeDevice(device *models.Record) error {
logger.Info.Println("wake triggered for", device.GetString("name"))
logger.Info.Println("Wake triggered for", device.GetString("name"))
err := SendMagicPacket(device)
if err != nil {
return err