Skip to content

Commit

Permalink
fix: fix coroutine issue
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 6, 2023
1 parent 9a9c421 commit 614ef7c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
16 changes: 12 additions & 4 deletions unit-picker/src/main/kotlin/cc/unitmesh/pick/picker/CodePicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.archguard.action.checkout.GitSourceSettings
import org.archguard.action.checkout.executeGitCheckout
import java.nio.file.Files
Expand All @@ -13,7 +14,7 @@ import kotlin.coroutines.coroutineContext
class CodePicker(val config: PickerConfig) {
private val logger = org.slf4j.LoggerFactory.getLogger(javaClass)

suspend fun execute() {
fun execute() = runBlocking {
val scope = CoroutineScope(coroutineContext)
scope.launch {
val codeDir = checkoutCode(config.url, config.branch, config.baseDir)
Expand All @@ -24,12 +25,19 @@ class CodePicker(val config: PickerConfig) {
logger.info("start picker")

launch {
PickDirectoryWalker(walkdirChannel).start(codeDir.toString())
walkdirChannel.close()
launch {
PickDirectoryWalker(walkdirChannel).start(codeDir.toString())
walkdirChannel.close()
}
launch {
for (fileJob in walkdirChannel) {
// todo call by language worker
println(fileJob)
}
}
}

logger.info("stop picker")

// 3. generate tree to jsonl
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,14 @@ class PickDirectoryWalker(
}

private suspend fun walk(workdir: String) = coroutineScope {
val dirents = readDir(workdir) ?: return@coroutineScope
val files = readDir(workdir)
if (files == null) {
logger.error("Failed to read directory: $workdir")
return@coroutineScope
}
val directoryContents = files.toList()

dirents.map {
directoryContents.map {
val name = it.name

if (name == ".gitignore" || it.name == ".ignore") {
Expand All @@ -82,12 +87,11 @@ class PickDirectoryWalker(
}
}

dirents.forEach { file ->
directoryContents.forEach { file ->
val name = file.name
val path = file.toString()
val isDir = file.isDirectory


for (deny in PathDenyList) {
if (path.contains(deny)) {
return@forEach
Expand Down Expand Up @@ -117,7 +121,7 @@ class PickDirectoryWalker(
}
}

if(dirChannels.size >= 1) {
if (dirChannels.isNotEmpty()) {
dirChannels.removeAt(0).close()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ class CodePickerTest {
PickerConfig(url = "https://github.com/unit-mesh/unit-eval-testing")
)

// CoroutineScope(Dispatchers.IO).launch {
// picker.execute()
// }
picker.execute()
}
}

0 comments on commit 614ef7c

Please sign in to comment.