Add a shortcut to restart the sync for an account from scratch.

This commit is contained in:
Karim Hamidou 2016-11-29 14:59:37 -08:00
parent 48fff4f4e1
commit ac818f30fe
4 changed files with 34 additions and 20 deletions

View file

@ -55,16 +55,13 @@ const buildAccountWith = ({name, email, provider, settings, credentials}) => {
account.setCredentials(credentials);
return account.save().then((saved) =>
AccountToken.create({accountId: saved.id}).then((token) =>
LocalDatabaseConnector.ensureAccountDatabase(saved.id).then(() => {
SyncProcessManager.addWorkerForAccount(saved);
return Promise.resolve({
account: saved,
token: token,
});
})
)
AccountToken.create({accountId: saved.id}).then((token) => {
SyncProcessManager.addWorkerForAccount(saved);
return Promise.resolve({
account: saved,
token: token,
});
})
);
});
});

View file

@ -45,7 +45,13 @@ class AccountCard extends React.Component {
SyncProcessManager.wakeWorkerForAccount(account);
});
})
})
});
}
onResetSync = () => {
SyncProcessManager.removeWorkerForAccountId(this.props.account.id);
LocalDatabaseConnector.destroyAccountDatabase(this.props.account.id);
SyncProcessManager.addWorkerForAccount(this.props.account);
}
renderError() {
@ -89,6 +95,7 @@ class AccountCard extends React.Component {
style={{top: `${position.top}px`, left: `${position.left}px`}}
>
<h3>{account.emailAddress} [{account.id}]</h3>
<button name="Reset sync" onClick={this.onResetSync}>Reset sync</button>
<SyncbackRequestDetails accountId={account.id} />
<div className="stats">
<b>First Sync Duration (sec)</b>:

View file

@ -50,15 +50,18 @@ class SyncProcessManager {
}
addWorkerForAccount(account) {
return LocalDatabaseConnector.forAccount(account.id).then((db) => {
if (this._workers[account.id]) {
return Promise.reject(new Error("Local worker already exists"));
}
return LocalDatabaseConnector.ensureAccountDatabase(account.id)
.then(() => {
return LocalDatabaseConnector.forAccount(account.id).then((db) => {
if (this._workers[account.id]) {
return Promise.reject(new Error("Local worker already exists"));
}
this._workers[account.id] = new SyncWorker(account, db, () => {
this.removeWorkerForAccountId(account.id)
});
return Promise.resolve();
this._workers[account.id] = new SyncWorker(account, db, () => {
this.removeWorkerForAccountId(account.id)
});
return Promise.resolve();
})
})
.then(() => {
this._logger.info({account_id: account.id}, `ProcessManager: Claiming Account Succeeded`)
@ -69,6 +72,8 @@ class SyncProcessManager {
}
removeWorkerForAccountId(accountId) {
const worker = this._workers[accountId];
worker.cleanup();
this._workers[accountId] = null;
}

View file

@ -60,7 +60,12 @@ class LocalDatabaseConnector {
destroyAccountDatabase(accountId) {
const dbname = `a-${accountId}`;
fs.removeFileSync(path.join(process.env.NYLAS_HOME, `${dbname}.sqlite`));
fs.access(dbname, fs.F_OK, (err) => {
if (!err) {
fs.unlinkSync(path.join(process.env.NYLAS_HOME, `${dbname}.sqlite`));
}
});
return Promise.resolve()
}