From ba9a8eab881fc0f135ff59e47451848fe835a1ab Mon Sep 17 00:00:00 2001 From: "bparodi@lezzo.org" Date: Sat, 16 Nov 2024 12:30:29 +0100 Subject: [PATCH] job runner --- src/Cron.fs | 6 ------ src/Datatypes.fs | 1 + src/Grains.fs | 15 ++++++++++----- src/Library.fs | 5 +++++ src/Shell.fs | 49 +++++++++++++++++++++++++++++++++++++++++++++++- src/src.fsproj | 2 +- 6 files changed, 65 insertions(+), 13 deletions(-) diff --git a/src/Cron.fs b/src/Cron.fs index d2e1a2b..22cb974 100644 --- a/src/Cron.fs +++ b/src/Cron.fs @@ -11,15 +11,10 @@ open Pentole.String open Pentole.Map open Pentole - - - type PatternType = After of string - type WhenExpr = | Cron of Instant | Pattern of PatternType - type CronJobDefinition = { job_name: string user: string @@ -42,7 +37,6 @@ let private convert def = { } - let local_tz = DateTimeZoneProviders.Tzdb.GetSystemDefault () let local_tz_net = System.TimeZoneInfo.Local diff --git a/src/Datatypes.fs b/src/Datatypes.fs index 29d32ba..bfece5b 100644 --- a/src/Datatypes.fs +++ b/src/Datatypes.fs @@ -8,6 +8,7 @@ open Pentole.Path [] type Notification = | Time | Database +[] type CronJob = { job_name: string user: string diff --git a/src/Grains.fs b/src/Grains.fs index 342701e..54c1447 100644 --- a/src/Grains.fs +++ b/src/Grains.fs @@ -18,9 +18,14 @@ type ShellGrain() = interface IShellGrain with member _.schedule (ct) (jobs: ChainOfJobs) = task { - printfn "Grain view: --------" - printfn "HEAD= %A" jobs.head - jobs.rest |> List.iter (printfn "%A") - printfn "--------" - + // printfn "Grain view: --------" + // printfn "HEAD= %A" jobs.head + // jobs.rest |> List.iter (printfn "%A") + // printfn "--------" + let! rc = + jobs.head + |> Shell.run_job + match rc with + | Ok rc -> printfn "rc = OK %A" rc + | Error rc -> printfn "rc = Error %A" rc } diff --git a/src/Library.fs b/src/Library.fs index f48fa40..005a601 100644 --- a/src/Library.fs +++ b/src/Library.fs @@ -36,6 +36,11 @@ type Bidello(client: IClusterClient) = let schedule_jobs (jobs: ChainOfJobs) = let runner = rnd.Next () |> client.GetGrain + // let rc = + // Shell.run_job jobs.head + // |> Async.AwaitTask + // |> Async.RunSynchronously + // printfn "JOB= %A" rc runner.schedule ct jobs |> ignore task { diff --git a/src/Shell.fs b/src/Shell.fs index 9b8970e..bbec54f 100644 --- a/src/Shell.fs +++ b/src/Shell.fs @@ -1,10 +1,14 @@ module Bidello.Shell -open Sheller open FSharp.Control.LazyExtensions +open System.Collections.Generic +open Sheller open Pentole.Path +open Pentole + +open Bidello.Datatypes let private shell_ () = let possible_shells = [ @@ -48,3 +52,46 @@ let which (executable: string) = shell.Value |> Result.bind run + +type RunResult = + | Success of string | Failure of (int * string) + | Unknown of string | NoShell of string + +let run_job (cj: CronJob) = task { + + let workdir = cj.workdir |> function Absolute a -> a + "/" + let executable = cj.executable |> function Absolute a -> a + let user = cj.user + let env = + cj.environment + |> Seq.map (fun (k, v) -> KeyValuePair (k, v)) + + let run (shell: string) = task { + try + let! rc = + Builder + .UseShell(shell) + .WithEnvironmentVariables(env) + .UseExecutable(executable) + .WithArgument(Array.ofList cj.args) + .UseStartInfoTransform(fun si -> + si.WorkingDirectory <- workdir + si.UserName <- user + ) + .UseNoThrow() + .ExecuteAsync () + + if rc.ExitCode = 0 then + return rc.StandardOutput.Trim () |> Success + else + return Failure (rc.ExitCode, rc.StandardError) + with exn -> + return Unknown exn.Message + } + + match shell.Value with + | Error e -> return NoShell e + | Ok shell -> + printfn $"Running: {executable}|{shell}|{workdir}|{user}" + return! run shell +} diff --git a/src/src.fsproj b/src/src.fsproj index 03e87d2..804e42b 100644 --- a/src/src.fsproj +++ b/src/src.fsproj @@ -13,8 +13,8 @@ - +