Merge pull request #306 from ywk253100/sync_image

try to stop replication when a policy is disabled
This commit is contained in:
Wenkai Yin 2016-06-02 18:21:35 +08:00
commit 5b7376f096
2 changed files with 52 additions and 6 deletions

View File

@ -122,5 +122,13 @@ func (pa *RepPolicyAPI) UpdateEnablement() {
log.Infof("replication of %d triggered", pa.policyID) log.Infof("replication of %d triggered", pa.policyID)
} }
}() }()
} else {
go func() {
if err := postReplicationAction(pa.policyID, "stop"); err != nil {
log.Errorf("failed to stop replication of %d: %v", pa.policyID, err)
} else {
log.Infof("try to stop replication of %d", pa.policyID)
}
}()
} }
} }

View File

@ -167,26 +167,64 @@ func TriggerReplicationByRepository(repository string, tags []string, operation
} }
} }
func postReplicationAction(policyID int64, acton string) error {
data := struct {
PolicyID int64 `json:"policy_id"`
Action string `json:"action"`
}{
PolicyID: policyID,
Action: acton,
}
b, err := json.Marshal(&data)
if err != nil {
return err
}
url := buildReplicationActionURL()
resp, err := http.DefaultClient.Post(url, "application/json", bytes.NewBuffer(b))
if err != nil {
return err
}
if resp.StatusCode == http.StatusOK {
return nil
}
defer resp.Body.Close()
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf("%d %s", resp.StatusCode, string(b))
}
func buildReplicationURL() string { func buildReplicationURL() string {
url := getJobServiceURL() url := getJobServiceURL()
url = strings.TrimSpace(url)
url = strings.TrimRight(url, "/")
return fmt.Sprintf("%s/api/jobs/replication", url) return fmt.Sprintf("%s/api/jobs/replication", url)
} }
func buildJobLogURL(jobID string) string { func buildJobLogURL(jobID string) string {
url := getJobServiceURL() url := getJobServiceURL()
url = strings.TrimSpace(url)
url = strings.TrimRight(url, "/")
return fmt.Sprintf("%s/api/jobs/replication/%s/log", url, jobID) return fmt.Sprintf("%s/api/jobs/replication/%s/log", url, jobID)
} }
func buildReplicationActionURL() string {
url := getJobServiceURL()
return fmt.Sprintf("%s/api/jobs/replication/actions", url)
}
func getJobServiceURL() string { func getJobServiceURL() string {
url := os.Getenv("JOB_SERVICE_URL") url := os.Getenv("JOB_SERVICE_URL")
url = strings.TrimSpace(url)
url = strings.TrimRight(url, "/")
if len(url) == 0 { if len(url) == 0 {
url = "http://jobservice" url = "http://jobservice"
} }
return url return url
} }