From 31bf8c9e5efdde8b076f0c8a5245434b5967cdb6 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 3 Apr 2024 12:50:49 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E3=82=BF=E3=82=B9=E3=82=AF=E5=AE=9F?= =?UTF-8?q?=E8=A1=8C=E9=83=A8=E5=88=86=E3=82=92=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consumer/src/main/kotlin/Main.kt | 40 ++++++++++++++++++- .../dev/usbharu/owl/consumer/TaskRequest.kt | 29 ++++++++++++++ .../dev/usbharu/owl/consumer/TaskResult.kt | 25 ++++++++++++ .../dev/usbharu/owl/consumer/TaskRunner.kt | 21 ++++++++++ 4 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 consumer/src/main/kotlin/dev/usbharu/owl/consumer/TaskRequest.kt create mode 100644 consumer/src/main/kotlin/dev/usbharu/owl/consumer/TaskResult.kt create mode 100644 consumer/src/main/kotlin/dev/usbharu/owl/consumer/TaskRunner.kt diff --git a/consumer/src/main/kotlin/Main.kt b/consumer/src/main/kotlin/Main.kt index e805e8d..3ff2d4a 100644 --- a/consumer/src/main/kotlin/Main.kt +++ b/consumer/src/main/kotlin/Main.kt @@ -1,6 +1,10 @@ package dev.usbharu +import dev.usbharu.dev.usbharu.owl.consumer.TaskRequest +import dev.usbharu.dev.usbharu.owl.consumer.TaskRunner import dev.usbharu.owl.* +import dev.usbharu.owl.common.property.CustomPropertySerializerFactory +import dev.usbharu.owl.common.property.PropertySerializeUtils import io.grpc.ManagedChannelBuilder import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.coroutineScope @@ -8,6 +12,8 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.flow.* import kotlinx.coroutines.isActive import kotlinx.coroutines.launch +import java.time.Instant +import java.util.* import kotlin.math.max suspend fun main() { @@ -29,6 +35,11 @@ suspend fun main() { this.tasks.addAll(listOf()) }) + + val map = mapOf() + + val propertySerializerFactory = CustomPropertySerializerFactory(setOf()) + val concurrent = MutableStateFlow(64) val processing = MutableStateFlow(0) @@ -66,13 +77,40 @@ suspend fun main() { processing.update { it + 1 } try { - emit(taskResult { + val taskResult = map[it.name]?.run( + TaskRequest( + it.name, + UUID(it.id.mostSignificantUuidBits, it.id.leastSignificantUuidBits), + it.attempt, + Instant.ofEpochSecond(it.queuedAt.seconds, it.queuedAt.nanos.toLong()), + PropertySerializeUtils.deserialize(propertySerializerFactory, it.propertiesMap) + ) + ) + + if (taskResult == null) { + throw Exception() + } + + emit(taskResult { + this.success = taskResult.success + this.attempt = it.attempt + this.id = it.id + this.result.putAll( + PropertySerializeUtils.serialize( + propertySerializerFactory, + taskResult.result + ) + ) + this.message = taskResult.message }) } catch (e: Exception) { emit(taskResult { this.success = false + this.attempt = it.attempt + this.id = it.id + this.message = e.localizedMessage }) } finally { processing.update { it - 1 } diff --git a/consumer/src/main/kotlin/dev/usbharu/owl/consumer/TaskRequest.kt b/consumer/src/main/kotlin/dev/usbharu/owl/consumer/TaskRequest.kt new file mode 100644 index 0000000..d66e9a7 --- /dev/null +++ b/consumer/src/main/kotlin/dev/usbharu/owl/consumer/TaskRequest.kt @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2024 usbharu + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.usbharu.dev.usbharu.owl.consumer + +import dev.usbharu.owl.common.property.PropertyValue +import java.time.Instant +import java.util.* + +data class TaskRequest( + val name:String, + val id:UUID, + val attempt:Int, + val queuedAt: Instant, + val properties:Map> +) diff --git a/consumer/src/main/kotlin/dev/usbharu/owl/consumer/TaskResult.kt b/consumer/src/main/kotlin/dev/usbharu/owl/consumer/TaskResult.kt new file mode 100644 index 0000000..20858a9 --- /dev/null +++ b/consumer/src/main/kotlin/dev/usbharu/owl/consumer/TaskResult.kt @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2024 usbharu + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.usbharu.dev.usbharu.owl.consumer + +import dev.usbharu.owl.common.property.PropertyValue + +data class TaskResult( + val success: Boolean, + val result: Map>, + val message: String +) diff --git a/consumer/src/main/kotlin/dev/usbharu/owl/consumer/TaskRunner.kt b/consumer/src/main/kotlin/dev/usbharu/owl/consumer/TaskRunner.kt new file mode 100644 index 0000000..44513ec --- /dev/null +++ b/consumer/src/main/kotlin/dev/usbharu/owl/consumer/TaskRunner.kt @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2024 usbharu + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.usbharu.dev.usbharu.owl.consumer + +fun interface TaskRunner { + suspend fun run(taskRequest: TaskRequest):TaskResult +} \ No newline at end of file