| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | const log = require('./log'); | 
					
						
							|  |  |  | const rp = require('request-promise'); | 
					
						
							|  |  |  | const sql = require('./sql'); | 
					
						
							| 
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 |  |  | const options = require('./options'); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | const migration = require('./migration'); | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | const utils = require('./utils'); | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  | const config = require('./config'); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | const source_id = require('./source_id'); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | const notes = require('./notes'); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | const syncUpdate = require('./sync_update'); | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  | const SYNC_SERVER = config['Sync']['syncServerHost']; | 
					
						
							| 
									
										
										
										
											2017-11-01 22:36:26 -04:00
										 |  |  | const isSyncSetup = !!SYNC_SERVER; | 
					
						
							| 
									
										
										
										
											2017-11-09 20:06:33 -05:00
										 |  |  | const SYNC_TIMEOUT = config['Sync']['syncServerTimeout'] || 5000; | 
					
						
							| 
									
										
										
										
											2017-11-13 19:45:13 -05:00
										 |  |  | const SYNC_PROXY = config['Sync']['syncProxy']; | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | let syncInProgress = false; | 
					
						
							| 
									
										
										
										
											2017-11-16 20:08:04 -05:00
										 |  |  | let proxyToggle = true; | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | async function sync() { | 
					
						
							|  |  |  |     if (syncInProgress) { | 
					
						
							|  |  |  |         log.info("Sync already in progress"); | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         return { | 
					
						
							|  |  |  |             success: false, | 
					
						
							|  |  |  |             message: "Sync already in progress" | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     syncInProgress = true; | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         if (!await migration.isDbUpToDate()) { | 
					
						
							|  |  |  |             log.info("DB not up to date"); | 
					
						
							| 
									
										
										
										
											2017-10-31 20:09:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |             return { | 
					
						
							|  |  |  |                 success: false, | 
					
						
							|  |  |  |                 message: "DB not up to date" | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         const syncContext = await login(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await pushSync(syncContext); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await pullSync(syncContext); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await pushSync(syncContext); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             success: true | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							| 
									
										
										
										
											2017-11-16 20:08:04 -05:00
										 |  |  |         proxyToggle = !proxyToggle; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         if (e.message.indexOf('ECONNREFUSED') !== -1) { | 
					
						
							|  |  |  |             log.info("No connection to sync server."); | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |             return { | 
					
						
							|  |  |  |                 success: false, | 
					
						
							|  |  |  |                 message: "No connection to sync server." | 
					
						
							|  |  |  |             }; | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         else { | 
					
						
							|  |  |  |             log.info("sync failed: " + e.stack); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return { | 
					
						
							|  |  |  |                 success: false, | 
					
						
							|  |  |  |                 message: e.message | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |     finally { | 
					
						
							|  |  |  |         syncInProgress = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function login() { | 
					
						
							|  |  |  |     const timestamp = utils.nowTimestamp(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const documentSecret = await options.getOption('document_secret'); | 
					
						
							|  |  |  |     const hash = utils.hmac(documentSecret, timestamp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const syncContext = { cookieJar: rp.jar() }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 23:25:23 -05:00
										 |  |  |     const resp = await syncRequest(syncContext, 'POST', '/api/login/sync', { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         timestamp: timestamp, | 
					
						
							|  |  |  |         dbVersion: migration.APP_DB_VERSION, | 
					
						
							|  |  |  |         hash: hash | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     syncContext.sourceId = resp.sourceId; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return syncContext; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function pullSync(syncContext) { | 
					
						
							|  |  |  |     const lastSyncedPull = parseInt(await options.getOption('last_synced_pull')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  |     const changesUri = '/api/sync/changed?lastSyncId=' + lastSyncedPull; | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const syncRows = await syncRequest(syncContext, 'GET', changesUri); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     log.info("Pulled " + syncRows.length + " changes from " + changesUri); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const sync of syncRows) { | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  |         if (source_id.isLocalSourceId(sync.source_id)) { | 
					
						
							|  |  |  |             log.info("Skipping " + sync.entity_name + " " + sync.entity_id + " because it has local source id."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 22:18:25 -05:00
										 |  |  |         console.log("Pulling ", sync); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         const resp = await syncRequest(syncContext, 'GET', "/api/sync/" + sync.entity_name + "/" + sync.entity_id); | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 20:09:07 -04:00
										 |  |  |         if (sync.entity_name === 'notes') { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |             await syncUpdate.updateNote(resp.entity, resp.links, syncContext.sourceId); | 
					
						
							| 
									
										
										
										
											2017-10-31 20:09:07 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if (sync.entity_name === 'notes_tree') { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |             await syncUpdate.updateNoteTree(resp, syncContext.sourceId); | 
					
						
							| 
									
										
										
										
											2017-10-31 20:09:07 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if (sync.entity_name === 'notes_history') { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |             await syncUpdate.updateNoteHistory(resp, syncContext.sourceId); | 
					
						
							| 
									
										
										
										
											2017-10-31 20:09:07 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-02 22:55:22 -04:00
										 |  |  |         else if (sync.entity_name === 'notes_reordering') { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |             await syncUpdate.updateNoteReordering(resp, syncContext.sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-02 22:55:22 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 |  |  |         else if (sync.entity_name === 'options') { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |             await syncUpdate.updateOptions(resp, syncContext.sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-05 00:16:02 -04:00
										 |  |  |         else if (sync.entity_name === 'recent_notes') { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |             await syncUpdate.updateRecentNotes(resp, syncContext.sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-05 00:16:02 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-10-31 20:09:07 -04:00
										 |  |  |         else { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |             throw new Error("Unrecognized entity type " + sync.entity_name); | 
					
						
							| 
									
										
										
										
											2017-10-31 20:09:07 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 |  |  |         await options.setOption('last_synced_pull', sync.id); | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     log.info("Finished pull"); | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | async function pushSync(syncContext) { | 
					
						
							|  |  |  |     let lastSyncedPush = parseInt(await options.getOption('last_synced_push')); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     while (true) { | 
					
						
							|  |  |  |         const sync = await sql.getSingleResultOrNull('SELECT * FROM sync WHERE id > ? LIMIT 1', [lastSyncedPush]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (sync === null) { | 
					
						
							|  |  |  |             // nothing to sync
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             log.info("Nothing to push"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             break; | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         if (sync.source_id === syncContext.sourceId) { | 
					
						
							|  |  |  |             log.info("Skipping sync " + sync.entity_name + " " + sync.entity_id + " because it originates from sync target"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             await readAndPushEntity(sync, syncContext); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         lastSyncedPush = sync.id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await options.setOption('last_synced_push', lastSyncedPush); | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-03 22:08:27 -04:00
										 |  |  | async function readAndPushEntity(sync, syncContext) { | 
					
						
							| 
									
										
										
										
											2017-11-01 23:16:21 -04:00
										 |  |  |     let entity; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (sync.entity_name === 'notes') { | 
					
						
							|  |  |  |         entity = await sql.getSingleResult('SELECT * FROM notes WHERE note_id = ?', [sync.entity_id]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (sync.entity_name === 'notes_tree') { | 
					
						
							| 
									
										
										
										
											2017-11-18 17:17:46 -05:00
										 |  |  |         entity = await sql.getSingleResult('SELECT * FROM notes_tree WHERE note_tree_id = ?', [sync.entity_id]); | 
					
						
							| 
									
										
										
										
											2017-11-01 23:16:21 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (sync.entity_name === 'notes_history') { | 
					
						
							|  |  |  |         entity = await sql.getSingleResult('SELECT * FROM notes_history WHERE note_history_id = ?', [sync.entity_id]); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-02 22:55:22 -04:00
										 |  |  |     else if (sync.entity_name === 'notes_reordering') { | 
					
						
							|  |  |  |         entity = { | 
					
						
							|  |  |  |             note_pid: sync.entity_id, | 
					
						
							| 
									
										
										
										
											2017-11-18 17:17:46 -05:00
										 |  |  |             ordering: await sql.getMap('SELECT note_tree_id, note_pos FROM notes_tree WHERE note_pid = ?', [sync.entity_id]) | 
					
						
							| 
									
										
										
										
											2017-11-02 22:55:22 -04:00
										 |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 |  |  |     else if (sync.entity_name === 'options') { | 
					
						
							|  |  |  |         entity = await sql.getSingleResult('SELECT * FROM options WHERE opt_name = ?', [sync.entity_id]); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-05 00:16:02 -04:00
										 |  |  |     else if (sync.entity_name === 'recent_notes') { | 
					
						
							| 
									
										
										
										
											2017-11-18 17:17:46 -05:00
										 |  |  |         entity = await sql.getSingleResult('SELECT * FROM recent_notes WHERE note_tree_id = ?', [sync.entity_id]); | 
					
						
							| 
									
										
										
										
											2017-11-05 00:16:02 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-01 23:16:21 -04:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         throw new Error("Unrecognized entity type " + sync.entity_name); | 
					
						
							| 
									
										
										
										
											2017-11-01 23:16:21 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 22:10:41 -04:00
										 |  |  |     if (!entity) { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         log.info("Sync entity for " + sync.entity_name + " " + sync.entity_id + " doesn't exist. Skipping."); | 
					
						
							| 
									
										
										
										
											2017-11-04 22:10:41 -04:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     log.info("Pushing changes in " + sync.entity_name + " " + sync.entity_id); | 
					
						
							| 
									
										
										
										
											2017-11-01 23:16:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     await sendEntity(syncContext, entity, sync.entity_name); | 
					
						
							| 
									
										
										
										
											2017-11-01 23:16:21 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | async function sendEntity(syncContext, entity, entityName) { | 
					
						
							|  |  |  |     const payload = { | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  |         sourceId: source_id.currentSourceId, | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         entity: entity | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     if (entityName === 'notes') { | 
					
						
							|  |  |  |         payload.links = await sql.getResults('select * from links where note_id = ?', [entity.note_id]); | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     await syncRequest(syncContext, 'PUT', '/api/sync/' + entityName, payload); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-28 22:17:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | async function syncRequest(syncContext, method, uri, body) { | 
					
						
							|  |  |  |     const fullUri = SYNC_SERVER + uri; | 
					
						
							| 
									
										
										
										
											2017-10-28 22:17:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2017-11-13 19:45:13 -05:00
										 |  |  |         const options = { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |             method: method, | 
					
						
							|  |  |  |             uri: fullUri, | 
					
						
							|  |  |  |             jar: syncContext.cookieJar, | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |             json: true, | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |             body: body, | 
					
						
							|  |  |  |             timeout: SYNC_TIMEOUT | 
					
						
							| 
									
										
										
										
											2017-11-13 19:45:13 -05:00
										 |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 20:08:04 -05:00
										 |  |  |         if (SYNC_PROXY && proxyToggle) { | 
					
						
							| 
									
										
										
										
											2017-11-13 19:45:13 -05:00
										 |  |  |             options.proxy = SYNC_PROXY; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return await rp(options); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         throw new Error("Request to " + method + " " + fullUri + " failed, inner exception: " + e.stack); | 
					
						
							| 
									
										
										
										
											2017-11-05 00:16:02 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:06:33 -05:00
										 |  |  | if (isSyncSetup) { | 
					
						
							|  |  |  |     log.info("Setting up sync to " + SYNC_SERVER + " with timeout " + SYNC_TIMEOUT); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 21:54:12 -05:00
										 |  |  |     if (SYNC_PROXY) { | 
					
						
							|  |  |  |         log.info("Sync proxy: " + SYNC_PROXY); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  |     setInterval(sync, 60000); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // kickoff initial sync immediately
 | 
					
						
							|  |  |  |     setTimeout(sync, 1000); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | else { | 
					
						
							|  |  |  |     log.info("Sync server not configured, sync timer not running.") | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2017-10-29 11:22:41 -04:00
										 |  |  |     sync, | 
					
						
							| 
									
										
										
										
											2017-11-01 22:36:26 -04:00
										 |  |  |     isSyncSetup | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | }; |