-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remote support #139
Remote support #139
Changes from all commits
51645c3
045c71a
85579ee
8913b82
5c9bcb3
d81076c
d097aef
9ab60ca
c8e911c
9b985c5
c27d2cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"encoding/gob" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
|
@@ -13,23 +14,17 @@ import ( | |
"github.com/ajvb/kala/utils/iso8601" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/mattn/go-shellwords" | ||
"github.com/nu7hatch/gouuid" | ||
) | ||
|
||
var ( | ||
shParser = shellwords.NewParser() | ||
|
||
RFC3339WithoutTimezone = "2006-01-02T15:04:05" | ||
|
||
ErrInvalidJob = errors.New("Invalid Job. Job's must contain a Name and a Command field") | ||
ErrInvalidJob = errors.New("Invalid Local Job. Job's must contain a Name and a Command field") | ||
ErrInvalidRemoteJob = errors.New("Invalid Remote Job. Job's must contain a Name and a url field") | ||
ErrInvalidJobType = errors.New("Invalid Job type. Types supported: 0 for local and 1 for remote") | ||
) | ||
|
||
func init() { | ||
shParser.ParseEnv = true | ||
shParser.ParseBacktick = true | ||
} | ||
|
||
type Job struct { | ||
Name string `json:"name"` | ||
Id string `json:"id"` | ||
|
@@ -76,6 +71,12 @@ type Job struct { | |
// Meta data about successful and failed runs. | ||
Metadata Metadata `json:"metadata"` | ||
|
||
// Type of the job | ||
JobType jobType `json:"type"` | ||
|
||
// Custom properties for the remote job type | ||
RemoteProperties RemoteProperties `json:"remote_properties"` | ||
|
||
// Collection of Job Stats | ||
Stats []*JobStat `json:"stats"` | ||
|
||
|
@@ -86,6 +87,31 @@ type Job struct { | |
IsDone bool `json:"is_done"` | ||
} | ||
|
||
type jobType int | ||
|
||
const ( | ||
LocalJob jobType = iota | ||
RemoteJob | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a comment describing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @levrado Good idea, but you should be able to just say |
||
// RemoteProperties Custom properties for the remote job type | ||
type RemoteProperties struct { | ||
Url string `json:"url"` | ||
Method string `json:"method"` | ||
|
||
// A body to attach to the http request | ||
Body string `json:"body"` | ||
|
||
// A list of headers to add to http request (e.g. [{"key": "charset", "value": "UTF-8"}]) | ||
Headers http.Header `json:"headers"` | ||
|
||
// A timeout property for the http request in seconds | ||
Timeout int `json:"timeout"` | ||
|
||
// A list of expected response codes (e.g. [200, 201]) | ||
ExpectedResponseCodes []int `json:"expected_response_codes"` | ||
} | ||
|
||
type Metadata struct { | ||
SuccessCount uint `json:"success_count"` | ||
LastSuccess time.Time `json:"last_success"` | ||
|
@@ -124,11 +150,10 @@ func (j *Job) Init(cache JobCache) error { | |
j.lock.Lock() | ||
defer j.lock.Unlock() | ||
|
||
// Job Validation | ||
// TODO: Move this to a seperated method? | ||
if j.Name == "" || j.Command == "" { | ||
log.Errorf(ErrInvalidJob.Error()) | ||
return ErrInvalidJob | ||
//validate job type and params | ||
err := j.validation() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
u4, err := uuid.NewV4() | ||
|
@@ -432,3 +457,18 @@ func (j *Job) ShouldStartWaiting() bool { | |
} | ||
return true | ||
} | ||
|
||
func (j *Job) validation() error { | ||
var err error | ||
if j.JobType == LocalJob && (j.Name == "" || j.Command == "") { | ||
err = ErrInvalidJob | ||
} else if j.JobType == RemoteJob && (j.Name == "" || j.RemoteProperties.Url == "") { | ||
err = ErrInvalidRemoteJob | ||
} else if j.JobType != LocalJob && j.JobType != RemoteJob { | ||
err = ErrInvalidJobType | ||
} else { | ||
return nil | ||
} | ||
log.Errorf(err.Error()) | ||
return err | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reduce code duplication by turning
into
then finish with
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (and use that same trick for every |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apply
gofmt
to this