mirror of https://github.com/jlelse/GoBlog
Add hourly hooks and improve command execution code
This commit is contained in:
parent
753424f715
commit
141fe3485f
|
@ -106,6 +106,7 @@ type configUser struct {
|
|||
|
||||
type configHooks struct {
|
||||
Shell string `mapstructure:"shell"`
|
||||
Hourly []string `mapstructure:"hourly"`
|
||||
PreStart []string `mapstructure:"prestart"`
|
||||
}
|
||||
|
||||
|
|
2
go.mod
2
go.mod
|
@ -37,7 +37,7 @@ require (
|
|||
github.com/yuin/goldmark-emoji v1.0.1
|
||||
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
|
||||
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0 // indirect
|
||||
golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7 // indirect
|
||||
golang.org/x/sys v0.0.0-20201018121011-98379d014ca7 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
|
||||
gopkg.in/ini.v1 v1.62.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.3.0 // indirect
|
||||
|
|
4
go.sum
4
go.sum
|
@ -372,8 +372,8 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c h1:UIcGWL6/wpCfyGuJnRFJRurA+yj8RrW7Q6x2YMCXt6c=
|
||||
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7 h1:XtNJkfEjb4zR3q20BBBcYUykVOEMgZeIUOpBPfNYgxg=
|
||||
golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201018121011-98379d014ca7 h1:CNOpL+H7PSxBI7dF/EIUsfOguRSzWp6CQ91yxZE6PG4=
|
||||
golang.org/x/sys v0.0.0-20201018121011-98379d014ca7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
|
||||
|
|
44
hooks.go
44
hooks.go
|
@ -1,10 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
func preStartHooks() {
|
||||
|
@ -14,22 +13,29 @@ func preStartHooks() {
|
|||
}
|
||||
}
|
||||
|
||||
func executeCommand(cmd string) {
|
||||
var stdout, stderr bytes.Buffer
|
||||
parsed := exec.Command(appConfig.Hooks.Shell, "-c", cmd)
|
||||
parsed.Stdout = &stdout
|
||||
parsed.Stderr = &stderr
|
||||
cmdErr := parsed.Run()
|
||||
if cmdErr != nil {
|
||||
fmt.Println("Executing command failed:")
|
||||
fmt.Println(cmdErr.Error())
|
||||
}
|
||||
if stdout.Len() > 0 {
|
||||
log.Println("Output:")
|
||||
log.Print(stdout.String())
|
||||
}
|
||||
if stderr.Len() > 0 {
|
||||
log.Println("Error:")
|
||||
log.Print(stderr.String())
|
||||
func startHourlyHooks() {
|
||||
for _, cmd := range appConfig.Hooks.Hourly {
|
||||
go func(cmd string) {
|
||||
run := func() {
|
||||
log.Println("Executing hourly hook:", cmd)
|
||||
executeCommand(cmd)
|
||||
}
|
||||
// Execute once
|
||||
run()
|
||||
// Start ticker and execute regularly
|
||||
ticker := time.NewTicker(1 * time.Hour)
|
||||
for range ticker.C {
|
||||
run()
|
||||
}
|
||||
}(cmd)
|
||||
}
|
||||
}
|
||||
|
||||
func executeCommand(cmd string) {
|
||||
out, err := exec.Command(appConfig.Hooks.Shell, "-c", cmd).CombinedOutput()
|
||||
if err != nil {
|
||||
log.Println("Failed to execute command:", err.Error())
|
||||
}
|
||||
log.Println("Output:")
|
||||
log.Print(string(out))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue