Bug 1557310 - A state machine's on_exit method runs before the main method if the main method is an embedded Ansible playbook
Summary: A state machine's on_exit method runs before the main method if the main meth...
Alias: None
Product: Red Hat CloudForms Management Engine
Classification: Red Hat
Component: Automate
Version: 5.9.0
Hardware: Unspecified
OS: Unspecified
Target Milestone: GA
: 5.10.0
Assignee: Tina Fitzgerald
QA Contact: Dmitry Misharov
Depends On:
Blocks: 1562782
TreeView+ depends on / blocked
Reported: 2018-03-16 12:11 UTC by Peter McGowan
Modified: 2019-02-11 14:00 UTC (History)
7 users (show)

Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
: 1562782 (view as bug list)
Last Closed: 2019-02-11 14:00:02 UTC
Category: ---
Cloudforms Team: ---
Target Upstream Version:

Attachments (Terms of Use)

Description Peter McGowan 2018-03-16 12:11:58 UTC
Description of problem:
A state machine's on_exit method runs before the main method if the main method is an embedded Ansible playbook. The on_exit does not wait for the playbook to complete (or even start).

Version-Release number of selected component (if applicable):

How reproducible:
Every time

Steps to Reproduce:
1. Create a one-state state machine that has a single-line on_entry method as follows:

$evm.log(:info, "In on_entry at #{Time.now}...")

... a single-line on_exit method as follows:

$evm.log(:info, "In on_exit at #{Time.now}...")

... and a main method that is an embedded Ansible playbook as follows:

- name: Print the time
  hosts: localhost
  - debug: msg="In Ansible method at {{ ansible_date_time.iso8601 }} "

2. Run the state machine from simulation

Actual results:

The lines written to automation.log are:

<AEMethod on_entry> In on_entry at 2018-03-16 11:48:10 +0000...
<AEMethod on_exit> In on_exit at 2018-03-16 11:48:11 +0000...

The output from the job is:

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "In Ansible method at 2018-03-16T11:48:45Z "

It can be seen that the playbook runs many seconds after on_exit has completed.

Expected results:
The on_exit method should run after the playbook has completed

Additional info:

Comment 2 mkanoor 2018-03-16 13:59:17 UTC
Hi Peter,
We had some discussions on this issue a while back.
If the on_exit is not called on a retry, you dont have the ability to process the retry.
Shouldn't the on_exit method be checking if its in retry and bailing and waiting for the task to finish. If we called it only when the step finishes then we miss out on the retry opportunity.

Comment 3 Peter McGowan 2018-03-16 14:32:42 UTC
Hi Madhu

I'm not sure I understand the context of retrying, wouldn't you always want the on_exit method to be called once the main method has finished, regardless of whether a retry was triggered? I'm thinking for example of generic 'update_status' methods such as:

update_status(status => 'Processed Preprocess') 

You'd only want this to run once the main method has completed.

Comment 4 Greg McCullough 2018-03-16 15:10:06 UTC
Tina - Please work with Madhu on this issue.

Comment 5 Peter McGowan 2018-03-19 09:40:52 UTC
Further testing has shown that the on_exit method is run just once with an ae_result of 'retry'. 

I added this single line to on_exit:

$evm.log(:info, "In on_exit, ae_result = #{$evm.root['ae_result']}")

This only runs once, and prints: 

'<AEMethod on_exit> In on_exit, ae_result = retry'

I believe that it should also run when the Ansible method has completed, and ae_result would be 'ok'?

Comment 6 William Fitzgerald 2018-03-20 15:18:46 UTC
This works using a catalog service.
Simulation does not handle retires.  Working as designed.


Comment 7 Tina Fitzgerald 2018-03-20 18:28:10 UTC
The state machine on_exit is still an issue. 

Peter had a second issue where it appears as though the retry wasn't working properly. This second issue has been resolved.

Comment 8 Tina Fitzgerald 2018-03-28 17:57:07 UTC
We're working on a fix now.

Comment 10 CFME Bot 2018-03-29 18:52:57 UTC
New commit detected on ManageIQ/manageiq-automation_engine/master:

commit b1e7e41be36166245dfcba7e8c33f3c2f2eae142
Author:     Tina Fitzgerald <tfitzger>
AuthorDate: Wed Mar 28 17:33:49 2018 -0400
Commit:     Tina Fitzgerald <tfitzger>
CommitDate: Wed Mar 28 17:33:49 2018 -0400

    Change the Ansible Playbook method ae_result value from retry to
    async_launch and change the state machine processing to skip the on_exit
     for the async_launch and change the value to retry.


 lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_playbook_method.rb | 2 +-
 lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_state_machine.rb | 16 +-
 spec/engine/miq_ae_state_machine_steps_spec.rb | 14 +
 spec/miq_ae_playbook_method_spec.rb | 6 +-
 4 files changed, 28 insertions(+), 10 deletions(-)

Comment 12 Dmitry Misharov 2018-07-30 13:32:49 UTC
Fixed and verified in on_exit is not called when the playbook was launched in a state machine.

Note You need to log in before you can comment on or make changes to this bug.