Skip to content

Commit

Permalink
rename models.Job to models.JobSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
se3000 committed Mar 10, 2018
1 parent cb73ef8 commit 681cf0a
Show file tree
Hide file tree
Showing 22 changed files with 59 additions and 59 deletions.
2 changes: 1 addition & 1 deletion cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (cli *Client) GetJobs(c *clipkg.Context) error {
}
defer resp.Body.Close()

var jobs []models.Job
var jobs []models.JobSpec
return cli.deserializeResponse(resp, &jobs)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestClientGetJobs(t *testing.T) {
client, r := cltest.NewClientAndRenderer(app.Store.Config)

assert.Nil(t, client.GetJobs(nil))
jobs := *r.Renders[0].(*[]models.Job)
jobs := *r.Renders[0].(*[]models.JobSpec)
assert.Equal(t, 2, len(jobs))
assert.Equal(t, j1.ID, jobs[0].ID)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type RendererTable struct {
// and relevant information.
func (rt RendererTable) Render(v interface{}) error {
switch typed := v.(type) {
case *[]models.Job:
case *[]models.JobSpec:
rt.renderJobs(*typed)
case *presenters.Job:
rt.renderJob(*typed)
Expand All @@ -52,7 +52,7 @@ func (rt RendererTable) Render(v interface{}) error {
return nil
}

func (rt RendererTable) renderJobs(jobs []models.Job) error {
func (rt RendererTable) renderJobs(jobs []models.JobSpec) error {
table := tablewriter.NewWriter(rt)
table.SetHeader([]string{"ID", "Created At", "Initiators", "Tasks"})
for _, v := range jobs {
Expand All @@ -73,7 +73,7 @@ func render(name string, table *tablewriter.Table) {
table.Render()
}

func jobRowToStrings(job models.Job) []string {
func jobRowToStrings(job models.JobSpec) []string {
p := presenters.Job{job, nil}
return []string{
p.ID,
Expand Down
4 changes: 2 additions & 2 deletions cmd/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
func TestRendererJSONRenderJobs(t *testing.T) {
r := cmd.RendererJSON{ioutil.Discard}
job := cltest.NewJob()
jobs := []models.Job{job}
jobs := []models.JobSpec{job}
assert.Nil(t, r.Render(&jobs))
}

func TestRendererTableRenderJobs(t *testing.T) {
r := cmd.RendererTable{ioutil.Discard}
job := cltest.NewJob()
jobs := []models.Job{job}
jobs := []models.JobSpec{job}
assert.Nil(t, r.Render(&jobs))
}

Expand Down
6 changes: 3 additions & 3 deletions internal/cltest/cltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func ObserveLogs() *observer.ObservedLogs {
return observed
}

func FixtureCreateJobViaWeb(t *testing.T, app *TestApplication, path string) models.Job {
func FixtureCreateJobViaWeb(t *testing.T, app *TestApplication, path string) models.JobSpec {
resp := BasicAuthPost(
app.Server.URL+"/v2/jobs",
"application/json",
Expand All @@ -293,7 +293,7 @@ func FixtureCreateJobViaWeb(t *testing.T, app *TestApplication, path string) mod
return j
}

func CreateJobRunViaWeb(t *testing.T, app *TestApplication, j models.Job, body ...string) models.JobRun {
func CreateJobRunViaWeb(t *testing.T, app *TestApplication, j models.JobSpec, body ...string) models.JobRun {
t.Helper()
url := app.Server.URL + "/v2/jobs/" + j.ID + "/runs"
bodyBuffer := &bytes.Buffer{}
Expand Down Expand Up @@ -421,7 +421,7 @@ func StringToRunLogData(str string) hexutil.Bytes {
return hexutil.MustDecode(prefix + lenHex + data + endPad)
}

func WaitForRuns(t *testing.T, j models.Job, store *store.Store, want int) []models.JobRun {
func WaitForRuns(t *testing.T, j models.JobSpec, store *store.Store, want int) []models.JobRun {
t.Helper()
g := gomega.NewGomegaWithT(t)

Expand Down
8 changes: 4 additions & 4 deletions internal/cltest/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
null "gopkg.in/guregu/null.v3"
)

func NewJob() models.Job {
func NewJob() models.JobSpec {
j := models.NewJob()
j.Tasks = []models.Task{NewTask("NoOp")}
return j
Expand All @@ -40,19 +40,19 @@ func NewTask(taskType string, json ...string) models.Task {
}
}

func NewJobWithSchedule(sched string) models.Job {
func NewJobWithSchedule(sched string) models.JobSpec {
j := NewJob()
j.Initiators = []models.Initiator{{Type: models.InitiatorCron, Schedule: models.Cron(sched)}}
return j
}

func NewJobWithWebInitiator() models.Job {
func NewJobWithWebInitiator() models.JobSpec {
j := NewJob()
j.Initiators = []models.Initiator{{Type: models.InitiatorWeb}}
return j
}

func NewJobWithLogInitiator() models.Job {
func NewJobWithLogInitiator() models.JobSpec {
j := NewJob()
j.Initiators = []models.Initiator{{
Type: models.InitiatorEthLog,
Expand Down
2 changes: 1 addition & 1 deletion services/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (app *ChainlinkApplication) GetStore() *store.Store {
// AddJob adds a job to the store and the scheduler. If there was
// an error from adding the job to the store, the job will not be
// added to the scheduler.
func (app *ChainlinkApplication) AddJob(job models.Job) error {
func (app *ChainlinkApplication) AddJob(job models.JobSpec) error {
err := app.Store.SaveJob(&job)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions services/ethereum_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (el *EthereumListener) Stop() error {

// AddJob looks for "runlog" and "ethlog" Initiators for a given job
// and watches the Ethereum blockchain for the addresses in the job.
func (el *EthereumListener) AddJob(job models.Job) error {
func (el *EthereumListener) AddJob(job models.JobSpec) error {
if !job.IsLogInitiated() || !el.HeadTracker.IsConnected() {
return nil
}
Expand All @@ -53,8 +53,8 @@ func (el *EthereumListener) AddJob(job models.Job) error {
return nil
}

func (el *EthereumListener) Jobs() []models.Job {
var jobs []models.Job
func (el *EthereumListener) Jobs() []models.JobSpec {
var jobs []models.JobSpec
for _, js := range el.jobSubscriptions {
jobs = append(jobs, js.Job)
}
Expand Down
4 changes: 2 additions & 2 deletions services/job_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// BeginRun creates a new run if the job is valid and starts the job.
func BeginRun(job models.Job, store *store.Store, input models.RunResult) (models.JobRun, error) {
func BeginRun(job models.JobSpec, store *store.Store, input models.RunResult) (models.JobRun, error) {
run, err := BuildRun(job, store)
if err != nil {
return models.JobRun{}, err
Expand All @@ -22,7 +22,7 @@ func BeginRun(job models.Job, store *store.Store, input models.RunResult) (model

// BuildRun checks to ensure the given job has not started or ended before
// creating a new run for the job.
func BuildRun(job models.Job, store *store.Store) (models.JobRun, error) {
func BuildRun(job models.JobSpec, store *store.Store) (models.JobRun, error) {
now := store.Clock.Now()
if !job.Started(now) {
return models.JobRun{}, JobRunnerError{
Expand Down
8 changes: 4 additions & 4 deletions services/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (s *Scheduler) Stop() {

// AddJob is the governing function for Recurring and OneTime,
// and will only execute if the Scheduler has not already started.
func (s *Scheduler) AddJob(job models.Job) {
func (s *Scheduler) AddJob(job models.JobSpec) {
if !s.started {
return
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func (r *Recurring) Stop() {

// AddJob looks for "cron" initiators, adds them to cron's schedule
// for execution when specified.
func (r *Recurring) AddJob(job models.Job) {
func (r *Recurring) AddJob(job models.JobSpec) {
for _, initr := range job.InitiatorsFor(models.InitiatorCron) {
cronStr := string(initr.Schedule)
if !job.Ended(r.Clock.Now()) {
Expand Down Expand Up @@ -142,7 +142,7 @@ func (ot *OneTime) Start() error {
}

// AddJob runs the job at the time specified for the "runat" initiator.
func (ot *OneTime) AddJob(job models.Job) {
func (ot *OneTime) AddJob(job models.JobSpec) {
for _, initr := range job.InitiatorsFor(models.InitiatorRunAt) {
go ot.RunJobAt(initr.Time, job)
}
Expand All @@ -155,7 +155,7 @@ func (ot *OneTime) Stop() {

// RunJobAt wait until the Stop() function has been called on the run
// or the specified time for the run is after the present time.
func (ot *OneTime) RunJobAt(t models.Time, job models.Job) {
func (ot *OneTime) RunJobAt(t models.Time, job models.JobSpec) {
select {
case <-ot.done:
case <-ot.Clock.After(t.DurationFromNow()):
Expand Down
14 changes: 7 additions & 7 deletions services/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ var RunLogTopic = common.HexToHash("0x06f4bf36b4e011a5c499cef1113c2d166800ce4013

// Listens to event logs being pushed from the Ethereum Node specific to a job.
type JobSubscription struct {
Job models.Job
Job models.JobSpec
unsubscribers []Unsubscriber
}

// Constructor of JobSubscription that to starts listening to and keeps track of
// event logs corresponding to a job.
func StartJobSubscription(job models.Job, head *models.IndexableBlockNumber, store *store.Store) (JobSubscription, error) {
func StartJobSubscription(job models.JobSpec, head *models.IndexableBlockNumber, store *store.Store) (JobSubscription, error) {
var merr error
var initSubs []Unsubscriber
for _, initr := range job.InitiatorsFor(models.InitiatorEthLog) {
Expand Down Expand Up @@ -80,7 +80,7 @@ type Unsubscriber interface {
// for use with a Chainlink Initiator. Initiator specific functionality is delegated
// to the ReceiveLog callback using a strategy pattern.
type RPCLogSubscription struct {
Job models.Job
Job models.JobSpec
Initiator models.Initiator
ReceiveLog func(RPCLogEvent)
store *store.Store
Expand All @@ -92,7 +92,7 @@ type RPCLogSubscription struct {
// Create a new RPCLogSubscription that feeds received logs to the callback func parameter.
func NewRPCLogSubscription(
initr models.Initiator,
job models.Job,
job models.JobSpec,
head *models.IndexableBlockNumber,
store *store.Store,
callback func(RPCLogEvent),
Expand Down Expand Up @@ -140,12 +140,12 @@ func (sub RPCLogSubscription) listenToLogs() {
}

// Starts an RPCLogSubscription tailored for use with RunLogs.
func StartRunLogSubscription(initr models.Initiator, job models.Job, head *models.IndexableBlockNumber, store *store.Store) (Unsubscriber, error) {
func StartRunLogSubscription(initr models.Initiator, job models.JobSpec, head *models.IndexableBlockNumber, store *store.Store) (Unsubscriber, error) {
return NewRPCLogSubscription(initr, job, head, store, ReceiveRunLog)
}

// Starts an RPCLogSubscription tailored for use with EthLogs.
func StartEthLogSubscription(initr models.Initiator, job models.Job, head *models.IndexableBlockNumber, store *store.Store) (Unsubscriber, error) {
func StartEthLogSubscription(initr models.Initiator, job models.JobSpec, head *models.IndexableBlockNumber, store *store.Store) (Unsubscriber, error) {
return NewRPCLogSubscription(initr, job, head, store, ReceiveEthLog)
}

Expand Down Expand Up @@ -204,7 +204,7 @@ func runJob(le RPCLogEvent, data models.JSON) {
// RPCLogSubscription.
type RPCLogEvent struct {
Log types.Log
Job models.Job
Job models.JobSpec
Initiator models.Initiator
store *store.Store
}
Expand Down
6 changes: 3 additions & 3 deletions services/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// ValidateJob checks the job and its associated Initiators and Tasks for any
// application logic errors.
func ValidateJob(j models.Job, store *store.Store) error {
func ValidateJob(j models.JobSpec, store *store.Store) error {
var merr error
if j.StartAt.Valid && j.EndAt.Valid && j.StartAt.Time.After(j.EndAt.Time) {
merr = multierr.Append(merr, fmtJobError(errors.New("startat cannot be before endat")))
Expand All @@ -36,7 +36,7 @@ func fmtJobError(err error) error {
}

// ValidateInitiator checks the Initiator for any application logic errors.
func ValidateInitiator(i models.Initiator, j models.Job) error {
func ValidateInitiator(i models.Initiator, j models.JobSpec) error {
switch strings.ToLower(i.Type) {
case models.InitiatorRunAt:
return validateRunAtInitiator(i, j)
Expand All @@ -53,7 +53,7 @@ func ValidateInitiator(i models.Initiator, j models.Job) error {
}
}

func validateRunAtInitiator(i models.Initiator, j models.Job) error {
func validateRunAtInitiator(i models.Initiator, j models.JobSpec) error {
if i.Time.Unix() <= 0 {
return fmtInitiatorError(errors.New(`runat must have a time`))
} else if j.StartAt.Valid && i.Time.Unix() < j.StartAt.Time.Unix() {
Expand Down
2 changes: 1 addition & 1 deletion services/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestValidateJob(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var j models.Job
var j models.JobSpec
assert.Nil(t, json.Unmarshal(test.input, &j))
result := services.ValidateJob(j, store)
assert.Equal(t, test.want, result)
Expand Down
20 changes: 10 additions & 10 deletions store/models/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ const (
StatusCompleted = "completed"
)

// Job is the definition for all the work to be carried out by the node
// JobSpec is the definition for all the work to be carried out by the node
// for a given contract. It contains the Initiators, Tasks (which are the
// individual steps to be carried out), StartAt, EndAt, and CreatedAt fields.
type Job struct {
type JobSpec struct {
ID string `json:"id" storm:"id,unique"`
Initiators []Initiator `json:"initiators"`
Tasks []Task `json:"tasks" storm:"inline"`
Expand All @@ -37,16 +37,16 @@ type Job struct {

// NewJob initializes a new job by generating a unique ID and setting
// the CreatedAt field to the time of invokation.
func NewJob() Job {
return Job{
func NewJob() JobSpec {
return JobSpec{
ID: utils.NewBytes32ID(),
CreatedAt: Time{Time: time.Now()},
}
}

// NewRun initializes the job by creating the IDs for the job
// and all associated tasks, and setting the CreatedAt field.
func (j Job) NewRun() JobRun {
func (j JobSpec) NewRun() JobRun {
jrid := utils.NewBytes32ID()
taskRuns := make([]TaskRun, len(j.Tasks))
for i, task := range j.Tasks {
Expand All @@ -67,7 +67,7 @@ func (j Job) NewRun() JobRun {

// InitiatorsFor returns an array of Initiators for the given list of
// Initiator types.
func (j Job) InitiatorsFor(types ...string) []Initiator {
func (j JobSpec) InitiatorsFor(types ...string) []Initiator {
list := []Initiator{}
for _, initr := range j.Initiators {
for _, t := range types {
Expand All @@ -80,7 +80,7 @@ func (j Job) InitiatorsFor(types ...string) []Initiator {
}

// WebAuthorized returns true if the "web" initiator is present.
func (j Job) WebAuthorized() bool {
func (j JobSpec) WebAuthorized() bool {
for _, initr := range j.Initiators {
if initr.Type == InitiatorWeb {
return true
Expand All @@ -90,7 +90,7 @@ func (j Job) WebAuthorized() bool {
}

// Returns true if any of the job's initiators are triggered by event logs.
func (j Job) IsLogInitiated() bool {
func (j JobSpec) IsLogInitiated() bool {
for _, initr := range j.Initiators {
if initr.IsLogInitiated() {
return true
Expand All @@ -100,15 +100,15 @@ func (j Job) IsLogInitiated() bool {
}

// Ended returns true if the job has ended.
func (j Job) Ended(t time.Time) bool {
func (j JobSpec) Ended(t time.Time) bool {
if !j.EndAt.Valid {
return false
}
return t.After(j.EndAt.Time)
}

// Started returns true if the job has started.
func (j Job) Started(t time.Time) bool {
func (j JobSpec) Started(t time.Time) bool {
if !j.StartAt.Valid {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion store/models/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func (orm ORM) migrate() {
orm.initializeModel(&Job{})
orm.initializeModel(&JobSpec{})
orm.initializeModel(&JobRun{})
orm.initializeModel(&Initiator{})
orm.initializeModel(&Tx{})
Expand Down
Loading

0 comments on commit 681cf0a

Please sign in to comment.