-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_cli.py
More file actions
268 lines (217 loc) · 7.5 KB
/
sync_cli.py
File metadata and controls
268 lines (217 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python3
"""
Eclosion Background Sync CLI
Standalone CLI for running sync operations from OS-level schedulers
(launchd, Task Scheduler, systemd). Reads passphrase from OS keychain,
unlocks credentials, runs sync, and exits.
Exit codes:
0 - Sync completed successfully
1 - No passphrase stored in keychain
2 - Failed to unlock credentials (wrong passphrase or corrupted)
3 - Sync failed
4 - Another sync is already in progress (lock file exists)
5 - Sync disabled due to previous failures
"""
import asyncio
import json
import logging
import os
import sys
from datetime import datetime
from pathlib import Path
# Add project root to path for imports
PROJECT_ROOT = Path(__file__).parent
sys.path.insert(0, str(PROJECT_ROOT))
from core.keychain import SERVICE_NAME, get_passphrase
from services.credentials_service import CredentialsService
from services.sync_service import SyncService
# Configure logging to file
LOG_DIR = Path(os.environ.get("STATE_DIR", Path.home() / ".config" / "eclosion")) / "logs"
LOG_FILE = LOG_DIR / "background-sync.log"
# Maximum consecutive failures before disabling
MAX_CONSECUTIVE_FAILURES = 3
FAILURE_STATE_FILE = "background_sync_failures.json"
def setup_logging() -> None:
"""Configure logging for background sync."""
LOG_DIR.mkdir(parents=True, exist_ok=True)
# Rotate log if too large (>1MB)
if LOG_FILE.exists() and LOG_FILE.stat().st_size > 1024 * 1024:
rotated = LOG_FILE.with_suffix(".log.1")
if rotated.exists():
rotated.unlink()
LOG_FILE.rename(rotated)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler(LOG_FILE, encoding="utf-8"),
logging.StreamHandler(sys.stdout),
],
)
def get_lock_file_path() -> Path:
"""Get the path to the sync lock file."""
state_dir = Path(os.environ.get("STATE_DIR", Path.home() / ".config" / "eclosion"))
return state_dir / "sync.lock"
def is_sync_locked() -> bool:
"""Check if another sync is in progress."""
lock_file = get_lock_file_path()
if not lock_file.exists():
return False
# Check if lock is stale (older than 10 minutes)
try:
lock_age = datetime.now().timestamp() - lock_file.stat().st_mtime
if lock_age > 10 * 60: # 10 minutes
logging.info("Removing stale lock file")
lock_file.unlink()
return False
return True
except OSError:
return False
def acquire_lock() -> bool:
"""Acquire the sync lock."""
if is_sync_locked():
return False
lock_file = get_lock_file_path()
try:
lock_file.parent.mkdir(parents=True, exist_ok=True)
lock_file.write_text(
json.dumps(
{
"pid": os.getpid(),
"timestamp": datetime.now().isoformat(),
"source": "background-sync-cli",
}
)
)
return True
except OSError as e:
logging.error("Failed to acquire lock: %s", e)
return False
def release_lock() -> None:
"""Release the sync lock."""
lock_file = get_lock_file_path()
try:
if lock_file.exists():
lock_file.unlink()
except OSError as e:
logging.error("Failed to release lock: %s", e)
def get_failure_state_path() -> Path:
"""Get the path to the failure state file."""
state_dir = Path(os.environ.get("STATE_DIR", Path.home() / ".config" / "eclosion"))
return state_dir / FAILURE_STATE_FILE
def get_failure_count() -> int:
"""Get the current consecutive failure count."""
state_file = get_failure_state_path()
if not state_file.exists():
return 0
try:
data = json.loads(state_file.read_text())
return int(data.get("consecutive_failures", 0))
except (json.JSONDecodeError, OSError):
return 0
def record_failure(error: str) -> None:
"""Record a sync failure."""
state_file = get_failure_state_path()
try:
current = get_failure_count()
state_file.write_text(
json.dumps(
{
"consecutive_failures": current + 1,
"last_failure": datetime.now().isoformat(),
"last_error": error,
}
)
)
except OSError as e:
logging.error("Failed to record failure: %s", e)
def clear_failures() -> None:
"""Clear the failure count on success."""
state_file = get_failure_state_path()
try:
if state_file.exists():
state_file.unlink()
except OSError as e:
logging.error("Failed to clear failure state: %s", e)
def is_disabled_due_to_failures() -> bool:
"""Check if background sync is disabled due to too many failures."""
return get_failure_count() >= MAX_CONSECUTIVE_FAILURES
async def run_sync() -> int:
"""
Run the background sync.
Returns:
Exit code (0 for success, non-zero for failure)
"""
logger = logging.getLogger(__name__)
# Check if disabled due to failures
if is_disabled_due_to_failures():
logger.error(
"Background sync disabled due to %d consecutive failures. "
"Please re-enable in settings.",
MAX_CONSECUTIVE_FAILURES,
)
return 5
# Check lock
if is_sync_locked():
logger.info("Another sync is in progress, skipping")
return 4
# Get passphrase from keychain
logger.info("Retrieving passphrase from OS keychain (service=%s)", SERVICE_NAME)
passphrase = get_passphrase()
if not passphrase:
logger.error("No passphrase found in OS keychain")
return 1
# Acquire lock
if not acquire_lock():
logger.error("Failed to acquire sync lock")
return 4
try:
# Unlock credentials
logger.info("Unlocking credentials")
creds_service = CredentialsService()
unlock_result = creds_service.unlock(passphrase)
if not unlock_result.get("success"):
error = unlock_result.get("error", "Unknown error")
logger.error("Failed to unlock credentials: %s", error)
record_failure(f"Unlock failed: {error}")
return 2
# Run sync
logger.info("Starting sync")
sync_service = SyncService()
result = await sync_service.full_sync()
if result.get("success"):
logger.info("Sync completed successfully")
clear_failures()
return 0
else:
error = result.get("error", "Unknown error")
logger.error("Sync failed: %s", error)
record_failure(f"Sync failed: {error}")
return 3
except Exception as e:
logger.exception("Unexpected error during sync: %s", e)
record_failure(f"Exception: {e}")
return 3
finally:
release_lock()
def main() -> int:
"""Main entry point."""
setup_logging()
logger = logging.getLogger(__name__)
logger.info("=" * 60)
logger.info("Eclosion Background Sync CLI starting")
logger.info("=" * 60)
try:
exit_code = asyncio.run(run_sync())
except KeyboardInterrupt:
logger.info("Sync interrupted by user")
release_lock()
exit_code = 130
except Exception as e:
logger.exception("Fatal error: %s", e)
release_lock()
exit_code = 1
logger.info("Exiting with code %d", exit_code)
return exit_code
if __name__ == "__main__":
sys.exit(main())