@@ -12,7 +12,6 @@ import kotlinx.coroutines.CoroutineScope
1212import kotlinx.coroutines.Job
1313import kotlinx.coroutines.coroutineScope
1414import kotlinx.coroutines.launch
15- import kotlinx.coroutines.sync.Mutex
1615import org.wordpress.android.analytics.AnalyticsTracker.Stat
1716import org.wordpress.android.fluxc.Dispatcher
1817import org.wordpress.android.fluxc.generated.UploadActionBuilder
@@ -63,13 +62,6 @@ class UploadStarter @Inject constructor(
6362) : CoroutineScope {
6463 private val job = Job ()
6564
66- /* *
67- * When the app comes to foreground both `queueUploadFromAllSites` and `queueUploadFromSite` are invoked.
68- * The problem is that they can run in parallel and `uploadServiceFacade.isPostUploadingOrQueued(it)` might return
69- * out-of-date result and a same post is added twice.
70- */
71- private val mutex = Mutex ()
72-
7365 override val coroutineContext: CoroutineContext get() = job + bgDispatcher
7466
7567 /* *
@@ -141,40 +133,41 @@ class UploadStarter @Inject constructor(
141133
142134 /* *
143135 * This is meant to be used by [checkConnectionAndUpload] only.
136+ *
137+ * The method needs to be synchronized from the following reasons. When the app comes to foreground both
138+ * `queueUploadFromAllSites` and `queueUploadFromSite` are invoked. The problem is that they can run in parallel
139+ * and `uploadServiceFacade.isPostUploadingOrQueued(it)` might return out-of-date result and a same post is added
140+ * twice.
144141 */
142+ @Synchronized
145143 private suspend fun upload (site : SiteModel ) = coroutineScope {
146- try {
147- mutex.lock()
148- postStore.getPostsWithLocalChanges(site)
149- .asSequence()
150- .map { post ->
151- val action = uploadActionUseCase.getAutoUploadAction(post, site)
152- Pair (post, action)
153- }
154- .filter { (_, action) ->
155- action != DO_NOTHING
156- }
157- .toList()
158- .forEach { (post, action) ->
159- trackAutoUploadAction(action, post.status)
160- AppLog .d(
161- AppLog .T .POSTS ,
162- " UploadStarter for post title: ${post.title} , action: $action "
163- )
164- dispatcher.dispatch(
165- UploadActionBuilder .newIncrementNumberOfAutoUploadAttemptsAction(
166- post
167- )
168- )
169- uploadServiceFacade.uploadPost(
170- context = context,
171- post = post,
172- trackAnalytics = false
173- )
174- }
175- } finally {
176- mutex.unlock()
177- }
144+ postStore.getPostsWithLocalChanges(site)
145+ .asSequence()
146+ .map { post ->
147+ val action = uploadActionUseCase.getAutoUploadAction(post, site)
148+ Pair (post, action)
149+ }
150+ .filter { (_, action) ->
151+ action != DO_NOTHING
152+ }
153+ .toList()
154+ .forEach { (post, action) ->
155+ trackAutoUploadAction(action, post.status)
156+ AppLog .d(
157+ AppLog .T .POSTS ,
158+ " UploadStarter for post title: ${post.title} , action: $action "
159+ )
160+ dispatcher.dispatch(
161+ UploadActionBuilder .newIncrementNumberOfAutoUploadAttemptsAction(
162+ post
163+ )
164+ )
165+ uploadServiceFacade.uploadPost(
166+ context = context,
167+ post = post,
168+ trackAnalytics = false
169+ )
170+ }
178171 }
179172
180173 private fun trackAutoUploadAction (action : UploadAction , status : String ) {
0 commit comments