From 10cf76a682b91085b1a35e38b7e8ca76b658015a Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Thu, 2 Jun 2016 18:17:00 +0800 Subject: [PATCH] try to stop replication when a policy is disabled --- api/replication_policy.go | 8 +++++++ api/utils.go | 50 ++++++++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/api/replication_policy.go b/api/replication_policy.go index 10cb67313..10906ba39 100644 --- a/api/replication_policy.go +++ b/api/replication_policy.go @@ -122,5 +122,13 @@ func (pa *RepPolicyAPI) UpdateEnablement() { 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) + } + }() } } diff --git a/api/utils.go b/api/utils.go index ef4919cf5..fead92469 100644 --- a/api/utils.go +++ b/api/utils.go @@ -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 { url := getJobServiceURL() - url = strings.TrimSpace(url) - url = strings.TrimRight(url, "/") - return fmt.Sprintf("%s/api/jobs/replication", url) } func buildJobLogURL(jobID string) string { url := getJobServiceURL() - url = strings.TrimSpace(url) - url = strings.TrimRight(url, "/") - 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 { url := os.Getenv("JOB_SERVICE_URL") + url = strings.TrimSpace(url) + url = strings.TrimRight(url, "/") + if len(url) == 0 { url = "http://jobservice" } + return url }