RedHatInsights / yggdrasil

GNU General Public License v3.0
21 stars 37 forks source link

Added new command message: Cancel #122

Closed ahitacat closed 1 year ago

ahitacat commented 1 year ago

Proposal

Add a new control message of type command that can be used to cancel a work that is already in progress in a worker. yggd will receive this command and forward it to the appropriate worker. If this worker implements the cancellation feature it will stop or cancel its work. But it is up to the worker to implement this functionality.

This PR also provides an example of how the cancellation message can be implemented in the echo worker. It uses a map of synchronized channels to manage the current work, and to send the cancellation message to the goroutines that are handle it.

This commit adds:

How to test it

Follow the Quickstart steps to set the environment. With the following changes:

Terminal 2 Run the worker with the loop and sleep parameters, this will allow some time to launch the cancel message.

go run ./worker/echo --log-level trace --loop 10 --sleep 10s

Terminal 4 Run the hello world, that will send a new job to the worker.

echo "hello world"| \
go run ./cmd/yggctl generate data-message --directive echo - | \
pub -broker tcp://localhost:1883 -topic yggdrasil/$(hostname)/data/in

In the worker it will appear the id of that message:

2023/10/06 12:21:58 addr = echo
2023/10/06 12:21:58 id = 67c40762-2343-48ad-9bf1-23a9b98bdacb
2023/10/06 12:21:58 responseTo = 
2023/10/06 12:21:58 metadata = map[string]string{}
2023/10/06 12:21:58 data = [104 101 108 108 111 32 119 111 114 108 100 10]
2023/10/06 12:21:58 emitting event BEGIN

Use the id 67c40762-2343-48ad-9bf1-23a9b98bdacb to send the cancellation message:

echo '{"command":"cancel", "arguments":{"directive":"echo","messageID":"67c40762-2343-48ad-9bf1-23a9b98bdacb"}}' |\
go run ./cmd/yggctl generate control-message --type command - |\
pub -broker tcp://test.mosquitto.org:1883 -topic yggdrasil/$(hostname)/control/in
ahitacat commented 1 year ago

Summary of changes made from reviews:

ahitacat commented 1 year ago

Sumary of changes

Addressed all the change from @jirihnidek comments:

subpop commented 1 year ago

worker/echo/main.go has changed; could you resolve those conflicts and we'll see if we can revisit this PR again.

ahitacat commented 1 year ago

Changelog

ahitacat commented 1 year ago

@subpop @jirihnidek I have fixed the typos and include your suggestions :smile: