|
4 | 4 | "archive/zip"
|
5 | 5 | "bufio"
|
6 | 6 | "bytes"
|
| 7 | + gocontext "context" |
7 | 8 | "crypto/sha1"
|
8 | 9 | "crypto/sha256"
|
9 | 10 | "debug/elf"
|
@@ -273,7 +274,13 @@ func (n *Netlify) DoDeploy(ctx context.Context, options *DeployOptions, deploy *
|
273 | 274 |
|
274 | 275 | if n.overCommitted(options.files) {
|
275 | 276 | var err error
|
276 |
| - deploy, err = n.WaitUntilDeployReady(ctx, deploy, options.PreProcessTimeout) |
| 277 | + |
| 278 | + timeout := options.PreProcessTimeout |
| 279 | + if timeout <= 0 { |
| 280 | + timeout = preProcessingTimeout |
| 281 | + } |
| 282 | + deployReadyCtx, _ := gocontext.WithTimeout(ctx, timeout) |
| 283 | + deploy, err = n.WaitUntilDeployReady(deployReadyCtx, deploy) |
277 | 284 | if err != nil {
|
278 | 285 | if options.Observer != nil {
|
279 | 286 | options.Observer.OnFailedDelta(deployFiles)
|
@@ -305,58 +312,48 @@ func (n *Netlify) DoDeploy(ctx context.Context, options *DeployOptions, deploy *
|
305 | 312 | return deploy, nil
|
306 | 313 | }
|
307 | 314 |
|
308 |
| -func (n *Netlify) waitForState(ctx context.Context, d *models.Deploy, timeout time.Duration, states ...string) (*models.Deploy, error) { |
| 315 | +func (n *Netlify) waitForState(ctx context.Context, d *models.Deploy, states ...string) (*models.Deploy, error) { |
309 | 316 | authInfo := context.GetAuthInfo(ctx)
|
310 | 317 | ticker := time.NewTicker(2 * time.Second)
|
311 | 318 | defer ticker.Stop()
|
312 | 319 |
|
313 | 320 | params := operations.NewGetSiteDeployParams().WithSiteID(d.SiteID).WithDeployID(d.ID)
|
314 |
| - start := time.Now() |
315 |
| - for t := range ticker.C { |
316 |
| - resp, err := n.Operations.GetSiteDeploy(params, authInfo) |
317 |
| - if err != nil { |
318 |
| - time.Sleep(3 * time.Second) |
319 |
| - continue |
320 |
| - } |
321 |
| - context.GetLogger(ctx).WithFields(logrus.Fields{ |
322 |
| - "deploy_id": d.ID, |
323 |
| - "state": resp.Payload.State, |
324 |
| - }).Debugf("Waiting until deploy state in %s", states) |
325 |
| - |
326 |
| - for _, state := range states { |
327 |
| - if resp.Payload.State == state { |
328 |
| - return resp.Payload, nil |
| 321 | + for { |
| 322 | + select { |
| 323 | + case <-ctx.Done(): |
| 324 | + return nil, fmt.Errorf("timed out while waiting to enter states [%s]", strings.Join(states, ", ")) |
| 325 | + case <-ticker.C: |
| 326 | + resp, err := n.Operations.GetSiteDeploy(params, authInfo) |
| 327 | + if err != nil { |
| 328 | + time.Sleep(3 * time.Second) |
| 329 | + continue |
| 330 | + } |
| 331 | + context.GetLogger(ctx).WithFields(logrus.Fields{ |
| 332 | + "deploy_id": d.ID, |
| 333 | + "state": resp.Payload.State, |
| 334 | + }).Debugf("Waiting until deploy state in %s", states) |
| 335 | + |
| 336 | + for _, state := range states { |
| 337 | + if resp.Payload.State == state { |
| 338 | + return resp.Payload, nil |
| 339 | + } |
329 | 340 | }
|
330 |
| - } |
331 |
| - |
332 |
| - if resp.Payload.State == "error" { |
333 |
| - return nil, fmt.Errorf("Error: entered error state while waiting to enter states: %s", strings.Join(states, ", ")) |
334 |
| - } |
335 | 341 |
|
336 |
| - if t.Sub(start) > timeout { |
337 |
| - return nil, fmt.Errorf("Error: deploy timed out while waiting to enter states: %s", strings.Join(states, ", ")) |
| 342 | + if resp.Payload.State == "error" { |
| 343 | + return nil, fmt.Errorf("entered error state while waiting to enter states [%s]", strings.Join(states, ", ")) |
| 344 | + } |
338 | 345 | }
|
339 | 346 | }
|
340 |
| - |
341 |
| - return d, nil |
342 | 347 | }
|
343 | 348 |
|
344 | 349 | // WaitUntilDeployReady blocks until the deploy is in the "prepared" or "ready" state.
|
345 |
| -func (n *Netlify) WaitUntilDeployReady(ctx context.Context, d *models.Deploy, timeout time.Duration) (*models.Deploy, error) { |
346 |
| - if timeout <= 0 { |
347 |
| - timeout = preProcessingTimeout |
348 |
| - } |
349 |
| - |
350 |
| - return n.waitForState(ctx, d, timeout, "prepared", "ready") |
| 350 | +func (n *Netlify) WaitUntilDeployReady(ctx context.Context, d *models.Deploy) (*models.Deploy, error) { |
| 351 | + return n.waitForState(ctx, d, "prepared", "ready") |
351 | 352 | }
|
352 | 353 |
|
353 | 354 | // WaitUntilDeployLive blocks until the deploy is in the or "ready" state. At this point, the deploy is ready to recieve traffic.
|
354 |
| -func (n *Netlify) WaitUntilDeployLive(ctx context.Context, d *models.Deploy, timeout time.Duration) (*models.Deploy, error) { |
355 |
| - if timeout <= 0 { |
356 |
| - timeout = preProcessingTimeout |
357 |
| - } |
358 |
| - |
359 |
| - return n.waitForState(ctx, d, timeout, "ready") |
| 355 | +func (n *Netlify) WaitUntilDeployLive(ctx context.Context, d *models.Deploy) (*models.Deploy, error) { |
| 356 | + return n.waitForState(ctx, d, "ready") |
360 | 357 | }
|
361 | 358 |
|
362 | 359 | func (n *Netlify) uploadFiles(ctx context.Context, d *models.Deploy, files *deployFiles, observer DeployObserver, t uploadType, timeout time.Duration) error {
|
|
0 commit comments