Exemple #1
0
class ScheInterface(object):
    def __init__(self, job_type, store_executor_alias, process_count):
        self.sche = TornadoScheduler()
        self.host = MONGO_CONFIG.get('host')
        self.mongo_client = MongoClient(self.host)
        self.job_type = job_type
        self.mongo_job_store = MongoDBJobStore(collection='job',
                                               database=DBNAME,
                                               client=self.mongo_client)
        self.store_executor_alise = store_executor_alias
        self.process_poll = ProcessPoolExecutor(process_count)

    def add_date_job(self, func, args, run_date, max_instances, listener_fun):
        self.sche.add_jobstore(self.mongo_job_store,
                               alias=self.store_executor_alise)
        self.sche.add_executor(self.process_poll,
                               alias=self.store_executor_alise)
        self.sche.add_listener(listener_fun,
                               EVENT_JOB_ERROR | EVENT_JOB_MISSED)
        try:
            self.sche.add_job(func,
                              self.job_type,
                              args=args,
                              run_date=run_date,
                              max_instances=max_instances,
                              jobstore=self.store_executor_alise)
            return True
        except Exception:
            return False
def on_tick():
    tick_logger = logging.getLogger(__name__)
    try:
        last = snapshotter.snap_last_trade('BTC-USD')
        rxt = datetime.datetime.now(pytz.utc)
        px = float(last['price'])
        qty = float(last['size'])
        last_row = [{'index': rxt, 'price': px, 'qty': qty}]
        tick_lib.write('BTC-USD', last_row, metadata={'source': 'CoinbasePro'})
        tick_logger.info("wrote latest trade to Arctic: {} @ {}".format(
            qty, px))
    except CoinbaseAPIError:
        tick_logger.info(
            "ignoring transient error from Coinbase Pro API; will retry")


if __name__ == '__main__':
    scheduler = TornadoScheduler()
    scheduler.add_jobstore(MemoryJobStore())
    scheduler.add_executor(TornadoExecutor())

    scheduler.add_job(on_tick, 'interval', minutes=1)
    scheduler.start()

    # Execution will block here until Ctrl+C (Ctrl+Break on Windows) is pressed.
    try:
        logger.info("starting Tornado")
        IOLoop.instance().start()
    except (KeyboardInterrupt, SystemExit):
        pass