Skip to content

Commit ebedc63

Browse files
committed
Add waiters in an effort to fix #32
1 parent 8cac573 commit ebedc63

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Changelog
22
## Unreleased
33

4+
## 0.3.4 - 2022-01-02
5+
6+
### Fixed
7+
8+
- Add method to ensure function is updated before we try to do anything else. Should hopefully fix [#32](https://github.com/hammerstonedev/sidecar/issues/32)
9+
10+
411
## 0.3.3 - 2021-11-01
512

613
### Added

src/Clients/LambdaClient.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Aws\Lambda\LambdaClient as BaseClient;
1010
use Exception;
1111
use Hammerstone\Sidecar\LambdaFunction;
12+
use Hammerstone\Sidecar\Sidecar;
1213
use Illuminate\Support\Arr;
1314
use Illuminate\Support\Str;
1415

@@ -28,7 +29,7 @@ public function getLatestVersion(LambdaFunction $function)
2829
}
2930

3031
/**
31-
* Test whether or not the latest deployed version is the one that is aliased.
32+
* Test whether the latest deployed version is the one that is aliased.
3233
*
3334
* @param LambdaFunction $function
3435
* @param $alias
@@ -77,7 +78,7 @@ public function aliasVersion(LambdaFunction $function, $alias, $version = null)
7778

7879
$aliased = $this->getAliasWithoutException($function, $alias);
7980

80-
// The alias already exists and its the version we were trying to alias anyway.
81+
// The alias already exists and it's the version we were trying to alias anyway.
8182
if ($aliased && $version === Arr::get($aliased, 'FunctionVersion')) {
8283
return self::NOOP;
8384
}
@@ -158,14 +159,28 @@ public function updateExistingFunction(LambdaFunction $function)
158159

159160
$config = Arr::except($config, ['Code', 'Publish']);
160161

162+
$this->waitUntilFunctionUpdated($function);
161163
$this->updateFunctionConfiguration($config);
162164

163-
retry(3, function () use ($code) {
164-
$this->updateFunctionCode($code);
165-
}, 2500, function ($exception) {
166-
return $exception instanceof LambdaException
167-
&& $exception->getStatusCode() === 409;
168-
});
165+
$this->waitUntilFunctionUpdated($function);
166+
$this->updateFunctionCode($code);
167+
}
168+
169+
/**
170+
* Wait until the function is out of the Pending state, so that
171+
* we don't get 409 conflict errors.
172+
*
173+
* @link https://aws.amazon.com/de/blogs/compute/coming-soon-expansion-of-aws-lambda-states-to-all-functions/
174+
* @link https://aws.amazon.com/blogs/compute/tracking-the-state-of-lambda-functions/
175+
* @link https://github.com/hammerstonedev/sidecar/issues/32
176+
* @link https://github.com/aws/aws-sdk-php/blob/master/src/data/lambda/2015-03-31/waiters-2.json
177+
* @param LambdaFunction $function
178+
*/
179+
public function waitUntilFunctionUpdated(LambdaFunction $function)
180+
{
181+
$this->waitUntil('FunctionUpdated', [
182+
'FunctionName' => $function->nameWithPrefix(),
183+
]);
169184
}
170185

171186
/**

tests/Unit/LambdaClientTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ public function update_existing_function()
197197
'Publish' => 'test-Publish',
198198
]);
199199

200+
$this->lambda->shouldReceive('waitUntil')
201+
->twice()
202+
->with('FunctionUpdated', [
203+
'FunctionName' => 'test-FunctionName',
204+
]);
205+
200206
$this->lambda->updateExistingFunction($function);
201207
}
202208

@@ -229,6 +235,12 @@ public function update_existing_image_function()
229235
'ImageUri' => '123.dkr.ecr.us-west-2.amazonaws.com/image:latest',
230236
]);
231237

238+
$this->lambda->shouldReceive('waitUntil')
239+
->twice()
240+
->with('FunctionUpdated', [
241+
'FunctionName' => 'test-FunctionName',
242+
]);
243+
232244
$this->lambda->updateExistingFunction($function);
233245
}
234246

0 commit comments

Comments
 (0)