-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
10 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
service_name: travis-pro | ||
repo_token: nLWjoaBWJ6HpMFDq10AlCwoVMlGZxEV5W |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ | |
|
||
language: python | ||
|
||
services: | ||
- redis | ||
|
||
python: | ||
- "3.3" | ||
- "2.7" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ test-all: | |
|
||
coverage: | ||
py.test --cov webhooks | ||
coverage html | ||
open htmlcov/index.html | ||
|
||
docs: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# webhook dependencies | ||
wrapt>=1.8.0 | ||
requests>=2.2.1 | ||
rq>=0.4.5 | ||
|
||
# Webhook test dependencies | ||
coverage | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import json | ||
import logging | ||
from time import sleep | ||
|
||
import requests | ||
from redis import Redis | ||
import rq | ||
from rq.decorators import job | ||
|
||
from ..encoders import WebHooksJSONEncoder | ||
|
||
|
||
logging.basicConfig(filename="async.log", level=logging.DEBUG) | ||
|
||
connection = Redis() | ||
q = rq.Queue(connection=connection) | ||
|
||
|
||
@job('default', connection=connection, timeout=5) | ||
def worker(wrapped, dkwargs, hash_value=None, *args, **kwargs): | ||
|
||
# First element is 0 to represent the first attempt | ||
# TODO - pass this in as a decorator argument | ||
ATTEMPTS = [0, 1, 2, 3, 4] | ||
|
||
# Get the URL from the kwargs | ||
url = kwargs.get('url', None) | ||
if url is None: | ||
url = dkwargs['url'] | ||
|
||
# Create the payload by calling the hooked/wrapped function. | ||
payload = wrapped(*args, **kwargs) | ||
|
||
# Add the hash value if there is one. | ||
if hash_value is not None and len(hash_value) > 0: | ||
payload['hash'] = hash_value | ||
|
||
# Dump the payload to json | ||
data = json.dumps(payload, cls=WebHooksJSONEncoder) | ||
|
||
for attempt in range(len(ATTEMPTS) - 1): | ||
# Print each attempt. In practice, this would write to logs | ||
msg = "Attempt: {attempt}, {url}\n{payload}".format( | ||
attempt=attempt, | ||
url=url, | ||
payload=data | ||
) | ||
logging.debug(msg) | ||
|
||
# post the payload | ||
r = requests.post(url, payload) | ||
|
||
# anything with a 200 status code is a success | ||
if r.status_code >= 200 and r.status_code < 300: | ||
# Exit the sender function. Here we provide the payload as a result. | ||
# In practice, this means writing the result to a datastore. | ||
logging.debug("Success!") | ||
return payload | ||
|
||
# Log the current status of things and try again. | ||
# TODO - add logs | ||
|
||
# Wait a bit before the next attempt | ||
sleep(attempt) | ||
else: | ||
logging.debug("Could not send webhook") | ||
|
||
# Exit the sender function. Here we provide the payload as a result for | ||
# display when this function is run outside of the sender function. | ||
return payload | ||
|
||
|
||
def sender(wrapped, dkwargs, hash_value=None, *args, **kwargs): | ||
|
||
logging.debug("Starting async") | ||
worker(wrapped, dkwargs, hash_value=None, *args, **kwargs) | ||
logging.debug("Ending async") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters