Skip to content
This repository has been archived by the owner on May 12, 2020. It is now read-only.

Commit

Permalink
Improve task timeout handling
Browse files Browse the repository at this point in the history
Now the task timeout is controlled by the time argument passed
to the `wait()` method. This allows some tasks, such as db backups
can take longer to run.
  • Loading branch information
skwashd committed Feb 5, 2017
1 parent 278c43b commit b4ab7bf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions acapi/resources/backuplist.py
Expand Up @@ -14,12 +14,12 @@ def __init__(self, base_uri, auth, *args, **kwargs):
super(BackupList, self).__init__(base_uri, auth, *args, **kwargs)
self.fetch()

def create(self):
def create(self, timeout=3600):
"""Create a new backup."""
task_data = self.request(method='POST')

task = self.create_task(self.uri, task_data)
task.wait()
task.wait(timeout)

# For some reason Acquia encodes JSON as a string in a JSON object.
result = json.loads(task['result'])
Expand Down
25 changes: 17 additions & 8 deletions acapi/resources/task.py
@@ -1,11 +1,13 @@
"""Acquia API task queue resource."""

from datetime import datetime
import logging
import re
import requests_cache
import time

from datetime import datetime
from datetime import timedelta

from acapi.exceptions import AcquiaCloudTaskFailedException
from acapi.resources.acquiaresource import AcquiaResource

Expand Down Expand Up @@ -79,25 +81,32 @@ def pending(self):
task = self.request()

self.data = task
return None == task['completed']
return task['completed'] is None

def wait(self):
def wait(self, timeout=1800):
"""Wait for a task to finish executing.
Parameters
----------
timeout : int
The maximum number of seconds to wait for the task to complete.
Returns
-------
Task
Task object.
"""
# We wait a maximum of 30mins
max_loops = (60 * 30 / self.POLL_INTERVAL)
start = datetime.now()
max_time = start + timedelta(seconds=timeout)

while self.pending():
if self.loops >= max_loops:
break
# Ensure the timeout hasn't been exceeded.
if start >= max_time:
msg = 'Time out exceeded while waiting for {tid}' \
.format(tid=self.data['id'])
raise AcquiaCloudTaskFailedException(msg, self.data)

self.loops += 1
time.sleep(self.POLL_INTERVAL)

# Grab the cached response
Expand Down

0 comments on commit b4ab7bf

Please sign in to comment.