Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

PythonWorker101

  • Loading branch information...
commit 09597e0492b14c327ede494f4a163420021bb838 1 parent 686a41d
@rkononov rkononov authored
View
2  php/worker101/README.md
@@ -6,6 +6,6 @@ dependencies.
1. Be sure you've setup your Iron.io credentials, see main [README.md](https://github.com/iron-io/iron_worker_examples).
1. Run `iron_worker upload worker101` to upload the worker code package to IronWorker.
1. Queue up a task:
- 1. From command line: `iron_worker queue PHPWorker101 -p '{"query":"xbox"}'`
+ 1. From command line: `iron_worker queue PHPWorker101 --payload '{"query":"xbox"}'`
1. Run `php enqueue.php` to queue up a task. Edit enqueue.php to change the Twitter query.
1. Look at [HUD](https://hud.iron.io) to view your tasks running, check logs, etc.
View
44 python/pagerduty/worker.py
@@ -1,6 +1,14 @@
import argparse
import json
-from pagerduty import PagerDuty
+import httlib
+import json
+import logging
+import socket
+import time
+import urllib
+
+SEARCH_HOST="search.twitter.com"
+SEARCH_PATH="/search.json"
parser = argparse.ArgumentParser(
description="Calculates the Fibonacci sequence up to a maximum number")
@@ -11,14 +19,32 @@
pd = False
if args.payload is not None:
payload = json.loads(open(args.payload).read())
- if 'pagerduty' in payload:
- pd = PagerDuty(payload['pagerduty']['secret_key'])
-
-def trigger_alert(description):
- if pd:
- pd.trigger(description)
+ if 'query' in payload:
+ query = payload['query']
+def search(query):
+ c = httplib.HTTPConnection(SEARCH_HOST)
+ params = {'q' : query}
+ if self.max_id is not None:
+ params['since_id'] = self.max_id
+ path = "%s?%s" %(SEARCH_PATH, urllib.urlencode(params))
+ try:
+ c.request('GET', path)
+ r = c.getresponse()
+ data = r.read()
+ c.close()
+ try:
+ result = json.loads(data)
+ except ValueError:
+ return None
+ if 'results' not in result:
+ return None
+ self.max_id = result['max_id']
+ return result['results']
+ except (httplib.HTTPException, socket.error, socket.timeout), e:
+ logging.error("search() error: %s" %(e))
+ return None
#some code here
-#let's report an incident eventually
-trigger_alert('Something bad happened, evacuate immediately')
+#Workers code
+twittersearch(query)
View
11 python/worker101/README.md
@@ -0,0 +1,11 @@
+# Worker 101
+
+This covers most of the core concepts of using IronWorker including loading third party
+dependencies.
+
+1. Be sure you've setup your Iron.io credentials, see main [README.md](https://github.com/iron-io/iron_worker_examples).
+1. Run `iron_worker upload worker101` to upload the worker code package to IronWorker.
+1. Queue up a task:
+ 1. From command line: `iron_worker queue PythonWorker101 --payload '{"query":"xbox"}'`
+ 1. Run `python enqueue.py` to queue up a task. Edit enqueue.py to change the Twitter query.
+1. Look at [HUD](https://hud.iron.io) to view your tasks running, check logs, etc.
View
7 python/worker101/enqueue.py
@@ -0,0 +1,7 @@
+from iron_worker import *
+
+worker = IronWorker(token='XXXXXXXXXX', project_id='xxxxxxxxxxx')
+
+payload = {'pagerduty': {'query':'iron.io'}}
+
+task = worker.postTask(name='PythonWorker101', payload=payload)
View
0  python/worker101/lib/__init__.py
No changes.
View
28 python/worker101/lib/twitter.py
@@ -0,0 +1,28 @@
+import httplib
+import json
+import logging
+import socket
+import time
+import urllib
+SEARCH_HOST="search.twitter.com"
+SEARCH_PATH="/search.json"
+
+def search(query):
+ c = httplib.HTTPConnection(SEARCH_HOST)
+ params = {'q' : query}
+ path = "%s?%s" %(SEARCH_PATH, urllib.urlencode(params))
+ try:
+ c.request('GET', path)
+ r = c.getresponse()
+ data = r.read()
+ c.close()
+ try:
+ result = json.loads(data)
+ except ValueError:
+ return None
+ if 'results' not in result:
+ return None
+ return result['results']
+ except (httplib.HTTPException, socket.error, socket.timeout), e:
+ logging.error("search() error: %s" %(e))
+ return None
View
36 python/worker101/worker101.py
@@ -0,0 +1,36 @@
+import argparse
+import json
+from lib import twitter
+query = 'iron.io'
+
+parser = argparse.ArgumentParser(
+ description="Simple argument parser")
+parser.add_argument("-payload", type=str, required=False,
+ help="The location of a file containing a JSON payload.")
+parser.add_argument("-d", type=str, required=False,
+ help="Directory")
+parser.add_argument("-e", type=str, required=False,
+ help="Environment")
+parser.add_argument("-id", type=str, required=False,
+ help="Task id")
+args = parser.parse_args()
+
+pd = False
+if args.payload is not None:
+ payload = json.loads(open(args.payload).read())
+ if 'query' in payload:
+ query = payload['query']
+
+#Workers code
+if query is not None:
+ print("Search by query")
+ results = twitter.search(query)
+ print(results)
+ myInput = open('myfile.txt','w+')
+ print("now writing to file")
+ myInput.write(results[0]['text'])
+ myInput.close()
+ myInput = open('myfile.txt','r')
+ print("now reading from file")
+ res_from_file = myInput.read()
+ print(res_from_file)
View
5 python/worker101/worker101.worker
@@ -0,0 +1,5 @@
+runtime 'python'
+exec 'worker101.py'
+dir 'lib'
+name 'PythonWorker101'
+
Please sign in to comment.
Something went wrong with that request. Please try again.