Skip to content
This repository was archived by the owner on May 25, 2023. It is now read-only.

Commit 21c5e2d

Browse files
committed
Changed configuration settings
-- Changed the structure of HOOK_DELIVERER_SETTINGS
1 parent 0c11a31 commit 21c5e2d

2 files changed

Lines changed: 72 additions & 33 deletions

File tree

README.rst

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ unmigrated application. You have to make sure that the `django rest hooks
5050
Make sure you have added :code:`rest_hooks_delivery` to the list of
5151
:code:`INSTALLED_APPS` before :code:`django.contrib.admin` and that you have
5252
set :code:`HOOK_DELIVERER` to one of the available deliverers. Currently
53-
:code:`rest_hooks_delivery.deliverers.batch` and
54-
:code:`rest_hooks_delivery.deliverers.retry` are available.
53+
:code:`rest_hooks_delivery.deliverers.retry` and
54+
:code:`rest_hooks_delivery.deliverers.batch` are available.
5555

5656
To use the retry deliverer:
5757

@@ -83,20 +83,45 @@ explained in the rest of this section assumes this. The deliverer has 2 modes.
8383

8484
size
8585
`````
86+
.. code-block:: python
87+
88+
HOOK_DELIVERER_SETTINGS = {
89+
...
90+
'size': 3, # Number of hook events per target URL
91+
...
92+
}
93+
8694
In size mode the deliverer will check the :code:`size` setting and batch the
8795
hooks whenever they reach the specified size.
8896

8997
time
9098
`````
99+
.. code-block:: python
100+
101+
HOOK_DELIVERER_SETTINGS = {
102+
...
103+
'time': 60, # Time to delay batching hook events for target URL(seconds)
104+
...
105+
}
106+
91107
In time mode the deliverer will trigger a delayed batching of hooks. It will
92108
read the time to delay from the :code:`time` setting. This delayed batching
93109
is triggered when the first hook for a target URL is sent to the deliverer.
94110

95111
mixed
96112
``````
113+
.. code-block:: python
114+
115+
HOOK_DELIVERER_SETTINGS = {
116+
...
117+
'time': 60,
118+
'size': 5,
119+
...
120+
}
121+
97122
The time and size modes can be mixed. The deliverer will batch by whichever
98-
event comes first. To use this mode, list both the size and time modes in
99-
the :code:`batch_by` setting. See below for example.
123+
event occurs first. To use this mode, provide both the time and size settings.
124+
The order of the settings in the configuration dictionary does not matter.
100125

101126

102127
Note: It is important to use caution when choosing the configuration values
@@ -105,25 +130,35 @@ for the deliverer as this can lead to resource misuse when not done properly.
105130
If this deliverer is selected, do not forget to start a celery worker for your
106131
project.
107132

108-
retry
109-
``````
110-
This deliverer can also retry failed deliveries. When retry is True the
111-
deliverer will retry failed deliveries every :code:`time` seconds until either
112-
successful or :code:`retries` retries have failed, at which point it will give
113-
up. When the deliverer gives up it will discard all failed hooks for the
114-
current target URL.
115-
116-
Don't forget to start a celery worker for your project:
117-
118133
.. code-block:: bash
119-
120-
celery -A proj worker -l info
134+
135+
celery -A proj worker -l info
121136
122137
where proj is the name of your project.
123138

124139
Check the `Celery <http://www.celeryproject.org>`_ website for a detailed
125140
example.
126141

142+
retry
143+
``````
144+
.. code-block:: python
145+
146+
HOOK_DELIVERER_SETTINGS = {
147+
...
148+
'retry': {
149+
'retries': 2, # Number of times to retry failed deliveries
150+
'retry_interval': 5, # Time to delay between retries(seconds)
151+
}
152+
...
153+
}
154+
155+
This deliverer can also retry failed deliveries. When the :code:`retry` setting
156+
is provided the deliverer will retry failed deliveries every
157+
:code:`retry_interval` seconds until either successful or :code:`retries`
158+
retries have failed, at which point it will give up. When the deliverer gives
159+
up it will discard all failed hooks for the current target URL. If this setting
160+
is not provided the deliverer will discard failed deliveries.
161+
127162
Example
128163
________
129164

@@ -142,11 +177,11 @@ ________
142177
HOOK_DELIVERER = 'rest_hooks_delivery.deliverers.batch'
143178
144179
HOOK_DELIVERER_SETTINGS = {
145-
'batch_by': ['time','size'], # List of batching modes
146-
# can be ['time'], ['size'] or ['size', 'time']
147-
'size': 3, # Number of hook events/target url to batch
148-
'time': 60, # time to delay batching for target URL(in seconds)
149-
'retry': True, # Retry failed hook deliveries(True) or discard(False)
150-
# Compulsory if retry is True
151-
'retries': 2, # Number of times to retry failed deliveries
180+
'size': 3,
181+
'time': 60,
182+
# You can comment out the mode you do not need above
183+
'retry': {
184+
'retries': 2,
185+
'retry_interval': 5,
186+
}
152187
}

rest_hooks_delivery/tasks.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,23 @@ def store_hook(*args, **kwargs):
3737
current_count = None
3838

3939
# If first in queue and batching by time
40-
if 'time' in HOOK_DELIVERER_SETTINGS['batch_by']:
40+
if 'time' in HOOK_DELIVERER_SETTINGS:
4141
current_count = event_count_for(target_url)
4242
if current_count == 1:
4343
batch_and_send.apply_async(args=(target_url,),
4444
countdown=HOOK_DELIVERER_SETTINGS['time'],
4545
link_error=fail_handler.s(target_url))
4646

47-
if 'size' in HOOK_DELIVERER_SETTINGS['batch_by']:
47+
if 'size' in HOOK_DELIVERER_SETTINGS:
4848
# check size for current target
4949
if current_count is None:
5050
current_count = event_count_for(target_url)
5151

5252
# (>=) because if retry is True then count can be > size
5353
if current_count >= HOOK_DELIVERER_SETTINGS['size']:
54-
batch_and_send(target_url)
54+
batch_and_send.apply_async(args=(target_url,),
55+
countdown=0,
56+
link_error=fail_handler.s(target_url))
5557

5658

5759
def event_count_for(target_url):
@@ -75,25 +77,27 @@ def batch_and_send(target_url):
7577
target_url,
7678
data=json.dumps(batch_data_list),
7779
headers={'Content-Type': 'application/json'})
78-
if (r.status_code > 299 and not HOOK_DELIVERER_SETTINGS['retry']) or\
80+
if (r.status_code > 299 and not 'retry' in HOOK_DELIVERER_SETTINGS) or\
7981
(r.status_code < 300):
8082
events.delete()
81-
elif (r.status_code > 299 and HOOK_DELIVERER_SETTINGS['retry']):
83+
elif (r.status_code > 299 and 'retry' in HOOK_DELIVERER_SETTINGS):
8284
if batch_and_send.request.retries == \
83-
HOOK_DELIVERER_SETTINGS['retries']:
85+
HOOK_DELIVERER_SETTINGS['retry']['retries']:
8486
events.delete()
8587
else:
8688
raise batch_and_send.retry(
8789
args=(target_url,),
88-
countdown=HOOK_DELIVERER_SETTINGS['time'])
90+
countdown=\
91+
HOOK_DELIVERER_SETTINGS['retry']['retry_interval'])
8992
except requests.exceptions.ConnectionError as exc:
90-
if HOOK_DELIVERER_SETTINGS['retry']:
93+
if 'retry' in HOOK_DELIVERER_SETTINGS:
9194
if batch_and_send.request.retries == \
92-
HOOK_DELIVERER_SETTINGS['retries']:
95+
HOOK_DELIVERER_SETTINGS['retry']['retries']:
9396
events.delete()
9497
else:
9598
raise batch_and_send.retry(
9699
args=(target_url,), exc=exc,
97-
countdown=HOOK_DELIVERER_SETTINGS['time'])
100+
countdown=\
101+
HOOK_DELIVERER_SETTINGS['retry']['retry_interval'])
98102
else:
99103
events.delete()

0 commit comments

Comments
 (0)