Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(workers): worker input error #147

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ insert_final_newline = true
[*.{kt,kts}]
ktlint_standard_multiline-expression-wrapping = disabled
ktlint_standard_string-template-indent = disabled
ktlint_standard_import-ordering = disabled

Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,16 @@ class AppWorkerManager(
DescriptorUpdateWorker.ManualUpdateWorkerName,
ExistingWorkPolicy.REPLACE,
OneTimeWorkRequestBuilder<DescriptorUpdateWorker>()
.setInputData(descriptors?.let { DescriptorUpdateWorker.buildWorkData(it) } ?: Data.EMPTY)
.setInputData(
descriptors?.let {
DescriptorUpdateWorker.buildWorkData(
it.map {
descriptor ->
descriptor.id
},
)
} ?: Data.EMPTY,
)
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.build(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,18 @@ class DescriptorUpdateWorker(
override suspend fun doWork(): Result {
return coroutineScope {
val descriptors = getDescriptors() ?: return@coroutineScope Result.failure()
if (descriptors.isEmpty()) return@coroutineScope Result.success(buildWorkData(descriptors))
if (descriptors.isEmpty()) return@coroutineScope Result.success(buildWorkData(descriptors.map { it.id }))
dependencies.getDescriptorUpdate.invoke(descriptors)
return@coroutineScope Result.success(buildWorkData(descriptors))
return@coroutineScope Result.success(buildWorkData(descriptors.map { it.id }))
}
}

private suspend fun getDescriptors(): List<InstalledTestDescriptorModel>? {
val descriptorsJson = inputData.getString(DATA_KEY_DESCRIPTORS)
if (descriptorsJson != null) {
try {
return json.decodeFromString<List<InstalledTestDescriptorModel>>(descriptorsJson)
val ids = json.decodeFromString<List<InstalledTestDescriptorModel.Id>>(descriptorsJson)
return testDescriptorRepository.selectByRunIds(ids).first()
} catch (e: Exception) {
Logger.w("Could not start update worker: invalid configuration", e)
return null
Expand Down Expand Up @@ -83,7 +84,7 @@ class DescriptorUpdateWorker(
}

companion object {
fun buildWorkData(descriptors: List<InstalledTestDescriptorModel>): Data {
fun buildWorkData(descriptors: List<InstalledTestDescriptorModel.Id>): Data {
return workDataOf(
DATA_KEY_DESCRIPTORS to Dependencies.buildJson().encodeToString(descriptors),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class TestDescriptorRepository(
.mapToList(backgroundDispatcher)
.map { list -> list.mapNotNull { it.toModel() } }

fun selectByRunIds(ids: List<InstalledTestDescriptorModel.Id>) =
database.testDescriptorQueries
.selectByRunIds(ids.map { it.value })
.asFlow()
.mapToList(backgroundDispatcher)
.map { list -> list.mapNotNull { it.toModel() } }

suspend fun createOrIgnore(models: List<InstalledTestDescriptorModel>) {
withContext(backgroundDispatcher) {
database.transaction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ fun DescriptorScreen(
}
if (state.isRefreshing) {
UpdateProgressStatus(
modifier = Modifier.align(Alignment.BottomCenter).padding(bottom = 48.dp),
modifier = Modifier.align(Alignment.BottomCenter)
.padding(WindowInsets.navigationBars.asPaddingValues()),
type = state.refreshType,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,8 @@ UPDATE TestDescriptor SET auto_update = ? WHERE runId = ?;
selectAll:
SELECT * FROM TestDescriptor;

selectByRunIds:
SELECT * FROM TestDescriptor WHERE runId IN ?;

deleteByRunId:
DELETE FROM TestDescriptor WHERE runId = ?;