Running a task once per instance

Just sharing a snippet of code.

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.

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.

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) >= 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))])
5 Likes

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.