thin-edge / thin-edge.io

The open edge framework for lightweight IoT devices
https://thin-edge.io
Apache License 2.0
223 stars 55 forks source link

tedge-mapper-c8y is sending cloud operation status for sub workflow calls #3048

Closed reubenmiller closed 3 months ago

reubenmiller commented 4 months ago

Describe the bug

If a workflow triggers a sub operation, then the mapper is incorrectly reacting to command status changes and sending a cloud message to transition operation status.

For example, below shows the presence of the 501,c8y_Restart SmartREST 2.0 message which tries to transition a cloud operation from PENDING to EXECUTING, even though this operation does not exist and is associated to the firmware_update operation (e.g. the workflow that called the restart workflow).

[te/device/rpizero2-d83add42f121///cmd/restart/sub:firmware_update:c8y-mapper-192481] {"logPath":"/var/log/tedge/agent/workflow-firmware_update-c8y-mapper-192481.log","status":"executing"}
[c8y/s/us/rpi4-d83add90fe56:device:rpizero2-d83add42f121] 501,c8y_Restart

A sub-workflow can be identified by the sub: prefix in the command id, e.g. in the above example the thin-edge.io command id is sub:firmware_update:c8y-mapper-192481.

To Reproduce

  1. Add the following firmware_update workflow by executing the command below

    cat << EOT > /etc/tedge/operations/firmware_update.toml
    #:schema https://gist.githubusercontent.com/reubenmiller/4e28e8403fe0c54b7461ac7d1d6838c2/raw/56bb3339be8e251e5cbdade7a6cdb40aa9992126/tedge.workflow.json
    operation = "firmware_update"
    on_error = "failed"
    
    [init]
    action = "proceed"
    on_success = "scheduled"
    
    [scheduled]
    action = "proceed"
    on_success = "executing"
    
    [executing]
    script = "echo ${.payload.status}"
    on_success = "restart"
    
    [restart]
    operation = "restart"
    on_exec = "await_restart"
    
    [await_restart]
    action = "await-operation-completion"
    on_success = "restarted"
    on_error = { status = "failed", reason = "Failed to restart device" }
    
    [restarted]
    script = "echo ${.payload.status}"
    on_success = "successful"
    
    [successful]
    action = "cleanup"
    
    [failed]
    action = "cleanup"
    EOT
  2. Register support for the firmware_update command

    tedge mqtt pub -r "$(tedge config get mqtt.topic_root)/$(tedge config get mqtt.device_topic_id)/cmd/firmware_update" '{}'
  3. Ensure the firmware_update cloud handling is enabled on the tedge-mapper-c8y by running the following command:

    tedge config set c8y.enable.firmware_update true
  4. Start (or restart) the tedge-agent service

    sudo systemctl restart tedge-agent
  5. Subscribe to the mqtt messages

    tedge mqtt sub '#'   
  6. From Cumulocity IoT Device Management, create a firmware operation for the device

  7. Verify that the 501,c8y_Restart message is NOT sent (once the bug is fixed, it is expected that the message SHOULD NOT be sent)

Expected behavior

Screenshots

Environment (please complete the following information):

Additional context

gligorisaev commented 3 months ago

The bug is not reproducable anymore.