Skip to content

Instantly share code, notes, and snippets.

@dankrause
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dankrause/11261655 to your computer and use it in GitHub Desktop.
Save dankrause/11261655 to your computer and use it in GitHub Desktop.
An example jobboard client for taskflow
#!/usr/bin/env python
"""jobclient.py
Usage:
jobclient.py [options] <jobboard_name> create <name> <details>
jobclient.py [options] <jobboard_name> list
jobclient.py [options] <jobboard_name> delete <uuid>
jobclient.py [options] <jobboard_name> clear
Options:
--persistence=<connection> Persistence connection string [Default: memory]
--jobboard=<type> Jobboard type [default: zookeeper]
"""
import docopt
import time
import json
from contextlib import contextmanager
from taskflow.persistence import logbook
from taskflow.jobs.backends import fetch as fetch_jobboard
from taskflow.persistence.backends import fetch as fetch_persistence
@contextmanager
def persistence(*args, **kwargs):
backend = fetch_persistence(*args, **kwargs)
connection = backend.get_connection()
connection.upgrade()
yield backend
connection.close()
@contextmanager
def jobboard(*args, **kwargs):
jb = fetch_jobboard(*args, **kwargs)
jb.connect()
yield jb
jb.close()
args = docopt.docopt(__doc__)
if args["create"]:
store = json.loads(args["<details>"])
book = logbook.LogBook(args["<name>"])
with persistence({"connection": args["--persistence"]}) as backend:
backend.get_connection().save_logbook(book)
with jobboard(args["<jobboard_name>"], args["--jobboard"], persistence=backend) as jb:
jb.post(args["<name>"], book, details=store)
if args["list"]:
with jobboard(args["<jobboard_name>"], args["--jobboard"]) as jb:
time.sleep(0.3)
for job in jb.iterjobs():
print "{} - {}".format(job.uuid, job.details)
if args["delete"]:
with jobboard(args["<jobboard_name>"], args["--jobboard"]) as jb:
time.sleep(0.3)
for job in jb.iterjobs():
if job.uuid == args["<uuid>"]:
print "Clearing job: {} - {}".format(job.uuid, job.details)
jb.claim(job, "test-client")
jb.consume(job, "test-client")
if args["clear"]:
with jobboard(args["<jobboard_name>"], args["--jobboard"]) as jb:
time.sleep(0.3)
for job in jb.iterjobs():
print "Clearing job: {} - {}".format(job.uuid, job.details)
jb.claim(job, "test-client")
jb.consume(job, "test-client")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment