From 8784211f7b12b9fc69aed035678d71362c730af7 Mon Sep 17 00:00:00 2001 From: nxshock Date: Tue, 29 Mar 2022 19:45:31 +0500 Subject: [PATCH] Merge JobConfig to Job structure --- httpserver.go | 4 ++-- job.go | 52 +++++++++++++++++++++++++++++---------------------- job_test.go | 22 ---------------------- parser.go | 20 -------------------- 4 files changed, 32 insertions(+), 66 deletions(-) delete mode 100644 job_test.go delete mode 100644 parser.go diff --git a/httpserver.go b/httpserver.go index 78c7a74..16e863c 100644 --- a/httpserver.go +++ b/httpserver.go @@ -60,12 +60,12 @@ func handleForceStart(w http.ResponseWriter, r *http.Request) { for _, jobEntry := range jobEntries { job := jobEntry.Job.(*Job) - if job.FileName == jobName { + if job.Name == jobName { host, _, err := net.SplitHostPort(r.RemoteAddr) if err != nil { host = r.RemoteAddr } - log.WithField("job", "http_server").Printf("forced start %s from %s", job.FileName, host) + log.WithField("job", "http_server").Printf("forced start %s from %s", job.Name, host) go job.Run() time.Sleep(time.Second / 4) // wait some time for job start http.Redirect(w, r, "/", http.StatusTemporaryRedirect) diff --git a/job.go b/job.go index 0abcf48..6e973a1 100644 --- a/job.go +++ b/job.go @@ -14,20 +14,17 @@ import ( log "github.com/sirupsen/logrus" ) +// JobConfig is a TOML representation of job type JobConfig struct { - Cron string - Command string - Description string + Cron string // cron decription + Command string // command for execution + Description string // job description } type Job struct { Name string // from filename - Cron string // cron decription - Command string // command for execution - Params []string // command params - FileName string // short job name - Description string // job description + JobConfig // Fields for stats CurrentRunningCount int @@ -48,15 +45,9 @@ func readJob(filePath string) (*Job, error) { return nil, err } - command, params := parseCommand(jobConfig.Command) - job := &Job{ - Name: strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath)), - Cron: jobConfig.Cron, - Command: command, - Params: params, - FileName: strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filepath.Base(filePath))), - Description: jobConfig.Description} + Name: strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath)), + JobConfig: jobConfig} return job, nil } @@ -67,6 +58,21 @@ func (js *JobConfig) Write() { ioutil.WriteFile("job.conf", buf.Bytes(), 0644) } +func (j *Job) CommandAndParams() (command string, params []string) { + quoted := false + items := strings.FieldsFunc(j.JobConfig.Command, func(r rune) bool { + if r == '"' { + quoted = !quoted + } + return !quoted && r == ' ' + }) + for i := range items { + items[i] = strings.Trim(items[i], `"`) + } + + return items[0], items[1:] +} + func (j *Job) Run() { startTime := time.Now() @@ -75,7 +81,7 @@ func (j *Job) Run() { j.LastStartTime = startTime.Format(config.TimeFormat) globalMutex.Unlock() - jobLogFile, _ := os.OpenFile(filepath.Join(config.LogFilesPath, j.FileName+".txt"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) + jobLogFile, _ := os.OpenFile(filepath.Join(config.LogFilesPath, j.Name+".txt"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) defer jobLogFile.Close() defer jobLogFile.WriteString("\n") @@ -83,17 +89,19 @@ func (j *Job) Run() { l.SetOutput(jobLogFile) l.SetFormatter(log.StandardLogger().Formatter) - log.WithField("job", j.FileName).Info("started") + log.WithField("job", j.Name).Info("started") l.Info("started") - cmd := exec.Command(j.Command, j.Params...) + command, params := j.CommandAndParams() + + cmd := exec.Command(command, params...) cmd.Stdout = jobLogFile cmd.Stderr = jobLogFile err := cmd.Run() if err != nil { - log.WithField("job", j.FileName).Error(err.Error()) - l.WithField("job", j.FileName).Error(err.Error()) + log.WithField("job", j.Name).Error(err.Error()) + l.WithField("job", j.Name).Error(err.Error()) globalMutex.Lock() j.LastError = err.Error() @@ -105,7 +113,7 @@ func (j *Job) Run() { } endTime := time.Now() - log.WithField("job", j.FileName).Infof("finished (%s)", endTime.Sub(startTime).Truncate(time.Second).String()) + log.WithField("job", j.Name).Infof("finished (%s)", endTime.Sub(startTime).Truncate(time.Second).String()) l.Infof("finished (%s)", endTime.Sub(startTime).Truncate(time.Second).String()) globalMutex.Lock() diff --git a/job_test.go b/job_test.go deleted file mode 100644 index d2450cb..0000000 --- a/job_test.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestReadJob(t *testing.T) { - expectedJob := &Job{ - Name: "job", - Cron: "* * * * *", - Command: "command", - Params: []string{"param1 param1", "param2"}, - FileName: "job", - Description: "comment"} - - job, err := readJob("tests/job.conf") - assert.NoError(t, err) - - assert.Equal(t, expectedJob, job) -} diff --git a/parser.go b/parser.go deleted file mode 100644 index 43069a9..0000000 --- a/parser.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import ( - "strings" -) - -func parseCommand(s string) (command string, params []string) { - quoted := false - items := strings.FieldsFunc(s, func(r rune) bool { - if r == '"' { - quoted = !quoted - } - return !quoted && r == ' ' - }) - for i := range items { - items[i] = strings.Trim(items[i], `"`) - } - - return items[0], items[1:] -}