Skip to content

Commit 0d8168b

Browse files
author
Dario Varotto
committed
Version 1.0.24: wait_for_completion raises an exception on errors
* wait_for_completion was just checking for completion now if the job goes in ERROR this raises an exception so we don't wait forever * add support for the job cancellation endpoint (while a job is in the ENQUEUED state)
1 parent dd9d49d commit 0d8168b

File tree

6 files changed

+83
-5
lines changed

6 files changed

+83
-5
lines changed

ravenpackapi/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from ravenpackapi.utils.constants import JSON_AVAILABLE_FIELDS, ENTITY_TYPES
1515

1616
_VALID_METHODS = ('get', 'post', 'put', 'delete')
17-
VERSION = '1.0.23'
17+
VERSION = '1.0.24'
1818

1919
logger = logging.getLogger("ravenpack.core")
2020

ravenpackapi/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ class DataFileTimeout(Exception):
1818
pass
1919

2020

21+
class JobNotProcessing(Exception):
22+
def __init__(self, *args, **kwargs):
23+
status = kwargs.pop('status', None)
24+
super(JobNotProcessing, self).__init__(*args)
25+
self.status = status
26+
27+
2128
def api_method(func):
2229
@wraps(func)
2330
def decorated_func(instance, *args, **kwargs):

ravenpackapi/models/job.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import csv
21
import datetime
32
import logging
43
from time import sleep
54

65
import requests
76

8-
from ravenpackapi.exceptions import api_method, APIException, DataFileTimeout
7+
from ravenpackapi.exceptions import (api_method,
8+
APIException,
9+
DataFileTimeout,
10+
JobNotProcessing)
911
from ravenpackapi.util import to_curl, parse_csv_line
1012

1113
logger = logging.getLogger(__name__)
@@ -55,7 +57,20 @@ def get_status(self):
5557
"token": token,
5658
},
5759
)
58-
self._data.update(response.json())
60+
json = response.json()
61+
self._data.update(json)
62+
return json.get('status')
63+
64+
@api_method
65+
def cancel(self):
66+
token = self.token
67+
response = self.api.request(
68+
endpoint="/jobs/%s" % token,
69+
method='delete'
70+
)
71+
response.json()
72+
self._data.update({"status": 'cancelled'})
73+
return self.status
5974

6075
@api_method
6176
def wait_for_completion(self, timeout_seconds=None):
@@ -70,6 +85,9 @@ def wait_for_completion(self, timeout_seconds=None):
7085
while True:
7186
if self.is_ready:
7287
break
88+
if not self.is_processing:
89+
raise JobNotProcessing("The job went in an error state: %s" % self.status,
90+
status=self.status)
7391
sleep(self._FILE_AVAILABILIY_SECONDS_DELAY)
7492
try:
7593
self.get_status()

ravenpackapi/tests/test_encoding.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
class TestEncoding(object):
55
api = RPApi()
6+
ds = None
67

78
@classmethod
89
def setup_class(cls):
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import logging
2+
3+
import pytest
4+
5+
from ravenpackapi import RPApi, Dataset
6+
from ravenpackapi.exceptions import JobNotProcessing, APIException
7+
8+
logger = logging.getLogger(__name__)
9+
10+
11+
class TestJobCancellation(object):
12+
api = RPApi()
13+
ds = None
14+
15+
@classmethod
16+
def setup_class(cls):
17+
cls.ds = cls.api.create_dataset(
18+
Dataset(
19+
name='test_job_cancel',
20+
filters={
21+
"rp_entity_id": 'D8442A'
22+
},
23+
)
24+
)
25+
26+
def test_job_cancel(self):
27+
params = dict(
28+
start_date='2018-05-10 21:51', # we have an event here
29+
end_date='2018-05-10 21:52',
30+
)
31+
job = self.ds.request_datafile(
32+
**params
33+
)
34+
status = job.get_status()
35+
36+
try:
37+
job.cancel()
38+
except APIException as exception:
39+
# cancel raised an exception, means that we were already processing it
40+
assert status == 'processing'
41+
assert exception.response.status_code == 400
42+
else:
43+
assert status == 'enqueued'
44+
assert job.get_status() == 'cancelled'
45+
46+
assert job.is_processing is False
47+
with pytest.raises(JobNotProcessing):
48+
job.wait_for_completion()
49+
50+
@classmethod
51+
def teardown_class(cls):
52+
cls.ds.delete()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import setup, find_packages
22

3-
VERSION = '1.0.23'
3+
VERSION = '1.0.24'
44

55
with open('README.rst') as readme_file:
66
readme = readme_file.read()

0 commit comments

Comments
 (0)