Question

I need to run multiple salt commands concurrently using salt-api on the master. The problem comes when I want to get the output in an async fashion.

Let's say I have the following code (more like pseudo-) in the runner (just for the sake of example):

client = salt.client.LocalClient()
for fun in funs:
    jid = client.cmd_async(target, fun, [arg])
    jobs.append(jid)

out = {}
while len(jobs):
    for jid in jobs:
        # this can be any function that can in some way assure me about the state
        # whether the command it's still running or it finished the job
        state = get_salt_cmd_state(jid)

        # checking if that command is really finished
        # in order to get it's output
        if state == FINISHED:
            out[jid] = get_salt_cmd_output(jid)
            jobs.remove(jid)

Looking into salt, on github (salt/client/init.py), you can find some methods for doing this, but most of them are at least partially blocking and require also the list of minions as an argument.

Is there a nice way to make this work?

Was it helpful?

Solution

I think what you're looking for is the get_cache_returns() method in salt/client/__init__.py. It looks like it doesn't block at all, but returns the contents of the job cache for the given jid. If it returns an empty dict, then that job is not complete, if I'm following the code correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top