Prevent duplicate threads

pull/175/head
M66B 5 years ago
parent fb98f07fb0
commit d6e82fc46a

File diff suppressed because it is too large Load Diff

@ -60,7 +60,7 @@ import io.requery.android.database.sqlite.SQLiteDatabase;
// https://developer.android.com/topic/libraries/architecture/room.html
@Database(
version = 146,
version = 147,
entities = {
EntityIdentity.class,
EntityAccount.class,
@ -1401,6 +1401,13 @@ public abstract class DB extends RoomDatabase {
Log.i("DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `account` ADD COLUMN `max_messages` INTEGER");
}
})
.addMigrations(new Migration(146, 147) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase db) {
Log.i("DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `account` ADD COLUMN `thread` INTEGER");
}
});
}

@ -122,6 +122,12 @@ public interface DaoAccount {
@Query("UPDATE account SET synchronize = :synchronize WHERE id = :id")
int setAccountSynchronize(long id, boolean synchronize);
@Query("UPDATE account SET thread = :thread WHERE id = :id")
int setAccountThread(long id, Long thread);
@Query("SELECT thread FROM account WHERE id = :id")
Long getAccountThread(long id);
@Query("UPDATE account SET state = :state WHERE id = :id")
int setAccountState(long id, String state);

@ -129,6 +129,7 @@ public class EntityAccount extends EntityOrder implements Serializable {
public Long created;
public Boolean tbd;
public Long thread;
public String state;
public String warning;
public String error;

@ -790,6 +790,14 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
try {
wlAccount.acquire();
final DB db = DB.getInstance(this);
final ExecutorService executor =
Helper.getBackgroundExecutor(1, "account_" + account.id);
long thread = Thread.currentThread().getId();
Long currentThread = thread;
db.account().setAccountThread(account.id, thread);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (account.notify)
account.createNotificationChannel(ServiceSynchronize.this);
@ -807,14 +815,11 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Log.w(account.name + " backoff " + ex.toString());
}
final DB db = DB.getInstance(this);
final ExecutorService executor =
Helper.getBackgroundExecutor(1, "account_" + account.id);
state.setBackoff(CONNECT_BACKOFF_START);
while (state.isRunning()) {
while (state.isRunning() &&
currentThread != null && currentThread.equals(thread)) {
state.reset();
Log.i(account.name + " run");
Log.i(account.name + " run thread=" + currentThread);
Handler handler = new Handler(getMainLooper());
final List<TwoStateOwner> cowners = new ArrayList<>();
@ -1573,7 +1578,12 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
else if (backoff < CONNECT_BACKOFF_AlARM_MAX * 60)
state.setBackoff(backoff * 2);
}
currentThread = Thread.currentThread().getId();
}
if (currentThread == null || !currentThread.equals(thread))
Log.e(account.name + " orphan thread id=" + currentThread + "/" + thread);
} finally {
EntityLog.log(this, account.name + " stopped");
wlAccount.release();

Loading…
Cancel
Save