sync
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 3m3s
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 3m3s
This commit is contained in:
@@ -39,8 +39,6 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
CONSUL_LIBRARY = False
|
CONSUL_LIBRARY = False
|
||||||
|
|
||||||
import schedule
|
|
||||||
|
|
||||||
# Configure logging
|
# Configure logging
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=logging.INFO,
|
level=logging.INFO,
|
||||||
@@ -66,7 +64,7 @@ class WeightRecord:
|
|||||||
class ConsulManager:
|
class ConsulManager:
|
||||||
"""Manages all configuration and state in Consul K/V store"""
|
"""Manages all configuration and state in Consul K/V store"""
|
||||||
|
|
||||||
def __init__(self, host: str = "consul.service.dc1.consul", port: int = 8500, prefix: str = "fitbit-garmin-sync"):
|
def __init__(self, host: str = "localhost", port: int = 8500, prefix: str = "fitbit-garmin-sync"):
|
||||||
if not CONSUL_LIBRARY:
|
if not CONSUL_LIBRARY:
|
||||||
raise ImportError("python-consul library not installed. Please install it with: pip install python-consul")
|
raise ImportError("python-consul library not installed. Please install it with: pip install python-consul")
|
||||||
|
|
||||||
@@ -832,7 +830,7 @@ class GarminClient:
|
|||||||
class WeightSyncApp:
|
class WeightSyncApp:
|
||||||
"""Main application class"""
|
"""Main application class"""
|
||||||
|
|
||||||
def __init__(self, consul_host: str = "consul.service.dc1.consul", consul_port: int = 8500,
|
def __init__(self, consul_host: str = "localhost", consul_port: int = 8500,
|
||||||
consul_prefix: str = "fitbit-garmin-sync"):
|
consul_prefix: str = "fitbit-garmin-sync"):
|
||||||
self.consul = ConsulManager(consul_host, consul_port, consul_prefix)
|
self.consul = ConsulManager(consul_host, consul_port, consul_prefix)
|
||||||
self.fitbit = FitbitClient(self.consul)
|
self.fitbit = FitbitClient(self.consul)
|
||||||
@@ -1038,22 +1036,28 @@ class WeightSyncApp:
|
|||||||
print(f"✅ Read-only mode {mode_text}")
|
print(f"✅ Read-only mode {mode_text}")
|
||||||
print(f" {'Will NOT upload to Garmin' if new_mode else 'Will upload to Garmin'}")
|
print(f" {'Will NOT upload to Garmin' if new_mode else 'Will upload to Garmin'}")
|
||||||
|
|
||||||
def start_scheduler(self):
|
async def start_scheduler(self):
|
||||||
"""Start the sync scheduler"""
|
"""Start the sync scheduler"""
|
||||||
config = self.consul.get_config()
|
config = self.consul.get_config()
|
||||||
sync_interval = config.get('sync', {}).get('sync_interval_minutes', 60)
|
sync_interval = config.get('sync', {}).get('sync_interval_minutes', 60)
|
||||||
|
|
||||||
logger.info(f"Starting scheduler with {sync_interval} minute interval")
|
logger.info(f"Starting scheduler with {sync_interval} minute interval")
|
||||||
|
logger.info("Running initial sync...")
|
||||||
|
|
||||||
schedule.every(sync_interval).minutes.do(
|
# Run initial sync immediately
|
||||||
lambda: asyncio.create_task(self.sync_weight_data())
|
await self.sync_weight_data()
|
||||||
)
|
|
||||||
|
|
||||||
asyncio.create_task(self.sync_weight_data())
|
logger.info(f"Scheduled syncs will run every {sync_interval} minutes")
|
||||||
|
|
||||||
|
# Schedule periodic syncs
|
||||||
while True:
|
while True:
|
||||||
schedule.run_pending()
|
try:
|
||||||
time.sleep(60)
|
await asyncio.sleep(sync_interval * 60)
|
||||||
|
logger.info("Running scheduled sync...")
|
||||||
|
await self.sync_weight_data()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error in scheduled sync: {e}")
|
||||||
|
await asyncio.sleep(60) # Wait a minute before retrying
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
@@ -1109,11 +1113,13 @@ async def main():
|
|||||||
try:
|
try:
|
||||||
config = app.consul.get_config()
|
config = app.consul.get_config()
|
||||||
read_only_mode = config.get('sync', {}).get('read_only_mode', False)
|
read_only_mode = config.get('sync', {}).get('read_only_mode', False)
|
||||||
|
sync_interval = config.get('sync', {}).get('sync_interval_minutes', 60)
|
||||||
print("🚀 Starting scheduled sync...")
|
print("🚀 Starting scheduled sync...")
|
||||||
|
print(f"⏰ Sync interval: {sync_interval} minutes")
|
||||||
if read_only_mode:
|
if read_only_mode:
|
||||||
print("📖 Running in read-only mode")
|
print("📖 Running in read-only mode")
|
||||||
print("Press Ctrl+C to stop")
|
print("Press Ctrl+C to stop")
|
||||||
app.start_scheduler()
|
await app.start_scheduler()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("\n👋 Scheduler stopped")
|
print("\n👋 Scheduler stopped")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user