Skip to content

Commit

Permalink
large cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pipermerriam committed Sep 29, 2016
1 parent 85fc76e commit 2daefcb
Show file tree
Hide file tree
Showing 14 changed files with 404 additions and 407 deletions.
80 changes: 64 additions & 16 deletions alarm_client/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import click
import functools
import random
import logging
import gevent

from web3 import (
Expand All @@ -9,7 +9,11 @@
)

from .config import Config
from .client.main import new_block_callback
from .client.main import (
new_block_callback,
executed_event_callback,
aborted_event_callback,
)


@click.group()
Expand All @@ -22,12 +26,23 @@
'-f',
)
@click.option(
'--scheduler-address',
'-s',
'--payment-lib-address',
)
@click.option(
'--request-lib-address',
'-r',
)
@click.option(
'--log-level',
'-l',
type=click.Choice([
logging.CRITICAL,
logging.ERROR,
logging.WARNING,
logging.INFO,
logging.DEBUG,
]),
default=logging.INFO,
)
@click.option(
'--provider',
Expand All @@ -41,20 +56,36 @@
# TODO: remove this
default='/Users/piper/sites/ethereum-alarm-clock/chains/local/geth.ipc',
)
@click.option(
'--compiled-assets-path',
'-a',
type=click.Path(dir_okay=False),
default='./build/contracts.json',
)
@click.pass_context
def main(ctx,
tracker_address,
factory_address,
scheduler_address,
payment_lib_address,
request_lib_address,
log_level,
provider,
ipc_path):
ipc_path,
compiled_assets_path):
if provider == 'ipc':
web3 = Web3(IPCProvider(ipc_path=ipc_path))
else:
raise click.ClickException("This shouldn't be possible")

config = Config(web3)
config = Config(
web3,
compiled_assets_path=compiled_assets_path,
log_level=log_level,
tracker_address=tracker_address,
factory_address=factory_address,
payment_lib_address=payment_lib_address,
request_lib_address=request_lib_address,
)

ctx.web3 = web3
ctx.config = config
Expand Down Expand Up @@ -258,30 +289,47 @@ def client(ctx):
main_ctx = ctx.parent
web3 = main_ctx.web3
config = main_ctx.config
TransactionRequestFactory = config.get_transaction_request(None)

new_block_filter = web3.eth.filter('latest')
executed_event_filter = TransactionRequestFactory.on('Executed')
aborted_event_filter = TransactionRequestFactory.on('Aborted')

click.echo("Starting client")

callback = functools.partial(new_block_callback, config=config)

new_block_filter.watch(callback)

# give it a moment to spin up.
gevent.sleep(1)
new_block_filter.watch(functools.partial(new_block_callback, config))
executed_event_filter.watch(functools.partial(executed_event_callback, config))
aborted_event_filter.watch(functools.partial(aborted_event_callback, config))

try:
while new_block_filter.running is True:
gevent.sleep(random.random())
while True:
gevent.sleep(1)
all_still_running = all((
new_block_filter.running,
executed_event_filter.running,
aborted_event_filter.running,
))
if not all_still_running:
break
finally:
if new_block_filter.running:
click.echo("Stopping Client")
click.echo("Stopping Block Filter")
new_block_filter.stop_watching()
if executed_event_filter.running:
click.echo("Stopping Executed Event Filter")
executed_event_filter.stop_watching()
if aborted_event_filter.running:
click.echo("Stopping Aborted Event Filter")
aborted_event_filter.stop_watching()


@main.command()
@click.pass_context
def repl(ctx):
"""
Drop into a debugger shell with most of what you might want available in
the local context.
"""
main_ctx = ctx.parent
web3 = main_ctx.web3 # noqa
config = main_ctx.config
Expand Down
Loading

0 comments on commit 2daefcb

Please sign in to comment.