def main(): init_logging() init_model(config.SQLALCHEMY_URL) shutdown_event = threading.Event() scheduler = BackgroundScheduler() # workflow_publisher = configured_publisher() activity_sync = ActivitySync() weather_sync = WeatherSync() athlete_sync = AthleteSync() # Every hour run a sync on the activities for athletes fall into the specified segment # athlete_id % total_segments == segment # TODO: Probably it would be more prudent to split into 15-minute segments, to match rate limits # Admittedly that will make the time-based segment calculation a little trickier. def segmented_sync_activities(): activity_sync.sync_rides_distributed(total_segments=4, segment=(arrow.now().hour % 4)) scheduler.add_job(segmented_sync_activities, 'cron', minute='50') # This should generally not pick up anytihng. scheduler.add_job(activity_sync.sync_rides_detail, 'cron', minute='20') # Sync weather at 8am UTC scheduler.add_job(weather_sync.sync_weather, 'cron', hour='8') # Sync athletes once a day at 6am UTC scheduler.add_job(athlete_sync.sync_athletes, 'cron', minute='30') scheduler.start() beanclient = Client(host=config.BEANSTALKD_HOST, port=config.BEANSTALKD_PORT, watch=[DefinedTubes.activity_update.value]) subscriber = ActivityUpdateSubscriber(beanstalk_client=beanclient, shutdown_event=shutdown_event) def shutdown_app(): shutdown_event.wait() scheduler.shutdown() shutdown_monitor = threading.Thread(target=shutdown_app) shutdown_monitor.start() try: # This is here to simulate application activity (which keeps the main thread alive). subscriber.run_forever() except (KeyboardInterrupt, SystemExit): log.info("Exiting on user request.") except: log.exception("Error running sync/listener.") finally: shutdown_event.set() shutdown_monitor.join()
def test_delete_job_reserved_by_other(c: Client) -> None: c.put('', ttr=1) other = Client(port=PORT) job = other.reserve() with pytest.raises(NotFoundError): c.delete(job) time.sleep(1) c.delete(job)
async def wrapper() -> None: cmd = ('beanstalkd', '-l', '127.0.0.1', '-p', str(PORT)) with subprocess.Popen(cmd) as beanstalkd: time.sleep(0.1) try: async with Client(port=PORT, **kwargs) as c: await test(c) finally: beanstalkd.terminate()
def wrapper() -> None: port = 4444 cmd = ('beanstalkd', '-l', '127.0.0.1', '-p', str(port)) with subprocess.Popen(cmd) as beanstalkd: time.sleep(0.1) try: with Client(port=port, **kwargs) as c: test(c) finally: beanstalkd.terminate()
def wrapper() -> None: cmd = [BEANSTALKD_PATH] if isinstance(address, str): cmd.extend(["-l", "unix:" + address]) else: host, port = address cmd.extend(["-l", host, "-p", str(port)]) with subprocess.Popen(cmd) as beanstalkd: time.sleep(0.1) try: with Client(address, use=use, watch=watch) as c: test(c) finally: beanstalkd.terminate()
def test_drain_mode() -> None: cmd = [BEANSTALKD_PATH, "-l", "unix:" + DEFAULT_UNIX_ADDRESS] with subprocess.Popen(cmd) as beanstalkd: time.sleep(0.1) try: with Client(address=DEFAULT_UNIX_ADDRESS) as c: assert c.put(b"first") == 1 beanstalkd.send_signal(signal.SIGUSR1) time.sleep(0.1) with pytest.raises(DrainingError): c.put(b"second") assert c.stats()["draining"] == "true" finally: beanstalkd.terminate()
def wrapper() -> None: cmd = [BEANSTALKD_PATH] if isinstance(address, str): cmd.extend(['-l', 'unix:' + address]) else: host, port = address cmd.extend(['-l', host, '-p', str(port)]) with subprocess.Popen(cmd) as beanstalkd: time.sleep(0.1) try: with Client(address, **kwargs) as c: test(c) finally: beanstalkd.terminate()
def test_delete_job_reserved_by_other(c: Client) -> None: c.put(b"", ttr=1) with Client(DEFAULT_INET_ADDRESS) as other: job = other.reserve() with pytest.raises(NotFoundError): c.delete(job)
async def test_delete_job_reserved_by_other(c: Client) -> None: await c.put('', ttr=1) async with Client(port=PORT) as other: job = await other.reserve() with pytest.raises(NotFoundError): await c.delete(job)