diff --git a/exec.prs b/exec.prs new file mode 100644 index 0000000..8760498 --- /dev/null +++ b/exec.prs @@ -0,0 +1,23 @@ +; Copied from ../syndicate-rs/syndicate-server/protocols/schemas/externalServices.prs +version 1 . + +Exec = . + +CommandLine = @shell string / @full FullCommandLine . +FullCommandLine = [@program string @args string ...] . + +RestartPolicy = +/ ; Whether the process terminates normally or abnormally, restart it + ; without affecting any peer processes within the service. + =always +/ ; If the process terminates normally, leave everything alone; if it + ; terminates abnormally, restart it without affecting peers. + @onError =on-error +/ ; If the process terminates normally, leave everything alone; if it + ; terminates abnormally, restart the whole daemon (all processes + ; within the daemon). + =all +/ ; Treat both normal and abnormal termination as normal termination; that is, never restart, + ; and enter state "complete" even if the process fails. + =never +. diff --git a/src/exec.nim b/src/exec.nim new file mode 100644 index 0000000..60a9717 --- /dev/null +++ b/src/exec.nim @@ -0,0 +1,32 @@ + +import + std/typetraits, preserves + +type + CommandLineKind* {.pure.} = enum + `shell`, `full` + CommandLineShell* = string + `CommandLine`* {.preservesOr.} = object + case orKind*: CommandLineKind + of CommandLineKind.`shell`: + `shell`*: CommandLineShell + + of CommandLineKind.`full`: + `full`*: FullCommandLine + + + Exec* {.preservesRecord: "exec".} = object + `argv`*: CommandLine + `restartPolicy`*: RestartPolicy + + `RestartPolicy`* {.preservesOr, pure.} = enum + `always`, `onError`, `all`, `never` + FullCommandLine* {.preservesTuple.} = object + `program`*: string + `args`* {.preservesTupleTail.}: seq[string] + +proc `$`*(x: CommandLine | Exec | FullCommandLine): string = + `$`(toPreserve(x)) + +proc encode*(x: CommandLine | Exec | FullCommandLine): seq[byte] = + encode(toPreserve(x)) diff --git a/src/uri_runner.nim b/src/uri_runner.nim index 48ef719..c97884e 100644 --- a/src/uri_runner.nim +++ b/src/uri_runner.nim @@ -1,15 +1,12 @@ # SPDX-FileCopyrightText: ☭ 2022 Emery Hemingway # SPDX-License-Identifier: Unlicense -import std/[asyncdispatch, deques, re, streams, strutils, osproc] -import preserves -import syndicate -import ./protocol +import std/[asyncdispatch, re] +import preserves, syndicate +import ./protocol, ./exec bootDataspace("main") do (root: Ref; turn: var Turn): - var - actions: seq[tuple[regex: Regex; cmd: string; args: seq[Assertion]]] - children: Deque[Process] + var actions: seq[tuple[regex: Regex; cmd: string; args: seq[Assertion]]] connectStdio(root, turn) @@ -26,12 +23,6 @@ bootDataspace("main") do (root: Ref; turn: var Turn): during(turn, root, ?ListenOn[Ref]) do (a: Assertion): let ds = unembed a onMessage(turn, ds, ?XdgOpen) do (uris: seq[string]): - while children.len > 0 and not children.peekFirst.running: - var child = children.popFirst() - if child.peekExitCode != 0: - stderr.writeLine child.errorStream.readAll - close child - # TODO check children on a timer? for uri in uris: var matched: bool for act in actions: @@ -46,9 +37,13 @@ bootDataspace("main") do (root: Ref; turn: var Turn): args[i] = uri else: args[i] = replacef(uri, act.regex, "$" & $arg.int) - var child = startProcess( - command = act.cmd, args = args, options = {}) - children.addLast child + message(turn, root, Exec( + argv: CommandLine( + orKind: CommandLineKind.full, + full: FullCommandLine( + program: act.cmd, + args: args)), + restartPolicy: RestartPolicy.never)) if not matched: stderr.writeLine "no actions matched for ", uri do: diff --git a/uri_runner.pr b/uri_runner.pr index 57800bb..dcb07ff 100644 --- a/uri_runner.pr +++ b/uri_runner.pr @@ -11,6 +11,8 @@ let ?root_ds = dataspace }> ? ?cap> [ + + ; send configuration to uri_runner $cap [ @@ -29,4 +31,20 @@ let ?root_ds = dataspace ; filesystem paths are always prefixed with file:// ] + + ; uri_runner sends messages to the server to start handler applications + $cap ?? [ + let ?id = timestamp + let ?facet = facet + let ?d = + $config > + $config + $config ? complete> [$facet ! stop] + $config ? failed> [$facet ! stop] + ] + ] diff --git a/xdg_open_ng.nimble b/xdg_open_ng.nimble index ed70d17..68bdf83 100644 --- a/xdg_open_ng.nimble +++ b/xdg_open_ng.nimble @@ -1,6 +1,6 @@ # Package -version = "0.3.2" +version = "0.4.0" author = "Emery" description = "A better xdg-open" license = "Unlicense"