Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PublishToLocal for health check #686

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sljeff
Copy link

@sljeff sljeff commented Jun 21, 2021

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
Copy link
Author

sljeff commented Jul 7, 2021

@RichardKnop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant