}, upsert=True, return_document=ReturnDocument.AFTER) heartbeat_rec_id = heartbeat_rec["_id"] patch_requests_with_default_timeout(timeout=60) if isinstance(settings.HTTP_SOURCE_ADDR, list): settings.HTTP_SOURCE_ADDR = settings.HTTP_SOURCE_ADDR[ settings.WORKER_INDEX % len(settings.HTTP_SOURCE_ADDR)] patch_requests_source_address((settings.HTTP_SOURCE_ADDR, 0)) # We defer including the main body of the application till here so the settings aren't captured before we've set them up. # The better way would be to defer initializing services until they're requested, but it's 10:30 and this will work just as well. from tapiriik.sync import Sync sync_heartbeat("ready") worker_message("ready") Sync = Sync() Sync.PerformGlobalSync(heartbeat_callback=sync_heartbeat, version=WorkerVersion) worker_message("shutting down cleanly") db.sync_workers.delete_one({"_id": heartbeat_rec_id}) close_connections() worker_message("shut down") logging.info("-----[ ENDING SYNC_WORKER ]-----") sys.stdout.flush()
if isinstance(settings.HTTP_SOURCE_ADDR, list): settings.HTTP_SOURCE_ADDR = settings.HTTP_SOURCE_ADDR[ settings.WORKER_INDEX % len(settings.HTTP_SOURCE_ADDR)] patch_requests_source_address((settings.HTTP_SOURCE_ADDR, 0)) print(" -> Index %s\n -> Interface %s" % (settings.WORKER_INDEX, settings.HTTP_SOURCE_ADDR)) # We defer including the main body of the application till here so the settings aren't captured before we've set them up. # The better way would be to defer initializing services until they're requested, but it's 10:30 and this will work just as well. from tapiriik.sync import Sync while Run: cycleStart = datetime.utcnow( ) # Avoid having synchronization fall down during DST setback processed_user_count = Sync.PerformGlobalSync( heartbeat_callback=sync_heartbeat, version=WorkerVersion) RecycleInterval -= processed_user_count # When there's no queue, all the workers sit sending 1000s of the queries to the database server if processed_user_count == 0: # Put this before the recycle shutdown, otherwise it'll quit and get rebooted ASAP remaining_cycle_time = NoQueueMinCycleTime - (datetime.utcnow() - cycleStart) if remaining_cycle_time > timedelta(0): print("Pausing for %ss" % remaining_cycle_time.total_seconds()) sync_heartbeat("idle-spin") time.sleep(remaining_cycle_time.total_seconds()) if RecycleInterval <= 0: break sync_heartbeat("idle") print("Sync worker shutting down cleanly")
upsert=True) sys.stdout.flush() patch_requests_with_default_timeout(timeout=60) if isinstance(settings.HTTP_SOURCE_ADDR, list): settings.HTTP_SOURCE_ADDR = settings.HTTP_SOURCE_ADDR[ settings.WORKER_INDEX % len(settings.HTTP_SOURCE_ADDR)] patch_requests_source_address((settings.HTTP_SOURCE_ADDR, 0)) print(" -> Index %s\n -> Interface %s" % (settings.WORKER_INDEX, settings.HTTP_SOURCE_ADDR)) # We defer including the main body of the application till here so the settings aren't captured before we've set them up. # The better way would be to defer initializing services until they're requested, but it's 10:30 and this will work just as well. from tapiriik.sync import Sync while Run: cycleStart = datetime.datetime.utcnow() RecycleInterval -= Sync.PerformGlobalSync( heartbeat_callback=sync_heartbeat, version=WorkerVersion) if RecycleInterval <= 0: break if (datetime.datetime.utcnow() - cycleStart).total_seconds() < 1: time.sleep(1) sync_heartbeat("idle") print("Sync worker shutting down cleanly") db.sync_workers.remove({"Process": os.getpid()}) sys.stdout.flush()
oldCwd = os.getcwd() WorkerVersion = subprocess.Popen(["git", "rev-parse", "HEAD"], stdout=subprocess.PIPE, cwd=os.path.dirname(__file__)).communicate()[0].strip() os.chdir(oldCwd) def sync_interrupt(signal, frame): global Run Run = False signal.signal(signal.SIGINT, sync_interrupt) def sync_heartbeat(state): db.sync_workers.update({"Process": os.getpid()}, {"$set": {"Heartbeat": datetime.datetime.utcnow(), "State": state}}) print("Sync worker starting at " + datetime.datetime.now().ctime() + " pid " + str(os.getpid())) db.sync_workers.update({"Process": os.getpid()}, {"Process": os.getpid(), "Heartbeat": datetime.datetime.utcnow(), "Startup": datetime.datetime.utcnow(), "Version": WorkerVersion, "Host": socket.gethostname(), "State": "startup"}, upsert=True) sys.stdout.flush() patch_requests_with_default_timeout(timeout=60) while Run: cycleStart = datetime.datetime.utcnow() Sync.PerformGlobalSync(heartbeat_callback=sync_heartbeat) if (datetime.datetime.utcnow() - cycleStart).total_seconds() < 1: time.sleep(1) sync_heartbeat("idle") print("Sync worker shutting down cleanly") db.sync_workers.remove({"Process": os.getpid()}) sys.stdout.flush()