-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[jenson] Schedule procs for execution from proctord
- Loading branch information
Showing
11 changed files
with
336 additions
and
1 deletion.
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,85 @@ | ||
package schedule | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/gojektech/proctor/proctord/jobs/metadata" | ||
"github.com/gojektech/proctor/proctord/logger" | ||
"github.com/gojektech/proctor/proctord/storage" | ||
"github.com/gojektech/proctor/proctord/utility" | ||
) | ||
|
||
type scheduler struct { | ||
store storage.Store | ||
metadataStore metadata.Store | ||
} | ||
|
||
type Scheduler interface { | ||
Schedule() http.HandlerFunc | ||
} | ||
|
||
func NewScheduler(store storage.Store, metadataStore metadata.Store) Scheduler { | ||
return &scheduler{ | ||
metadataStore: metadataStore, | ||
store: store, | ||
} | ||
} | ||
|
||
func (scheduler *scheduler) Schedule() http.HandlerFunc { | ||
return func(w http.ResponseWriter, req *http.Request) { | ||
var scheduledJob ScheduledJob | ||
err := json.NewDecoder(req.Body).Decode(&scheduledJob) | ||
userEmail := req.Header.Get(utility.UserEmailHeaderKey) | ||
defer req.Body.Close() | ||
if err != nil { | ||
logger.Error("Error parsing request body for scheduling jobs: ", err.Error()) | ||
|
||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(utility.ClientError)) | ||
|
||
return | ||
} | ||
|
||
_, err = scheduler.metadataStore.GetJobMetadata(scheduledJob.Name) | ||
if err != nil { | ||
if err.Error() == "redigo: nil returned" { | ||
logger.Error("Client provided non existent proc name: ", scheduledJob.Name) | ||
|
||
w.WriteHeader(http.StatusNotFound) | ||
w.Write([]byte(utility.NonExistentProcClientError)) | ||
} else { | ||
logger.Error("Error fetching metadata for proc", err.Error()) | ||
|
||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(utility.ServerError)) | ||
} | ||
|
||
return | ||
} | ||
|
||
scheduledJob.ID, err = scheduler.store.InsertScheduledJob(scheduledJob.Name, scheduledJob.Tags, scheduledJob.Time, scheduledJob.NotificationEmails, userEmail, scheduledJob.Args) | ||
if err != nil { | ||
logger.Error("Error persisting scheduled job", err.Error()) | ||
|
||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(utility.ServerError)) | ||
|
||
return | ||
} | ||
|
||
responseBody, err := json.Marshal(scheduledJob) | ||
if err != nil { | ||
logger.Error("Error marshaling response body", err.Error()) | ||
|
||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(utility.ServerError)) | ||
|
||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusCreated) | ||
w.Write(responseBody) | ||
return | ||
} | ||
} |
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,145 @@ | ||
package schedule | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gojektech/proctor/proctord/jobs/metadata" | ||
"github.com/gojektech/proctor/proctord/storage" | ||
"github.com/gojektech/proctor/proctord/utility" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type SchedulerTestSuite struct { | ||
suite.Suite | ||
mockStore *storage.MockStore | ||
mockMetadataStore *metadata.MockStore | ||
|
||
testScheduler Scheduler | ||
} | ||
|
||
func (suite *SchedulerTestSuite) SetupTest() { | ||
suite.mockMetadataStore = &metadata.MockStore{} | ||
suite.mockStore = &storage.MockStore{} | ||
|
||
suite.testScheduler = NewScheduler(suite.mockStore, suite.mockMetadataStore) | ||
} | ||
|
||
func (suite *SchedulerTestSuite) TestSuccessfulJobScheduling() { | ||
t := suite.T() | ||
|
||
userEmail := "mrproctor@example.com" | ||
scheduledJob := ScheduledJob{ | ||
Name: "any-job", | ||
Args: map[string]string{}, | ||
Time: "* 2 * * *", | ||
NotificationEmails: "foo@bar.com,bar@foo.com", | ||
Tags: "tag-one,tag-two", | ||
} | ||
requestBody, err := json.Marshal(scheduledJob) | ||
assert.NoError(t, err) | ||
|
||
responseRecorder := httptest.NewRecorder() | ||
req := httptest.NewRequest("POST", "/schedule", bytes.NewReader(requestBody)) | ||
req.Header.Set(utility.UserEmailHeaderKey, userEmail) | ||
|
||
suite.mockMetadataStore.On("GetJobMetadata", scheduledJob.Name).Return(&metadata.Metadata{}, nil) | ||
insertedScheduledJobID := "123" | ||
suite.mockStore.On("InsertScheduledJob", scheduledJob.Name, scheduledJob.Tags, scheduledJob.Time, scheduledJob.NotificationEmails, userEmail, scheduledJob.Args).Return(insertedScheduledJobID, nil) | ||
|
||
suite.testScheduler.Schedule()(responseRecorder, req) | ||
|
||
assert.Equal(t, http.StatusCreated, responseRecorder.Code) | ||
|
||
expectedResponse := ScheduledJob{} | ||
err = json.NewDecoder(responseRecorder.Body).Decode(&expectedResponse) | ||
assert.NoError(t, err) | ||
assert.Equal(t, insertedScheduledJobID, expectedResponse.ID) | ||
} | ||
|
||
func (suite *SchedulerTestSuite) TestBadRequestWhenRequestBodyIsIncorrectForJobScheduling() { | ||
t := suite.T() | ||
|
||
req := httptest.NewRequest("POST", "/schedule", bytes.NewBuffer([]byte("invalid json"))) | ||
responseRecorder := httptest.NewRecorder() | ||
|
||
suite.testScheduler.Schedule()(responseRecorder, req) | ||
|
||
assert.Equal(t, http.StatusBadRequest, responseRecorder.Code) | ||
responseBody, _ := ioutil.ReadAll(responseRecorder.Body) | ||
assert.Equal(t, utility.ClientError, string(responseBody)) | ||
} | ||
|
||
func (suite *SchedulerTestSuite) TestNonExistentJobScheduling() { | ||
t := suite.T() | ||
|
||
scheduledJob := ScheduledJob{ | ||
Name: "non-existent", | ||
} | ||
requestBody, err := json.Marshal(scheduledJob) | ||
assert.NoError(t, err) | ||
|
||
responseRecorder := httptest.NewRecorder() | ||
req := httptest.NewRequest("POST", "/schedule", bytes.NewReader(requestBody)) | ||
|
||
suite.mockMetadataStore.On("GetJobMetadata", scheduledJob.Name).Return(&metadata.Metadata{}, errors.New("redigo: nil returned")) | ||
|
||
suite.testScheduler.Schedule()(responseRecorder, req) | ||
|
||
assert.Equal(t, http.StatusNotFound, responseRecorder.Code) | ||
responseBody, _ := ioutil.ReadAll(responseRecorder.Body) | ||
assert.Equal(t, utility.NonExistentProcClientError, string(responseBody)) | ||
} | ||
|
||
func (suite *SchedulerTestSuite) TestErrorFetchingJobMetadata() { | ||
t := suite.T() | ||
|
||
scheduledJob := ScheduledJob{ | ||
Name: "non-existent", | ||
} | ||
requestBody, err := json.Marshal(scheduledJob) | ||
assert.NoError(t, err) | ||
|
||
responseRecorder := httptest.NewRecorder() | ||
req := httptest.NewRequest("POST", "/schedule", bytes.NewReader(requestBody)) | ||
|
||
suite.mockMetadataStore.On("GetJobMetadata", scheduledJob.Name).Return(&metadata.Metadata{}, errors.New("any error")) | ||
|
||
suite.testScheduler.Schedule()(responseRecorder, req) | ||
|
||
assert.Equal(t, http.StatusInternalServerError, responseRecorder.Code) | ||
responseBody, _ := ioutil.ReadAll(responseRecorder.Body) | ||
assert.Equal(t, utility.ServerError, string(responseBody)) | ||
} | ||
|
||
func (suite *SchedulerTestSuite) TestErrorPersistingScheduledJob() { | ||
t := suite.T() | ||
|
||
scheduledJob := ScheduledJob{ | ||
Name: "non-existent", | ||
} | ||
requestBody, err := json.Marshal(scheduledJob) | ||
assert.NoError(t, err) | ||
|
||
responseRecorder := httptest.NewRecorder() | ||
req := httptest.NewRequest("POST", "/schedule", bytes.NewReader(requestBody)) | ||
|
||
suite.mockMetadataStore.On("GetJobMetadata", scheduledJob.Name).Return(&metadata.Metadata{}, nil) | ||
suite.mockStore.On("InsertScheduledJob", scheduledJob.Name, scheduledJob.Tags, scheduledJob.Time, scheduledJob.NotificationEmails, "", scheduledJob.Args).Return("", errors.New("any-error")) | ||
|
||
suite.testScheduler.Schedule()(responseRecorder, req) | ||
|
||
assert.Equal(t, http.StatusInternalServerError, responseRecorder.Code) | ||
responseBody, _ := ioutil.ReadAll(responseRecorder.Body) | ||
assert.Equal(t, utility.ServerError, string(responseBody)) | ||
} | ||
|
||
func TestScheduleTestSuite(t *testing.T) { | ||
suite.Run(t, new(SchedulerTestSuite)) | ||
} |
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,10 @@ | ||
package schedule | ||
|
||
type ScheduledJob struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Args map[string]string `json:"args"` | ||
NotificationEmails string `json:"notification_emails"` | ||
Time string `json:"time"` | ||
Tags string `json:"tags"` | ||
} |
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 @@ | ||
DROP TABLE IF EXISTS jobs_schedule; |
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,11 @@ | ||
CREATE TABLE jobs_schedule ( | ||
id uuid not null primary key, | ||
name text, | ||
args text, | ||
tags text, | ||
notification_emails text, | ||
time text, | ||
user_email text, | ||
created_at timestamp default now(), | ||
updated_at timestamp default now() | ||
); |
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
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
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
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
Oops, something went wrong.