Skip to content

Commit

Permalink
Add shell property to Jobs
Browse files Browse the repository at this point in the history
This allows to run commands using shell or not using it.
  • Loading branch information
Victor Castell committed May 17, 2016
1 parent da1d988 commit f7ad64b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
26 changes: 22 additions & 4 deletions dkron/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dkron
import (
"math/rand"
"os/exec"
"runtime"
"time"

"github.com/Sirupsen/logrus"
Expand All @@ -26,11 +27,28 @@ func (a *AgentCommand) invokeJob(execution *Execution) error {

output, _ := circbuf.NewBuffer(maxBufSize)

args, err := shellwords.Parse(job.Command)
if err != nil {
log.WithError(err).Fatal("proc: Error parsing command arguments")
// Determine the shell invocation based on OS
var (
shell, flag string
cmd *exec.Cmd
)
if job.Shell {
if runtime.GOOS == windows {
shell = "cmd"
flag = "/C"
} else {
shell = "/bin/sh"
flag = "-c"
}
cmd = exec.Command(shell, flag, job.Command)
} else {
args, err := shellwords.Parse(job.Command)
if err != nil {
log.WithError(err).Fatal("proc: Error parsing command arguments")
}
cmd = exec.Command(args[0], args[1:]...)
}
cmd := exec.Command(args[0], args[1:]...)

cmd.Stderr = output
cmd.Stdout = output

Expand Down
3 changes: 3 additions & 0 deletions dkron/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type Job struct {
// Cron expression for the job. When to run the job.
Schedule string `json:"schedule"`

// Use shell to run the command.
Shell bool `json:"shell"`

// Command to run. Must be a shell command to execute.
Command string `json:"command"`

Expand Down
33 changes: 18 additions & 15 deletions dkron/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,37 @@ package dkron

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSchedule(t *testing.T) {
sched := NewScheduler()

if sched.Started == true {
t.Fatal("The scheduler should be stopped.")
}
assert.NotEqual(t, true, sched.Started)

testJob1 := &Job{
Name: "cron_job", Schedule: "@every 2s", Command: "echo 'test1'", Owner: "John Dough", OwnerEmail: "[email protected]",
Name: "cron_job",
Schedule: "@every 2s",
Command: "echo 'test1'",
Owner: "John Dough",
OwnerEmail: "[email protected]",
Shell: true,
}
sched.Start([]*Job{testJob1})

if sched.Started != true {
t.Fatal("The scheduler should be started.")
}
assert.Equal(t, true, sched.Started)

testJob2 := &Job{
Name: "cron_job", Schedule: "@every 5s", Command: "echo 'test2'", Owner: "John Dough", OwnerEmail: "[email protected]",
Name: "cron_job",
Schedule: "@every 5s",
Command: "echo 'test2'",
Owner: "John Dough",
OwnerEmail: "[email protected]",
Shell: true,
}
sched.Restart([]*Job{testJob2})

if sched.Started != true {
t.Fatal("The scheduler should be started.")
}

if len(sched.Cron.Entries()) > 1 {
t.Fatal("The scheduler has more jobs than expected.")
}
assert.Equal(t, true, sched.Started)
assert.Len(t, sched.Cron.Entries(), 1, "The scheduler has more jobs than expected.")
}

0 comments on commit f7ad64b

Please sign in to comment.