Reinstate auto-sync, and remove lots of old sync code

The on-change auto-sync now syncs only the modified library, and does so
quite efficiently (and should be able to be made more efficient), so we
might be able to reduce the timeout below 15 seconds.
This commit is contained in:
Dan Stillman 2016-04-01 02:24:50 -04:00
parent 9b231169b2
commit 514547ab3b
10 changed files with 101 additions and 1998 deletions

View file

@ -498,7 +498,7 @@ Zotero_Preferences.Sync = {
var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Components.interfaces.nsIPromptService);
if (!Zotero.Sync.Server.enabled) {
if (!Zotero.Sync.Runner.enabled) {
ps.alert(
null,
Zotero.getString('general.error'),

View file

@ -127,13 +127,13 @@
<row>
<box/>
<checkbox label="&zotero.preferences.sync.syncAutomatically;"
disabled="true"/>
preference="pref-sync-autosync"/>
</row>
<row>
<box/>
<checkbox label="&zotero.preferences.sync.syncFullTextContent;"
tooltiptext="&zotero.preferences.sync.syncFullTextContent.desc;"
disabled="true"/>
preference="pref-sync-fulltext-enabled"
tooltiptext="&zotero.preferences.sync.syncFullTextContent.desc;"/>
</row>
</rows>
</grid>

View file

@ -800,13 +800,6 @@ Zotero.DataObjects.prototype.isEditable = function (obj) {
return true;
}
Zotero.DataObjects.prototype.editCheck = function (obj) {
if (!Zotero.Sync.Server.updatesInProgress && !Zotero.Sync.Storage.updatesInProgress && !this.isEditable(obj)) {
throw ("Cannot edit " + this._ZDO_object + " in read-only Zotero library");
}
}
Zotero.defineProperty(Zotero.DataObjects.prototype, "primaryDataSQL", {
get: function () {
return "SELECT "

View file

@ -481,12 +481,6 @@ Zotero.Sync.Storage.Mode.ZFS.prototype = {
else if (req.status == 404) {
Components.utils.reportError("Unexpected status code 404 in upload authorization "
+ "request (" + item.libraryKey + ")");
if (Zotero.Prefs.get('sync.debugNoAutoResetClient')) {
Components.utils.reportError("Skipping automatic client reset due to debug pref");
}
if (!Zotero.Sync.Server.canAutoResetClient) {
Components.utils.reportError("Client has already been auto-reset -- manual sync required");
}
// TODO: Make an API request to fix this

File diff suppressed because it is too large Load diff

View file

@ -97,9 +97,18 @@ Zotero.Sync.EventListeners.ChangeListener = new function () {
Zotero.Sync.EventListeners.AutoSyncListener = {
_editTimeout: 15,
_observerID: null,
init: function () {
// Initialize save observer
Zotero.Notifier.registerObserver(this);
// If auto-sync is enabled, initialize the save observer
if (Zotero.Prefs.get('sync.autoSync')) {
this.register();
}
},
register: function () {
_observerID = Zotero.Notifier.registerObserver(this, false, 'autosync');
},
notify: function (event, type, ids, extraData) {
@ -108,8 +117,41 @@ Zotero.Sync.EventListeners.AutoSyncListener = {
return;
}
if (Zotero.Prefs.get('sync.autoSync') && Zotero.Sync.Server.enabled) {
Zotero.Sync.Runner.setSyncTimeout(false, false, true);
if (Zotero.Sync.Runner.syncInProgress) {
return;
}
// Only trigger sync for certain types
//
// TODO: settings, full text
if (Zotero.DataObjectUtilities.getTypes().indexOf(type) == -1) {
return;
}
// Determine affected libraries so only those can be synced
let libraryIDs = new Set();
if (Zotero.DataObjectUtilities.getTypes().indexOf(type) != -1) {
let objectsClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType(type);
ids.forEach(id => {
let lk = objectsClass.getLibraryAndKeyFromID(id);
if (lk) {
libraryIDs.add(lk.libraryID);
}
});
}
Zotero.Sync.Runner.setSyncTimeout(
this._editTimeout,
false,
{
libraries: libraryIDs.values()
}
);
},
unregister: function () {
if (_observerID) {
Zotero.Notifier.unregisterObserver(_observerID);
}
}
}
@ -136,7 +178,7 @@ Zotero.Sync.EventListeners.IdleListener = {
},
register: function () {
Zotero.debug("Initializing sync idle observer");
Zotero.debug("Registering auto-sync idle observer");
var idleService = Components.classes["@mozilla.org/widget/idleservice;1"]
.getService(Components.interfaces.nsIIdleService);
idleService.addIdleObserver(this, this._idleTimeout);
@ -148,9 +190,7 @@ Zotero.Sync.EventListeners.IdleListener = {
return;
}
if (!Zotero.Sync.Server.enabled
|| Zotero.Sync.Server.syncInProgress
|| Zotero.Sync.Storage.syncInProgress) {
if (!Zotero.Sync.Runner.enabled || Zotero.Sync.Runner.syncInProgress) {
return;
}
@ -167,10 +207,11 @@ Zotero.Sync.EventListeners.IdleListener = {
Zotero.debug("Beginning idle sync");
Zotero.Sync.Runner.setSyncTimeout(this._idleTimeout, true);
Zotero.Sync.Runner.sync({
background: true
});
Zotero.Sync.Runner.setSyncTimeout(this._idleTimeout, true, true);
},
_backObserver: {
@ -180,9 +221,7 @@ Zotero.Sync.EventListeners.IdleListener = {
}
Zotero.Sync.Runner.clearSyncTimeout();
if (!Zotero.Sync.Server.enabled
|| Zotero.Sync.Server.syncInProgress
|| Zotero.Sync.Storage.syncInProgress) {
if (!Zotero.Sync.Runner.enabled || Zotero.Sync.Runner.syncInProgress) {
return;
}
Zotero.debug("Beginning return-from-idle sync");
@ -193,7 +232,7 @@ Zotero.Sync.EventListeners.IdleListener = {
},
unregister: function () {
Zotero.debug("Stopping sync idle observer");
Zotero.debug("Unregistering auto-sync idle observer");
var idleService = Components.classes["@mozilla.org/widget/idleservice;1"]
.getService(Components.interfaces.nsIIdleService);
idleService.removeIdleObserver(this, this._idleTimeout);

View file

@ -33,7 +33,8 @@ if (!Zotero.Sync) {
Zotero.Sync.Runner_Module = function (options = {}) {
const stopOnError = false;
Zotero.defineProperty(this, 'background', { get: () => _background });
Zotero.defineProperty(this, 'enabled', { get: () => _apiKey || Zotero.Sync.Data.Local.getAPIKey() });
Zotero.defineProperty(this, 'syncInProgress', { get: () => _syncInProgress });
Zotero.defineProperty(this, 'lastSyncStatus', { get: () => _lastSyncStatus });
this.baseURL = options.baseURL || ZOTERO_CONFIG.API_URL;
@ -55,10 +56,11 @@ Zotero.Sync.Runner_Module = function (options = {}) {
}
}.bind(this);
var _enabled = false;
var _autoSyncTimer;
var _background;
var _firstInSession = true;
var _syncInProgress = false;
var _manualSyncRequired = false; // TODO: make public?
var _syncEngines = [];
var _storageEngines = [];
@ -130,7 +132,6 @@ Zotero.Sync.Runner_Module = function (options = {}) {
_firstInSession = false;
}
_background = !!options.background;
_syncInProgress = true;
this.updateIcons('animate');
@ -163,12 +164,15 @@ Zotero.Sync.Runner_Module = function (options = {}) {
this.addError(e);
}
}.bind(this),
background: _background,
background: !!options.background,
firstInSession: _firstInSession
};
let librariesToSync = yield this.checkLibraries(
client, options, keyInfo, options.libraries
client,
options,
keyInfo,
options.libraries ? Array.from(options.libraries) : []
);
// Sync data and files, and then repeat if necessary
let attempt = 1;
@ -290,15 +294,13 @@ Zotero.Sync.Runner_Module = function (options = {}) {
];
*/
var syncAllLibraries = !libraries.length;
var syncAllLibraries = !libraries || !libraries.length;
// TODO: Ability to remove or disable editing of user library?
if (syncAllLibraries) {
if (access.user && access.user.library) {
libraries.push(
Zotero.Libraries.userLibraryID, Zotero.Libraries.publicationsLibraryID
);
libraries = [Zotero.Libraries.userLibraryID, Zotero.Libraries.publicationsLibraryID];
}
}
else {
@ -698,15 +700,17 @@ Zotero.Sync.Runner_Module = function (options = {}) {
/**
* @param {Integer} [timeout=15] Timeout in seconds
* @param {Boolean} [recurring=false]
* @param {Boolean} [background] Triggered sync is a background sync
* @param {Integer} timeout - Timeout in seconds
* @param {Boolean} [recurring=false]
* @param {Object} [options] - Sync options
*/
this.setSyncTimeout = function (timeout, recurring, background) {
// check if server/auto-sync are enabled?
this.setSyncTimeout = function (timeout, recurring, options = {}) {
if (!Zotero.Prefs.get('sync.autoSync') || !this.enabled) {
return;
}
if (!timeout) {
var timeout = 15;
throw new Error("Timeout not provided");
}
if (_autoSyncTimer) {
@ -718,10 +722,15 @@ Zotero.Sync.Runner_Module = function (options = {}) {
createInstance(Components.interfaces.nsITimer);
}
var mergedOpts = {
background: true
};
Object.assign(mergedOpts, options);
// Implements nsITimerCallback
var callback = {
notify: function (timer) {
if (!Zotero.Sync.Server.enabled) {
if (!_getAPIKey()) {
return;
}
@ -730,25 +739,18 @@ Zotero.Sync.Runner_Module = function (options = {}) {
return;
}
if (Zotero.Sync.Storage.syncInProgress) {
Zotero.debug('Storage sync already in progress -- skipping auto-sync', 4);
return;
}
if (Zotero.Sync.Server.syncInProgress) {
if (_syncInProgress) {
Zotero.debug('Sync already in progress -- skipping auto-sync', 4);
return;
}
if (Zotero.Sync.Server.manualSyncRequired) {
if (_manualSyncRequired) {
Zotero.debug('Manual sync required -- skipping auto-sync', 4);
return;
}
this.sync({
background: background
});
}
this.sync(mergedOpts);
}.bind(this)
}
if (recurring) {
@ -758,12 +760,7 @@ Zotero.Sync.Runner_Module = function (options = {}) {
);
}
else {
if (Zotero.Sync.Storage.syncInProgress) {
Zotero.debug('Storage sync in progress -- not setting auto-sync timeout', 4);
return;
}
if (Zotero.Sync.Server.syncInProgress) {
if (_syncInProgress) {
Zotero.debug('Sync in progress -- not setting auto-sync timeout', 4);
return;
}

View file

@ -2431,20 +2431,14 @@ Zotero.Prefs = new function(){
Zotero.VersionHeader.unregister();
}
}],
[ "zoteroDotOrgVersionHeader", function(val) {
if (val) {
Zotero.VersionHeader.register();
}
else {
Zotero.VersionHeader.unregister();
}
}],
[ "sync.autoSync", function(val) {
if (val) {
Zotero.Sync.Runner.IdleListener.register();
Zotero.Sync.EventListeners.AutoSyncListener.register();
Zotero.Sync.EventListeners.IdleListener.register();
}
else {
Zotero.Sync.Runner.IdleListener.unregister();
Zotero.Sync.EventListeners.AutoSyncListener.unregister();
Zotero.Sync.EventListeners.IdleListener.unregister();
}
}],
[ "search.quicksearch-mode", function(val) {

View file

@ -406,30 +406,26 @@ var ZoteroPane = new function()
if (Zotero.Prefs.get('sync.autoSync')) {
yield Zotero.proxyAuthComplete.delay(1000);
if (!Zotero.Sync.Server.enabled) {
if (!Zotero.Sync.Runner.enabled) {
Zotero.debug('Sync not enabled -- skipping auto-sync', 4);
return;
}
if (Zotero.Sync.Server.syncInProgress || Zotero.Sync.Storage.syncInProgress) {
else if (Zotero.Sync.Runner.syncInProgress) {
Zotero.debug('Sync already running -- skipping auto-sync', 4);
return;
}
if (Zotero.Sync.Server.manualSyncRequired) {
else if (Zotero.Sync.Server.manualSyncRequired) {
Zotero.debug('Manual sync required -- skipping auto-sync', 4);
return;
}
Zotero.Sync.Runner.sync({
background: true
});
else {
Zotero.Sync.Runner.sync({
background: true
});
}
}
// Set sync icon to spinning or not
// Set sync icon to spinning if there's an existing sync
//
// We don't bother setting an error state at open
if (Zotero.Sync.Server.syncInProgress || Zotero.Sync.Storage.syncInProgress) {
// We don't bother setting an existing error state at open
if (Zotero.Sync.Runner.syncInProgress) {
Zotero.Sync.Runner.updateIcons('animate');
}

View file

@ -888,11 +888,6 @@ sync.status.uploadAccepted = Upload accepted \u2014 waiting for sync server
sync.status.syncingFiles = Syncing files
sync.status.syncingFullText = Syncing full-text content
sync.fulltext.upgradePrompt.title = New: Full-Text Content Syncing
sync.fulltext.upgradePrompt.text = Zotero can now sync the full-text content of files in your Zotero libraries with zotero.org and other linked devices, allowing you to easily search for your files wherever you are. The full-text content of your files will not be shared publicly.
sync.fulltext.upgradePrompt.changeLater = You can change this setting later from the Sync pane of the Zotero preferences.
sync.fulltext.upgradePrompt.enable = Use Full-Text Syncing
sync.storage.mbRemaining = %SMB remaining
sync.storage.kbRemaining = %SKB remaining
sync.storage.filesRemaining = %1$S/%2$S files