Merge pull request #262 from reasonerjt/job-service

api will handle replication job for a single repo
This commit is contained in:
Daniel Jiang 2016-05-24 13:24:22 +08:00
commit ea8c5bb251
4 changed files with 46 additions and 20 deletions

View File

@ -22,7 +22,9 @@ type ReplicationJob struct {
} }
type ReplicationReq struct { type ReplicationReq struct {
PolicyID int64 `json:"policy_id"` PolicyID int64 `json:"policy_id"`
Repo string `json:"repository"`
Operation string `json:"operation"`
} }
func (rj *ReplicationJob) Post() { func (rj *ReplicationJob) Post() {
@ -40,31 +42,54 @@ func (rj *ReplicationJob) Post() {
rj.RenderError(http.StatusNotFound, fmt.Sprintf("Policy not found, id: %d", data.PolicyID)) rj.RenderError(http.StatusNotFound, fmt.Sprintf("Policy not found, id: %d", data.PolicyID))
return return
} }
repoList, err := getRepoList(p.ProjectID) if len(data.Repo) == 0 { // sync all repositories
if err != nil { repoList, err := getRepoList(p.ProjectID)
log.Errorf("Failed to get repository list, project id: %d, error: %v", p.ProjectID, err) if err != nil {
rj.RenderError(http.StatusInternalServerError, err.Error()) log.Errorf("Failed to get repository list, project id: %d, error: %v", p.ProjectID, err)
return rj.RenderError(http.StatusInternalServerError, err.Error())
} return
log.Debugf("repo list: %v", repoList)
for _, repo := range repoList {
j := models.RepJob{
Repository: repo,
PolicyID: data.PolicyID,
Operation: models.RepOpTransfer,
} }
log.Debugf("Creating job for repo: %s, policy: %d", repo, data.PolicyID) log.Debugf("repo list: %v", repoList)
id, err := dao.AddRepJob(j) for _, repo := range repoList {
err := rj.addJob(repo, data.PolicyID, models.RepOpTransfer)
if err != nil {
log.Errorf("Failed to insert job record, error: %v", err)
rj.RenderError(http.StatusInternalServerError, err.Error())
return
}
}
} else { // sync a single repository
var op string
if len(data.Operation) > 0 {
op = data.Operation
} else {
op = models.RepOpTransfer
}
err := rj.addJob(data.Repo, data.PolicyID, op)
if err != nil { if err != nil {
log.Errorf("Failed to insert job record, error: %v", err) log.Errorf("Failed to insert job record, error: %v", err)
rj.RenderError(http.StatusInternalServerError, err.Error()) rj.RenderError(http.StatusInternalServerError, err.Error())
return return
} }
log.Debugf("Send job to scheduler, job id: %d", id)
job.Schedule(id)
} }
} }
func (rj *ReplicationJob) addJob(repo string, policyID int64, operation string) error {
j := models.RepJob{
Repository: repo,
PolicyID: policyID,
Operation: operation,
}
log.Debugf("Creating job for repo: %s, policy: %d", repo, policyID)
id, err := dao.AddRepJob(j)
if err != nil {
return err
}
log.Debugf("Send job to scheduler, job id: %d", id)
job.Schedule(id)
return nil
}
type RepActionReq struct { type RepActionReq struct {
PolicyID int64 `json:"policy_id"` PolicyID int64 `json:"policy_id"`
Action string `json:"action"` Action string `json:"action"`

View File

@ -7,7 +7,7 @@ import (
) )
func initRouters() { func initRouters() {
beego.Router("/api/jobs/replication", &api.ReplicationJob{}) beego.Router("/api/replicationJobs", &api.ReplicationJob{})
beego.Router("/api/jobs/replication/:id/log", &api.ReplicationJob{}, "get:GetLog") beego.Router("/api/replicationJobs/:id/log", &api.ReplicationJob{}, "get:GetLog")
beego.Router("/api/jobs/replication/actions", &api.ReplicationJob{}, "post:HandleAction") beego.Router("/api/replicationJobs/actions", &api.ReplicationJob{}, "post:HandleAction")
} }

View File

@ -0,0 +1 @@
{"policy_id": 1, "repository":"library/ubuntu"}