<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Running a task once per instance in HPE Morpheus Enterprise Software</title>
    <link>https://community.hpe.com/t5/hpe-morpheus-enterprise-software/running-a-task-once-per-instance/m-p/7248186#M1360</link>
    <description>&lt;P&gt;Oh that’s a cool trick!  In the past I’ve taken the array of server objects, sorted and compared current system to the list to see if it was the last/latest server to be provisioned to execute the code.  I never thought about storing that data a bit more persistent.&lt;/P&gt;</description>
    <pubDate>Mon, 11 Apr 2022 21:34:57 GMT</pubDate>
    <dc:creator>cbunge</dc:creator>
    <dc:date>2022-04-11T21:34:57Z</dc:date>
    <item>
      <title>Running a task once per instance</title>
      <link>https://community.hpe.com/t5/hpe-morpheus-enterprise-software/running-a-task-once-per-instance/m-p/7248185#M1359</link>
      <description>&lt;P&gt;Just sharing a snippet of code.&lt;/P&gt;
&lt;P&gt;Use case: Usually we have two instances (load balanced) when provisioning applications. We have a task that needs to run once at the end of provisioning the last instance and takes ages to complete.&lt;/P&gt;
&lt;P&gt;Adding this snippet in the provisioning workflow will fire the task at the last running instance. It stores the serverIds on which it has ran in Cypher and cleans up once completed.&lt;/P&gt;
&lt;PRE style="background : #f0f1f2;"&gt;&lt;CODE class="lang-auto"&gt;from pymorpheus import MorpheusClient
import json
  
cypherKey = "secret/" + morpheus["instance"]["name"]
morpheusclient = MorpheusClient(morpheus['morpheus']['applianceUrl'], token=morpheus['morpheus']['apiAccessToken'], sslverify=False)
 
results = morpheusclient.call("get", path="/cypher/%s" % cypherKey)
  
if 'data' in results:
    instances = json.loads(results["data"])
else:
    instances = []
 
if len(instances) &amp;gt;= len(morpheus["instance"]["containers"]) - 1:
    print("Fire!")
    morpheusclient.call("delete", path="/cypher/%s" % cypherKey)
else:
    instances.append(morpheus["server"]["id"])    
    morpheusclient.call("post", path="/cypher/%s" % cypherKey, options=[("type", "string"), ("value", json.dumps(instances))])
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Apr 2022 21:30:46 GMT</pubDate>
      <guid>https://community.hpe.com/t5/hpe-morpheus-enterprise-software/running-a-task-once-per-instance/m-p/7248185#M1359</guid>
      <dc:creator />
      <dc:date>2022-04-11T21:30:46Z</dc:date>
    </item>
    <item>
      <title>Re: Running a task once per instance</title>
      <link>https://community.hpe.com/t5/hpe-morpheus-enterprise-software/running-a-task-once-per-instance/m-p/7248186#M1360</link>
      <description>&lt;P&gt;Oh that’s a cool trick!  In the past I’ve taken the array of server objects, sorted and compared current system to the list to see if it was the last/latest server to be provisioned to execute the code.  I never thought about storing that data a bit more persistent.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2022 21:34:57 GMT</pubDate>
      <guid>https://community.hpe.com/t5/hpe-morpheus-enterprise-software/running-a-task-once-per-instance/m-p/7248186#M1360</guid>
      <dc:creator>cbunge</dc:creator>
      <dc:date>2022-04-11T21:34:57Z</dc:date>
    </item>
  </channel>
</rss>

