2

For example, I'm trying to create a method similar to doesObjectExist(), but for multiple files called doObjectsExist(). The problem is that I need it to return true if all files exist and false if even just one doesn't. Here's my current method.

public function doObjectsExist(...$objects) {
    $client = $this->client;
    $bucket = $this->bucket;
    $commands = [];
    foreach ($objects as $key) {
        $commands[] = $client->getCommand('HeadObject', [
            'Bucket' => $bucket,
            'Key'    => $key
        ]);
    }
    $pool = new CommandPool($client, $commands, [null,
        'rejected' => function (AwsException $reason, $iterKey, PromiseInterface $aggregatePromise) {
            return false;
        }
    ]);
    $promise = $pool->promise();
    try {
        $promise->wait();
        return true;
    }
    catch (AwsException $e) {
        return false;
    }
}
D-Marc
  • 2,937
  • 5
  • 20
  • 28

1 Answers1

0

Since you want the function to return false on any missing file, add a variable $missing = false after $commands. In your "rejected" callback function set $missing = true. After $promise->wait() return false if $missing equals true.

I would improve the rejected function to actually test $reason for what you need to detect.

[EDIT]

Here is an example that may help you understand CommandPool via example code with explanations.

Command Objects

John Hanley
  • 74,467
  • 6
  • 95
  • 159
  • Originally had a variable to keep track of missing, but it didn't wait to get set. Sucks that there's almost no info about this online. – D-Marc Oct 12 '17 at 03:44
  • I don't understand what you mean by "but it didn't wait to get set". – John Hanley Oct 12 '17 at 03:49
  • For instance, your variable name `$missing` does `$missing = true` inside one of the rejected CommandPools. If I do `if($missing)` immediately after `$promise->wait()` in the try/catch block, it ironically won't wait for `$missing` to be set to true. I hope I cleared that up. – D-Marc Oct 12 '17 at 03:53
  • Then there is something wrong with the code. $promise->wait() is blocking and should not return until all commands have completed (or failed). Put some debug code into your rejected callback and make sure that the timing is correct when you call wait(). Something is wrong. – John Hanley Oct 12 '17 at 03:58
  • What I have provided is the entire code (minus `$missing`). It really doesn't wait for some reason. Try it out for yourself. I've used promises plenty of time successfully. Using it in conjunction with CommandPools is what gives me trouble. I appreciate the help though. – D-Marc Oct 12 '17 at 04:09
  • When you say it does not wait ... are you sure that everything is succeeding then? My thinking here is that when you setup the commands, they have actually failed therefore calling wait() will do nothing (e.g. the promise has already failed). – John Hanley Oct 12 '17 at 04:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156513/discussion-between-d-marc-and-john-hanley). – D-Marc Oct 12 '17 at 04:19