refactor: let the periodic work run more frequently and never fail from excessive retries preventing from re-running.

remove resume pending jobs from ApplicationContext onCreate and handle in home activity's onCreate instead.

prevent some illegal argument exceptions from Random.kt by returning null if empty
This commit is contained in:
jubb 2021-05-10 17:07:10 +10:00
parent 11a89c0a76
commit 8439d57115
5 changed files with 11 additions and 27 deletions

View File

@ -327,7 +327,6 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
.setJobStorage(new FastJobStorage(DatabaseFactory.getJobDatabase(this)))
.setDependencyInjector(this)
.build());
JobQueue.getShared().resumePendingJobs();
}
private void initializeDependencyInjection() {
@ -455,7 +454,6 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
poller.setUserPublicKey(userPublicKey);
return;
}
LokiAPIDatabase apiDB = DatabaseFactory.getLokiAPIDatabase(this);
poller = new Poller();
closedGroupPoller = new ClosedGroupPoller();
}

View File

@ -17,7 +17,6 @@ import org.session.libsession.snode.SnodeAPI
import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsignal.utilities.logging.Log
import org.thoughtcrime.securesms.database.DatabaseFactory
import java.io.IOException
import java.util.concurrent.TimeUnit
class BackgroundPollWorker(val context: Context, params: WorkerParameters) : Worker(context, params) {
@ -25,26 +24,10 @@ class BackgroundPollWorker(val context: Context, params: WorkerParameters) : Wor
companion object {
const val TAG = "BackgroundPollWorker"
private const val RETRY_ATTEMPTS = 3
@JvmStatic
fun scheduleInstant(context: Context) {
val workRequest = OneTimeWorkRequestBuilder<BackgroundPollWorker>()
.setConstraints(Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
)
.build()
WorkManager
.getInstance(context)
.enqueue(workRequest)
}
@JvmStatic
fun schedulePeriodic(context: Context) {
Log.v(TAG, "Scheduling periodic work.")
val workRequest = PeriodicWorkRequestBuilder<BackgroundPollWorker>(15, TimeUnit.MINUTES)
val workRequest = PeriodicWorkRequestBuilder<BackgroundPollWorker>(5, TimeUnit.MINUTES)
.setConstraints(Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
@ -55,7 +38,7 @@ class BackgroundPollWorker(val context: Context, params: WorkerParameters) : Wor
.getInstance(context)
.enqueueUniquePeriodicWork(
TAG,
ExistingPeriodicWorkPolicy.KEEP,
ExistingPeriodicWorkPolicy.REPLACE,
workRequest
)
}
@ -105,9 +88,8 @@ class BackgroundPollWorker(val context: Context, params: WorkerParameters) : Wor
return Result.success()
} catch (exception: Exception) {
Log.v(TAG, "Background poll failed due to error: ${exception.message}.", exception)
return if (runAttemptCount < RETRY_ATTEMPTS) Result.retry() else Result.failure()
Log.e(TAG, "Background poll failed due to error: ${exception.message}.", exception)
return Result.retry()
}
}
@ -116,8 +98,7 @@ class BackgroundPollWorker(val context: Context, params: WorkerParameters) : Wor
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
Log.v(TAG, "Boot broadcast caught.")
BackgroundPollWorker.scheduleInstant(context)
BackgroundPollWorker.schedulePeriodic(context)
schedulePeriodic(context)
}
}
}

View File

@ -31,7 +31,7 @@ class PublicChatManager(private val context: Context) {
refreshChatsAndPollers()
for ((threadID, _) in chats) {
val poller = pollers[threadID]
areAllCaughtUp = if (poller != null) areAllCaughtUp && poller.isCaughtUp else true
areAllCaughtUp = if (poller != null) areAllCaughtUp && poller.isCaughtUp else areAllCaughtUp
}
return areAllCaughtUp
}
@ -42,6 +42,9 @@ class PublicChatManager(private val context: Context) {
val poller = pollers[threadID] ?: OpenGroupPoller(chat, executorService)
poller.isCaughtUp = false
}
for ((_,poller) in v2Pollers) {
poller.isCaughtUp = false
}
}
public fun startPollersIfNeeded() {

View File

@ -6,6 +6,7 @@ import java.security.SecureRandom
* Uses `SecureRandom` to pick an element from this collection.
*/
fun <T> Collection<T>.getRandomElementOrNull(): T? {
if (isEmpty()) return null
val index = SecureRandom().nextInt(size) // SecureRandom() should be cryptographically secure
return elementAtOrNull(index)
}

View File

@ -6,6 +6,7 @@ import java.security.SecureRandom
* Uses `SecureRandom` to pick an element from this collection.
*/
fun <T> Collection<T>.getRandomElementOrNull(): T? {
if (isEmpty()) return null
val index = SecureRandom().nextInt(size) // SecureRandom() should be cryptographically secure
return elementAtOrNull(index)
}