feat: publish standalone worker
Separate worker packaging and service lifecycle from the control plane.
Этот коммит содержится в:
35
internal/util/format_duration.go
Обычный файл
35
internal/util/format_duration.go
Обычный файл
@@ -0,0 +1,35 @@
|
||||
// Package util provides functionality.
|
||||
package util
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"rsgit.ru/rsmon/rsmon/config/translator"
|
||||
)
|
||||
|
||||
// FormatDuration provides functionality.
|
||||
func FormatDuration(duration int64) string {
|
||||
// spew.Dump(translator.Translator)
|
||||
hours := duration / 3600
|
||||
minutes := (duration - hours*3600) / 60
|
||||
// seconds := duration % 60
|
||||
str := ""
|
||||
if hours > 0 {
|
||||
tr, err := translator.Translator.C("hours", float64(hours), 0, strconv.FormatInt(hours, 10))
|
||||
if err != nil {
|
||||
log.Println("translator error", err)
|
||||
return ""
|
||||
}
|
||||
str = str + tr + ", "
|
||||
}
|
||||
|
||||
tr, err := translator.Translator.C("minutes", float64(minutes), 0, strconv.FormatInt(minutes, 10))
|
||||
if err != nil {
|
||||
log.Println("translator error", err)
|
||||
return ""
|
||||
}
|
||||
|
||||
str += tr
|
||||
return str
|
||||
}
|
||||
15
internal/util/format_duration_test.go
Обычный файл
15
internal/util/format_duration_test.go
Обычный файл
@@ -0,0 +1,15 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFormatDuration(t *testing.T) {
|
||||
assert.Equal(t, "1 минуту", FormatDuration(60), "")
|
||||
assert.Equal(t, "2 минуты", FormatDuration(120), "")
|
||||
assert.Equal(t, "5 минут", FormatDuration(300), "")
|
||||
assert.Equal(t, "1 час, 5 минут", FormatDuration(3600+300), "")
|
||||
assert.Equal(t, "1 час, 0 минут", FormatDuration(3600), "")
|
||||
}
|
||||
31
internal/util/unix/pidfile.go
Обычный файл
31
internal/util/unix/pidfile.go
Обычный файл
@@ -0,0 +1,31 @@
|
||||
// Package unix provides functionality.
|
||||
package unix
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// WritePidFile provides functionality.
|
||||
// Write a pid file, but first make sure it doesn't exist with a running pid.
|
||||
func WritePidFile(pidFile string) error {
|
||||
// Read in the pid file as a slice of bytes.
|
||||
if piddata, err := os.ReadFile(pidFile); err == nil {
|
||||
// Convert the file contents to an integer.
|
||||
if pid, err := strconv.Atoi(string(piddata)); err == nil {
|
||||
// Look for the pid in the process list.
|
||||
if process, err := os.FindProcess(pid); err == nil {
|
||||
// Send the process a signal zero kill.
|
||||
if err := process.Signal(syscall.Signal(0)); err == nil {
|
||||
// We only get an error if the pid isn't running, or it's not ours.
|
||||
return fmt.Errorf("pid already running: %d", pid)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// If we get here, then the pidfile didn't exist,
|
||||
// or the pid in it doesn't belong to the user running this app.
|
||||
return os.WriteFile(pidFile, []byte(fmt.Sprintf("%d", os.Getpid())), 0o664)
|
||||
}
|
||||
Ссылка в новой задаче
Block a user