Skip to content

Instantly share code, notes, and snippets.

@jcockhren
Last active August 29, 2015 13:55
Show Gist options
  • Save jcockhren/8722076 to your computer and use it in GitHub Desktop.
Save jcockhren/8722076 to your computer and use it in GitHub Desktop.
Example of an IronWorker that talks to couchDB
from couchdbkit import *
from models import Check
import requests
import datetime
# We need to preload the memory with a list of sites to check.
server = Server('somecouchserverhere')
sitedb = server.get_db('yourdbname')
sites_view = sitedb.view('someview', key="Site")
sites = sites_view.all()
db = server.get_db('yourdbname')
Check.set_db(db)
checks = list()
print "Checking {0} sites".format(len(sites))
for s in sites:
print s['value']['url']
r = requests.get("{0}://{1}{2}".format(s['value']['check_type'], s['value']['url'], s['value']['path']), verify=False)
check_time = datetime.datetime.utcnow()
check = Check(url=s['value']['url'],
path=s['value']['path'],
owner_id=s['value']['owner_id'],
check_type=s['value']['check_type'],
port=s['value']['port'])
check.response_time = r.elapsed.microseconds
print "Elapsed Time: {0} ms".format(r.elapsed.microseconds/1000.0)
check.time = check_time
if s['value']['check_type'] in ['https', 'http']:
if r.status_code in [200, 301, 301]:
check.response_value = 0
else:
check.response_value = 1
checks.append(check)
check.bulk_save(checks)
from couchdbkit import *
import datetime
class Site(Document):
url = StringProperty()
path = StringProperty()
check_type = StringProperty()
port = IntegerProperty()
owner_id = StringProperty()
class Check(Site):
time = DateTimeProperty()
response_time = IntegerProperty() # microseconds
response_value = IntegerProperty()
runtime "python"
exec "checks.py"
file "models.py"
pip "requests"
pip "couchdbkit"
pip "datetime"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment