Defering calling the de-register from consul call when a service is not running

This commit is contained in:
Diptanu Choudhury 2015-11-18 02:37:34 -08:00
parent cb34f34c12
commit b8c2cc81f0
2 changed files with 10 additions and 5 deletions

View file

@ -36,7 +36,7 @@ func NewConsulClient(logger *log.Logger) (*ConsulClient, error) {
func (c *ConsulClient) Register(task *structs.Task, allocID string) error {
var mErr multierror.Error
var serviceDefns []*api.AgentServiceRegistration
for idx, service := range task.Services {
for _, service := range task.Services {
service.Id = fmt.Sprintf("%s-%s", allocID, task.Name)
host, port := c.findPortAndHostForLabel(service.PortLabel, task)
if host == "" || port == 0 {
@ -49,11 +49,13 @@ func (c *ConsulClient) Register(task *structs.Task, allocID string) error {
Port: port,
Address: host,
}
serviceDefns[idx] = asr
serviceDefns = append(serviceDefns, asr)
}
for _, serviceDefn := range serviceDefns {
c.logger.Printf("[INFO] Registering service %v with Consul", serviceDefn.Name)
if err := c.client.Agent().ServiceRegister(serviceDefn); err != nil {
c.logger.Printf("[ERROR] Error while registering service %v with Consul: %v", serviceDefn.Name, err)
mErr.Errors = append(mErr.Errors, err)
}
}
@ -61,10 +63,12 @@ func (c *ConsulClient) Register(task *structs.Task, allocID string) error {
return mErr.ErrorOrNil()
}
func (c *ConsulClient) DeRegister(task *structs.Task) error {
func (c *ConsulClient) Deregister(task *structs.Task) error {
var mErr multierror.Error
for _, service := range task.Services {
c.logger.Printf("[INFO] De-Registering service %v with Consul", service.Name)
if err := c.client.Agent().ServiceDeregister(service.Id); err != nil {
c.logger.Printf("[ERROR] Error in de-registering service %v from Consul", service.Name)
mErr.Errors = append(mErr.Errors, err)
}
}

View file

@ -236,6 +236,9 @@ func (r *TaskRunner) run() {
// Register the services defined by the task with Consil
r.consulClient.Register(r.task, r.allocID)
// De-Register the services belonging to the task from consul
defer r.consulClient.Deregister(r.task)
OUTER:
// Wait for updates
for {
@ -309,8 +312,6 @@ func (r *TaskRunner) run() {
forceStart = true
}
// De-Register the services belonging to the task from consul
r.consulClient.DeRegister(r.task)
return
}