Skip to content

Commit f02f31c

Browse files
committed
fix: better mac support
1 parent 9112a46 commit f02f31c

File tree

6 files changed

+41
-14
lines changed

6 files changed

+41
-14
lines changed

serverhost-runtime/src/main/kotlin/app/simplecloud/droplet/serverhost/runtime/config/environment/generators/ScreenEnvironmentConfigGenerator.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import app.simplecloud.droplet.serverhost.runtime.config.environment.Environment
55
import app.simplecloud.droplet.serverhost.runtime.config.environment.EnvironmentStartConfig
66
import app.simplecloud.droplet.serverhost.runtime.launcher.ServerHostStartCommand
77
import app.simplecloud.droplet.serverhost.runtime.util.ScreenCapabilities
8+
import app.simplecloud.droplet.serverhost.shared.hack.OS
89
import java.nio.file.Paths
910
import kotlin.io.path.absolutePathString
1011

@@ -39,7 +40,7 @@ object ScreenEnvironmentConfigGenerator : EnvironmentConfigGenerator {
3940
return EnvironmentConfig(
4041
name = getName(),
4142
isScreen = true,
42-
useScreenStop = true,
43+
useScreenStop = OS.get() == OS.LINUX,
4344
start = EnvironmentStartConfig(
4445
command = command
4546
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package app.simplecloud.droplet.serverhost.runtime.process
2+
3+
class MacProcessInfo(private val pid: Long) : ProcessInfo {
4+
override fun getCommand(): String {
5+
val cmd = Runtime.getRuntime().exec(arrayOf("ps", "-p", pid.toString(), "-o", "command="))
6+
return cmd.inputReader(Charsets.UTF_8).readText().trim()
7+
}
8+
9+
override fun getRamAndCpuPercent(): Pair<Double, Double> {
10+
val cmd = Runtime.getRuntime()
11+
.exec(arrayOf("ps", "-p", pid.toString(), "-o", "%cpu,%mem="))
12+
var cpu = 0.0
13+
var mem = 0.0
14+
cmd.inputReader(Charsets.UTF_8).use { reader ->
15+
reader.readLines().forEach { line ->
16+
val resultString = line.trim()
17+
val result = resultString.split("\\s+".toRegex())
18+
if (result.size >= 2) {
19+
cpu = result[0].toDoubleOrNull() ?: 0.0
20+
mem = result[1].toDoubleOrNull() ?: 0.0
21+
}
22+
}
23+
}
24+
return Pair(mem, cpu)
25+
}
26+
27+
override fun asHandle(): ProcessHandle {
28+
return ProcessHandle.of(pid).get()
29+
}
30+
}

serverhost-runtime/src/main/kotlin/app/simplecloud/droplet/serverhost/runtime/process/ProcessInfo.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,9 @@ interface ProcessInfo {
2626

2727
fun of(pid: Long): ProcessInfo {
2828
return when(OS.get()) {
29-
OS.WINDOWS -> {
30-
WindowsProcessInfo(pid)
31-
}
32-
33-
OS.UNIX -> {
34-
UnixProcessInfo(pid)
35-
}
36-
29+
OS.WINDOWS -> WindowsProcessInfo(pid)
30+
OS.LINUX -> UnixProcessInfo(pid)
31+
OS.MAC -> MacProcessInfo(pid)
3732
null -> throw IllegalArgumentException("Unknown OS")
3833
}
3934
}

serverhost-runtime/src/main/kotlin/app/simplecloud/droplet/serverhost/runtime/util/ProcessDirectory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object ProcessDirectory {
1111
fun of(handle: ProcessHandle): Optional<Path> {
1212
val os = OS.get() ?: return Optional.empty()
1313
return when (os) {
14-
OS.UNIX -> {
14+
OS.LINUX, OS.MAC -> {
1515
ofUnix(handle.pid())
1616
}
1717
OS.WINDOWS -> {

serverhost-shared/src/main/kotlin/app/simplecloud/droplet/serverhost/shared/hack/OS.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package app.simplecloud.droplet.serverhost.shared.hack
22

33
enum class OS(val names: List<String>) {
44
WINDOWS(listOf("windows")),
5-
UNIX(listOf("mac", "linux"));
5+
LINUX(listOf("linux")),
6+
MAC(listOf("mac"));
67

78
companion object {
89
fun get(): OS? {
@@ -15,4 +16,4 @@ enum class OS(val names: List<String>) {
1516
return null
1617
}
1718
}
18-
}
19+
}

serverhost-shared/src/main/kotlin/app/simplecloud/droplet/serverhost/shared/hack/PortProcessHandle.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ object PortProcessHandle {
1717
fun of(port: Int): Optional<ProcessHandle> {
1818
val os = OS.get() ?: return Optional.empty()
1919
val command = when (os) {
20-
OS.UNIX -> arrayOf("sh", "-c", "lsof -i :$port | awk '{print \$2}'")
20+
OS.LINUX, OS.MAC -> arrayOf("sh", "-c", "lsof -i :$port | awk '{print \$2}'")
2121
OS.WINDOWS -> arrayOf("cmd", "/c", "netstat -ano | findstr $port")
2222
}
2323

@@ -37,7 +37,7 @@ object PortProcessHandle {
3737

3838
private fun parseProcessIdOrNull(os: OS, line: String): Long? {
3939
return when (os) {
40-
OS.UNIX -> line.toLongOrNull()
40+
OS.LINUX, OS.MAC -> line.toLongOrNull()
4141
OS.WINDOWS -> {
4242
val matcher = windowsPattern.matcher(line)
4343
if (!matcher.matches()) {

0 commit comments

Comments
 (0)