forked from albrow/jobs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit. Can register job types.
- Loading branch information
0 parents
commit eefdbad
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package zazu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package zazu | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// A set of all existing names for job types | ||
var jobTypes = map[string]*JobType{} | ||
|
||
type JobType struct { | ||
name string | ||
} | ||
|
||
type ErrorNameAlreadyRegistered struct { | ||
name string | ||
} | ||
|
||
func (e ErrorNameAlreadyRegistered) Error() string { | ||
return fmt.Sprintf("zazu: Cannot register job type because job type with name %s already exists", e.name) | ||
} | ||
|
||
func NewErrorNameAlreadyRegistered(name string) ErrorNameAlreadyRegistered { | ||
return ErrorNameAlreadyRegistered{name: name} | ||
} | ||
|
||
func RegisterJobType(name string) (*JobType, error) { | ||
if _, found := jobTypes[name]; found { | ||
return jobTypes[name], NewErrorNameAlreadyRegistered(name) | ||
} | ||
jobType := &JobType{name: name} | ||
jobTypes[name] = jobType | ||
return jobType, nil | ||
} | ||
|
||
func (j *JobType) String() string { | ||
return j.name | ||
} | ||
|
||
func (j *JobType) Do(data interface{}, when time.Timer) { | ||
|
||
} | ||
|
||
func (j *JobType) DoNow(data interface{}) { | ||
|
||
} | ||
|
||
func (j *JobType) Repeat(data interface{}, frequency time.Duration) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package zazu | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestRegisterJobType(t *testing.T) { | ||
// Make sure we can register a job type without error | ||
testJobName := "testJob1" | ||
jobType, err := RegisterJobType(testJobName) | ||
if err != nil { | ||
t.Errorf("Unexpected err registering job type: %s", err.Error()) | ||
} | ||
// Make sure the name property is correct | ||
if jobType.name != testJobName { | ||
t.Errorf("Got wrong name for job type. Expected %s but got %s", testJobName, jobType.name) | ||
} | ||
// Make sure we cannot register a job type with the same name | ||
if _, err := RegisterJobType(testJobName); err == nil { | ||
t.Errorf("Expected error when registering job with the same name but got none") | ||
} else if _, ok := err.(ErrorNameAlreadyRegistered); !ok { | ||
t.Errorf("Expected ErrorNameAlreadyRegistered but got error of type %T", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package zazu |