Compare commits
No commits in common. "a2061d124f3b5fc7ef5dbcc1872736abd1a00320" and "1544dfc59ec5c2ab669e211419ffc52187118487" have entirely different histories.
a2061d124f
...
1544dfc59e
@ -13,7 +13,7 @@ steps:
|
|||||||
- cp deploy /data
|
- cp deploy /data
|
||||||
- cp config.yml.sample /data/config.yml
|
- cp config.yml.sample /data/config.yml
|
||||||
- cd /data && tar cvf release.tar --exclude=release.tar .
|
- cd /data && tar cvf release.tar --exclude=release.tar .
|
||||||
- name: release
|
- name: relase
|
||||||
image: plugins/gitea-release
|
image: plugins/gitea-release
|
||||||
volumes:
|
volumes:
|
||||||
- name: dist
|
- name: dist
|
||||||
|
@ -17,9 +17,4 @@ listens:
|
|||||||
path: /test-post
|
path: /test-post
|
||||||
headers: {}
|
headers: {}
|
||||||
script: /tmp/run.sh
|
script: /tmp/run.sh
|
||||||
|
|
||||||
# cron job setting
|
|
||||||
cron_jobs:
|
|
||||||
- cron_time: '* * * * *'
|
|
||||||
script: /tmp/run.sh
|
|
||||||
```
|
```
|
||||||
|
115
cmd/svc/main.go
115
cmd/svc/main.go
@ -1,24 +1,20 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"context"
|
"context"
|
||||||
"deploy/pkg/config"
|
"deploy/pkg/config"
|
||||||
"deploy/pkg/logger"
|
"deploy/pkg/logger"
|
||||||
"deploy/pkg/server"
|
"deploy/pkg/server"
|
||||||
"deploy/pkg/tools"
|
"deploy/pkg/tools"
|
||||||
"errors"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/robfig/cron/v3"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"gopkg.in/fsnotify.v1"
|
"gopkg.in/fsnotify.v1"
|
||||||
)
|
)
|
||||||
@ -26,6 +22,7 @@ import (
|
|||||||
var cfgPath string = "./config.yml"
|
var cfgPath string = "./config.yml"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
flag.StringVar(&cfgPath, "c", "./config.yml", "config yaml path")
|
flag.StringVar(&cfgPath, "c", "./config.yml", "config yaml path")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
@ -85,20 +82,11 @@ func runService(reload <-chan struct{}) {
|
|||||||
log := logger.NewLogger(nil)
|
log := logger.NewLogger(nil)
|
||||||
|
|
||||||
var svc *http.Server
|
var svc *http.Server
|
||||||
var cron *Cron
|
|
||||||
|
|
||||||
// infinity loop
|
// infinity loop
|
||||||
for {
|
for {
|
||||||
cfg, err := config.Load(cfgPath)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if svc == nil {
|
if svc == nil {
|
||||||
svc = startServer(cfg, log)
|
svc = startServer(log)
|
||||||
}
|
|
||||||
if cron == nil {
|
|
||||||
cron = startCron(cfg, log)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@ -107,12 +95,9 @@ func runService(reload <-chan struct{}) {
|
|||||||
// channel closed exit program
|
// channel closed exit program
|
||||||
if err := stopServer(svc); err != nil {
|
if err := stopServer(svc); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
|
||||||
if err := stopCron(cron); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if time.Since(lastReload).Seconds() < float64(2*time.Second) {
|
if time.Since(lastReload).Seconds() < float64(2*time.Second) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -121,95 +106,15 @@ func runService(reload <-chan struct{}) {
|
|||||||
if err := stopServer(svc); err != nil {
|
if err := stopServer(svc); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := stopCron(cron); err != nil {
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func startServer(log *logrus.Logger) *http.Server {
|
||||||
|
cfg, err := config.Load(cfgPath)
|
||||||
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
svc = nil
|
|
||||||
cron = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Cron struct {
|
|
||||||
*cron.Cron
|
|
||||||
Logger *logrus.Logger
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Cron) setJobs(cfg *config.Config) {
|
|
||||||
for _, v := range cfg.CronJobs {
|
|
||||||
c.AddFunc(v.CronTime, func() {
|
|
||||||
cmd := exec.Command(v.Script)
|
|
||||||
|
|
||||||
// r, w := io.Pipe()
|
|
||||||
nlog := c.Logger.WithFields(logrus.Fields{"module": "cron", "script": v.Script})
|
|
||||||
reader, err := cmd.StdoutPipe()
|
|
||||||
if err != nil {
|
|
||||||
nlog.Warnf("setup pipe out fail: %+v\n", err)
|
|
||||||
}
|
|
||||||
errReader, err := cmd.StderrPipe()
|
|
||||||
if err != nil {
|
|
||||||
nlog.Warnf("setup pipe out fail: %+v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := cmd.Start(); err != nil {
|
|
||||||
nlog.Warnf("start script fail: %+v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
in := bufio.NewScanner(reader)
|
|
||||||
for in.Scan() {
|
|
||||||
nlog.Debugf(in.Text())
|
|
||||||
}
|
|
||||||
if err := in.Err(); err != nil {
|
|
||||||
nlog.Warnf("script error: %+v\n", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
go func() {
|
|
||||||
in := bufio.NewScanner(errReader)
|
|
||||||
for in.Scan() {
|
|
||||||
nlog.Debugf(in.Text())
|
|
||||||
}
|
|
||||||
if err := in.Err(); err != nil {
|
|
||||||
nlog.Warnf("script error: %+v\n", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func startCron(cfg *config.Config, log *logrus.Logger) *Cron {
|
|
||||||
tz, err := time.LoadLocation("Asia/Taipei")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
c := cron.New(
|
|
||||||
cron.WithLocation(tz),
|
|
||||||
cron.WithParser(cron.NewParser(cron.Minute|cron.Hour|cron.Dom|cron.Month|cron.Dow)),
|
|
||||||
)
|
|
||||||
|
|
||||||
c2 := &Cron{Cron: c, Logger: log}
|
|
||||||
|
|
||||||
c2.setJobs(cfg)
|
|
||||||
c2.Start()
|
|
||||||
|
|
||||||
return c2
|
|
||||||
}
|
|
||||||
|
|
||||||
func stopCron(c *Cron) error {
|
|
||||||
ctx := c.Stop()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return nil
|
|
||||||
case <-time.Tick(time.Second * 5):
|
|
||||||
// skip stop jobs
|
|
||||||
return errors.New("stop jobs fail")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func startServer(cfg *config.Config, log *logrus.Logger) *http.Server {
|
|
||||||
|
|
||||||
engine := server.NewServer(log)
|
engine := server.NewServer(log)
|
||||||
// set routes
|
// set routes
|
||||||
|
@ -7,7 +7,3 @@ listens:
|
|||||||
path: /t
|
path: /t
|
||||||
headers: {}
|
headers: {}
|
||||||
script: /tmp/run.sh
|
script: /tmp/run.sh
|
||||||
|
|
||||||
cron_jobs:
|
|
||||||
- cron_time: '0 1 * * *'
|
|
||||||
script: /tmp/run.sh
|
|
||||||
|
3
go.mod
3
go.mod
@ -1,6 +1,6 @@
|
|||||||
module deploy
|
module deploy
|
||||||
|
|
||||||
go 1.16
|
go 1.17
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gin-gonic/gin v1.7.2
|
github.com/gin-gonic/gin v1.7.2
|
||||||
@ -24,7 +24,6 @@ require (
|
|||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.1 // indirect
|
github.com/modern-go/reflect2 v1.0.1 // indirect
|
||||||
github.com/otakukaze/envconfig v1.0.4 // indirect
|
github.com/otakukaze/envconfig v1.0.4 // indirect
|
||||||
github.com/robfig/cron/v3 v3.0.0 // indirect
|
|
||||||
github.com/ugorji/go/codec v1.2.6 // indirect
|
github.com/ugorji/go/codec v1.2.6 // indirect
|
||||||
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
|
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
|
||||||
golang.org/x/sys v0.0.0-20210611083646-a4fc73990273 // indirect
|
golang.org/x/sys v0.0.0-20210611083646-a4fc73990273 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -48,8 +48,6 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
|||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
|
|
||||||
github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
|
||||||
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
errors "github.com/pkg/errors"
|
errors "github.com/pkg/errors"
|
||||||
"github.com/robfig/cron/v3"
|
|
||||||
|
|
||||||
confLoader "github.com/otakukaze/config-loader"
|
confLoader "github.com/otakukaze/config-loader"
|
||||||
)
|
)
|
||||||
@ -15,18 +14,12 @@ import (
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
Server Server `yaml:"server"`
|
Server Server `yaml:"server"`
|
||||||
Listens []Listen `yaml:"listens"`
|
Listens []Listen `yaml:"listens"`
|
||||||
CronJobs []CronJob `yaml:"cron_jobs"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Port int `yaml:"port" env:"SERVER_PORT" default:"10230"`
|
Port int `yaml:"port" env:"SERVER_PORT" default:"10230"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CronJob struct {
|
|
||||||
CronTime string `yaml:"cron_time"`
|
|
||||||
Script string `yaml:"script"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Listen struct {
|
type Listen struct {
|
||||||
HTTP *HTTPListen `yaml:"http"`
|
HTTP *HTTPListen `yaml:"http"`
|
||||||
}
|
}
|
||||||
@ -95,19 +88,5 @@ func Load(p ...string) (*Config, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check cronJob
|
|
||||||
cronParser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
|
|
||||||
for _, v := range cfg.CronJobs {
|
|
||||||
_, err := cronParser.Parse(v.CronTime)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// check script exists
|
|
||||||
if !tools.CheckExists(v.Script, false) {
|
|
||||||
return nil, errors.WithStack(fmt.Errorf("cron (%s) script not exists: %s\n", v.CronTime, v.Script))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ func (s *HTTPServer) setHandler(check map[string]string, script string) gin.Hand
|
|||||||
cmd := exec.Command(script)
|
cmd := exec.Command(script)
|
||||||
|
|
||||||
// r, w := io.Pipe()
|
// r, w := io.Pipe()
|
||||||
nlog := s.Logger.WithFields(logrus.Fields{"module": "http", "script": script})
|
nlog := s.Logger.WithFields(logrus.Fields{"script": script})
|
||||||
reader, err := cmd.StdoutPipe()
|
reader, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
nlog.Warnf("setup pipe out fail: %+v\n", err)
|
nlog.Warnf("setup pipe out fail: %+v\n", err)
|
||||||
|
@ -22,7 +22,7 @@ func (s Set) Size() int {
|
|||||||
func (s Set) Keys() []interface{} {
|
func (s Set) Keys() []interface{} {
|
||||||
keys := make([]interface{}, 0)
|
keys := make([]interface{}, 0)
|
||||||
|
|
||||||
for k := range s {
|
for k, _ := range s {
|
||||||
keys = append(keys, k)
|
keys = append(keys, k)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user