summaryrefslogtreecommitdiff
path: root/src/SomhairlesDream.Modeling/BlenderBridge.fs
blob: c4215ae3a5f4bc023d4c4d37d417cc42a92494e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
namespace SomhairlesDream.Modeling

open System
open System.Diagnostics
open System.IO
open System.Text.Json

type BlenderProcessBridge(python: string, stageRoot: string, ?timeout: TimeSpan) =
    let timeout = defaultArg timeout (TimeSpan.FromSeconds 120.)

    let bridgeError message =
        if String.IsNullOrWhiteSpace(message) then
            "blender bridge: process failed without diagnostics"
        else
            $"blender bridge: {message.Trim()}"

    let diagnostics (stderr: string) (stdout: string) =
        let value =
            if String.IsNullOrWhiteSpace(stderr) then stdout else stderr

        if String.IsNullOrWhiteSpace(value) then
            "no process diagnostics"
        elif value.Length > 2000 then
            value.Substring(0, 2000)
        else
            value

    let strings (root: JsonElement) (name: string) =
        let property = root.GetProperty(name)

        if property.ValueKind <> JsonValueKind.Array then
            failwith $"'{name}' must be an array"

        let values = property.EnumerateArray() |> Seq.map (fun item -> item.GetString()) |> Seq.toArray

        if values |> Array.exists isNull then
            failwith $"'{name}' contains a null value"

        values

    let utcTimestamp (root: JsonElement) (name: string) =
        let value = root.GetProperty(name).GetDateTimeOffset()

        if value.Offset <> TimeSpan.Zero then
            failwith $"'{name}' must be a UTC timestamp"

        value

    let parseResult (request: BridgeRequest) =
        if not (File.Exists(request.ResultPath)) then
            failwith $"fixture result file is missing: {request.ResultPath}"

        use document = JsonDocument.Parse(File.ReadAllText(request.ResultPath))
        let root = document.RootElement

        if not (root.GetProperty("ok").GetBoolean()) then
            failwith "fixture returned ok=false"

        let renderPath =
            let mutable property = Unchecked.defaultof<JsonElement>

            if root.TryGetProperty("renderPath", &property) then
                Some(property.GetString())
            else
                None

        let renderedAt =
            let mutable property = Unchecked.defaultof<JsonElement>

            if root.TryGetProperty("renderedAt", &property) then
                Some(utcTimestamp root "renderedAt")
            else
                None

        if request.RenderPath.IsSome <> renderPath.IsSome then
            failwith "fixture render result did not match the request"

        if request.RenderPath.IsSome && request.RenderPath <> renderPath then
            failwith "fixture returned an unexpected render path"

        { BlenderVersion = root.GetProperty("blenderVersion").GetString()
          ExportedAt = utcTimestamp root "exportedAt"
          ObjectIds = strings root "objectIds"
          VerifiedObjectIds = strings root "verifiedObjectIds"
          GlbBytes = root.GetProperty("glbBytes").GetInt64()
          RenderPath = renderPath
          RenderedAt = renderedAt }

    let writeProcessLog (path: string) (stdout: string) (stderr: string) =
        match Path.GetDirectoryName(path) with
        | null -> ()
        | directory -> Directory.CreateDirectory(directory) |> ignore

        let content =
            String.concat
                Environment.NewLine
                [| "[stdout]"
                   stdout
                   "[stderr]"
                   stderr |]

        File.WriteAllText(path, content)

    member private _.ScriptPath(stepId: string) =
        let scriptName = if stepId.StartsWith("probe-") then "probe.py" else "fixture.py"

        Path.GetFullPath(Path.Combine(stageRoot, "blender", scriptName))

    member private this.StartInfo(request: BridgeRequest) =
        let scriptPath = this.ScriptPath(request.Step.StepId)
        let info = ProcessStartInfo()
        info.FileName <- python
        info.UseShellExecute <- false
        info.CreateNoWindow <- true
        info.RedirectStandardOutput <- true
        info.RedirectStandardError <- true
        info.ArgumentList.Add(scriptPath)
        info.ArgumentList.Add("--step-id")
        info.ArgumentList.Add(request.Step.StepId)
        info.ArgumentList.Add("--output")
        info.ArgumentList.Add(Path.GetFullPath(request.GlbPath))
        info.ArgumentList.Add("--result-output")
        info.ArgumentList.Add(Path.GetFullPath(request.ResultPath))

        match request.RenderPath with
        | Some path ->
            info.ArgumentList.Add("--render-output")
            info.ArgumentList.Add(Path.GetFullPath(path))
        | None -> ()

        info

    interface IArtifactBridge with
        member this.Export(request, heartbeat) =
            try
                let scriptPath = this.ScriptPath(request.Step.StepId)

                if not (File.Exists(scriptPath)) then
                    Error(bridgeError $"blender script is missing: {scriptPath}")
                else
                    match Path.GetDirectoryName(request.GlbPath) with
                    | null -> ()
                    | directory -> Directory.CreateDirectory(directory) |> ignore

                    match request.RenderPath with
                    | Some path ->
                        match Path.GetDirectoryName(path) with
                        | null -> ()
                        | directory -> Directory.CreateDirectory(directory) |> ignore
                    | None -> ()

                    match Path.GetDirectoryName(request.ResultPath) with
                    | null -> ()
                    | directory -> Directory.CreateDirectory(directory) |> ignore

                    match Path.GetDirectoryName(request.LogPath) with
                    | null -> ()
                    | directory -> Directory.CreateDirectory(directory) |> ignore

                    if File.Exists(request.ResultPath) then
                        File.Delete(request.ResultPath)

                    if File.Exists(request.LogPath) then
                        File.Delete(request.LogPath)

                    use childProcess = new Process()
                    childProcess.StartInfo <- this.StartInfo(request)

                    if not (childProcess.Start()) then
                        Error(bridgeError "process could not be started")
                    else
                        let stdoutTask = childProcess.StandardOutput.ReadToEndAsync()
                        let stderrTask = childProcess.StandardError.ReadToEndAsync()
                        let started = Stopwatch.StartNew()
                        let mutable timedOut = false

                        while not childProcess.HasExited && not timedOut do
                            let remaining = timeout - started.Elapsed

                            if remaining <= TimeSpan.Zero then
                                timedOut <- true
                            else
                                let waitMilliseconds = int (min 5000.0 remaining.TotalMilliseconds)

                                if not (childProcess.WaitForExit(waitMilliseconds)) then
                                    if childProcess.HasExited then
                                        ()
                                    else
                                        heartbeat ()

                        if timedOut && not childProcess.HasExited then
                            childProcess.Kill(true)

                        childProcess.WaitForExit()
                        let stdout = stdoutTask.GetAwaiter().GetResult()
                        let stderr = stderrTask.GetAwaiter().GetResult()
                        writeProcessLog request.LogPath stdout stderr

                        if timedOut then
                            Error(bridgeError $"process timed out after {timeout.TotalSeconds} seconds")
                        elif childProcess.ExitCode <> 0 then
                            Error(bridgeError $"process exited with code {childProcess.ExitCode}: {diagnostics stderr stdout}")
                        else
                            try
                                Ok(parseResult request)
                            with ex ->
                                Error(bridgeError $"invalid fixture result: {ex.Message}")
            with ex ->
                Error(bridgeError ex.Message)