machinery icon indicating copy to clipboard operation
machinery copied to clipboard

add PublishToLocal for health check

Open sljeff opened this issue 3 years ago • 1 comments

I need a health check function to check if the local worker can still run properly (e.g. if all the goroutines are stuck and can't start new ones due to concurrency).

One way to do this is to check if the local worker can run and complete a new task in the expected time. So I need a PublishToLocal method.


A health check example:

var healthCheckCompleteChan = make(chan string, 1)

...
	server.RegisterTask(healthCheckTaskName, func(healthCheckUUID string) error {
		select {
		case healthCheckCompleteChan <- healthCheckUUID: // success and send uuid
			return nil
		case <-time.After(5 * time.Second):
			return fmt.Errorf("send health check result error: %v", healthCheckUUID)
		}
	})
...

func checkHealth(consumerTag string, taskExecutionTimeout time.Duration) error {
	// clear channel
	select {
	case <-healthCheckCompleteChan:
	default:
	}

	broker := server.GetBroker()
	healthCheckUUID, err := uuid.NewUUID()
	if err != nil {
		return err
	}
	if err := broker.PublishToLocal(consumerTag, &tasks.Signature{
		UUID: healthCheckUUID.String(),
		Name: healthCheckTaskName,
		Args: []tasks.Arg{
			{Type: "string", Value: healthCheckUUID.String()},
		},
	}, 5*time.Second); err != nil {
		return err
	}

	// wait for task execution success
	select {
	case successUUID := <-healthCheckCompleteChan:
		if successUUID == healthCheckUUID.String() {
			return nil
		}
	case <-time.After(taskExecutionTimeout):
	}
	return fmt.Errorf("health check execution fail: %v", healthCheckUUID.String())
}

Then I can run checkHealth(consumerTag, TasksShouldBeCompletedIn) method and do the appropriate processing.

sljeff avatar Jun 21 '21 11:06 sljeff

@RichardKnop

sljeff avatar Jul 07 '21 01:07 sljeff