engine, lang, lib, pgraph: Plumb through more ctx

This commit is contained in:
James Shubin
2025-10-02 23:28:51 -04:00
parent 6c6c9df75e
commit 303e80dee7
9 changed files with 21 additions and 19 deletions

View File

@@ -482,7 +482,7 @@ func (obj *ExecRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
cmdName = obj.IfShell // usually bash, or sh cmdName = obj.IfShell // usually bash, or sh
cmdArgs = []string{"-c", obj.IfCmd} cmdArgs = []string{"-c", obj.IfCmd}
} }
cmd := exec.Command(cmdName, cmdArgs...) cmd := exec.CommandContext(ctx, cmdName, cmdArgs...)
cmd.Dir = obj.IfCwd // run program in pwd if "" cmd.Dir = obj.IfCwd // run program in pwd if ""
// ignore signals sent to parent process (we're in our own group) // ignore signals sent to parent process (we're in our own group)
cmd.SysProcAttr = &syscall.SysProcAttr{ cmd.SysProcAttr = &syscall.SysProcAttr{
@@ -563,7 +563,7 @@ func (obj *ExecRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
cmdName = obj.NIfShell // usually bash, or sh cmdName = obj.NIfShell // usually bash, or sh
cmdArgs = []string{"-c", obj.NIfCmd} cmdArgs = []string{"-c", obj.NIfCmd}
} }
cmd := exec.Command(cmdName, cmdArgs...) cmd := exec.CommandContext(ctx, cmdName, cmdArgs...)
cmd.Dir = obj.NIfCwd // run program in pwd if "" cmd.Dir = obj.NIfCwd // run program in pwd if ""
// ignore signals sent to parent process (we're in our own group) // ignore signals sent to parent process (we're in our own group)
cmd.SysProcAttr = &syscall.SysProcAttr{ cmd.SysProcAttr = &syscall.SysProcAttr{
@@ -817,7 +817,7 @@ func (obj *ExecRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
cmdName = obj.DoneShell // usually bash, or sh cmdName = obj.DoneShell // usually bash, or sh
cmdArgs = []string{"-c", obj.DoneCmd} cmdArgs = []string{"-c", obj.DoneCmd}
} }
cmd := exec.Command(cmdName, cmdArgs...) cmd := exec.CommandContext(ctx, cmdName, cmdArgs...)
cmd.Dir = obj.DoneCwd // run program in pwd if "" cmd.Dir = obj.DoneCwd // run program in pwd if ""
// ignore signals sent to parent process (we're in our own group) // ignore signals sent to parent process (we're in our own group)
cmd.SysProcAttr = &syscall.SysProcAttr{ cmd.SysProcAttr = &syscall.SysProcAttr{

View File

@@ -197,7 +197,7 @@ func (obj *GroupRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
cmdName = "groupdel" cmdName = "groupdel"
} }
cmd := exec.Command(cmdName, args...) cmd := exec.CommandContext(ctx, cmdName, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{ cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true, Setpgid: true,
Pgid: 0, Pgid: 0,

View File

@@ -661,7 +661,7 @@ func TestResources1(t *testing.T) {
} }
t.Logf("test #%d: running CheckApply", index) t.Logf("test #%d: running CheckApply", index)
checkOK, err := res.CheckApply(doneCtx, true) // no noop! checkOK, err := res.CheckApply(context.TODO(), true) // no noop!
if err != nil { if err != nil {
t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: CheckApply failed: %s", index, err.Error()) t.Errorf("test #%d: CheckApply failed: %s", index, err.Error())

View File

@@ -5624,7 +5624,7 @@ func (obj *StmtProg) SetScope(scope *interfaces.Scope) error {
// debugging visualizations // debugging visualizations
if obj.data.Debug && orderingGraphSingleton { if obj.data.Debug && orderingGraphSingleton {
obj.data.Logf("running graphviz for ordering graph...") obj.data.Logf("running graphviz for ordering graph...")
if err := orderingGraph.ExecGraphviz("/tmp/graphviz-ordering.dot"); err != nil { if err := orderingGraph.ExecGraphviz(context.TODO(), "/tmp/graphviz-ordering.dot"); err != nil {
obj.data.Logf("graphviz: errored: %+v", err) obj.data.Logf("graphviz: errored: %+v", err)
} }
//if err := orderingGraphFiltered.ExecGraphviz("/tmp/graphviz-ordering-filtered.dot"); err != nil { //if err := orderingGraphFiltered.ExecGraphviz("/tmp/graphviz-ordering-filtered.dot"); err != nil {

View File

@@ -961,7 +961,7 @@ func (obj *Engine) Graph() *pgraph.Graph {
// ExecGraphviz writes out the diagram of a graph to be used for visualization // ExecGraphviz writes out the diagram of a graph to be used for visualization
// and debugging. You must not modify the graph (eg: during Lock) when calling // and debugging. You must not modify the graph (eg: during Lock) when calling
// this method. // this method.
func (obj *Engine) ExecGraphviz(dir string) error { func (obj *Engine) ExecGraphviz(ctx context.Context, dir string) error {
// No mutex needed here since this func runs in a non-concurrent Txn. // No mutex needed here since this func runs in a non-concurrent Txn.
// No mutex is needed at this time because we only run this in txn's and // No mutex is needed at this time because we only run this in txn's and
@@ -1019,7 +1019,7 @@ func (obj *Engine) ExecGraphviz(dir string) error {
}, },
} }
if err := gv.Exec(); err != nil { if err := gv.Exec(ctx); err != nil {
return err return err
} }
return nil return nil

View File

@@ -567,7 +567,7 @@ func TestAstFunc1(t *testing.T) {
} }
if runGraphviz { if runGraphviz {
t.Logf("test #%d: Running graphviz...", index) t.Logf("test #%d: Running graphviz...", index)
if err := fgraph.ExecGraphviz("/tmp/graphviz.dot"); err != nil { if err := fgraph.ExecGraphviz(context.TODO(), "/tmp/graphviz.dot"); err != nil {
t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: writing graph failed: %+v", index, err) t.Errorf("test #%d: writing graph failed: %+v", index, err)
return return
@@ -1120,7 +1120,7 @@ func TestAstFunc2(t *testing.T) {
} }
ast.ScopeGraph(graph) ast.ScopeGraph(graph)
if err := graph.ExecGraphviz("/tmp/set-scope.dot"); err != nil { if err := graph.ExecGraphviz(context.TODO(), "/tmp/set-scope.dot"); err != nil {
t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: writing graph failed: %+v", index, err) t.Errorf("test #%d: writing graph failed: %+v", index, err)
return return
@@ -1233,7 +1233,7 @@ func TestAstFunc2(t *testing.T) {
if runGraphviz { if runGraphviz {
t.Logf("test #%d: Running graphviz...", index) t.Logf("test #%d: Running graphviz...", index)
if err := fgraph.ExecGraphviz("/tmp/graphviz.dot"); err != nil { if err := fgraph.ExecGraphviz(context.TODO(), "/tmp/graphviz.dot"); err != nil {
t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: writing graph failed: %+v", index, err) t.Errorf("test #%d: writing graph failed: %+v", index, err)
return return
@@ -1996,7 +1996,7 @@ func TestAstFunc3(t *testing.T) {
} }
ast.ScopeGraph(graph) ast.ScopeGraph(graph)
if err := graph.ExecGraphviz("/tmp/set-scope.dot"); err != nil { if err := graph.ExecGraphviz(context.TODO(), "/tmp/set-scope.dot"); err != nil {
t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: writing graph failed: %+v", index, err) t.Errorf("test #%d: writing graph failed: %+v", index, err)
return return
@@ -2088,7 +2088,7 @@ func TestAstFunc3(t *testing.T) {
if runGraphviz { if runGraphviz {
t.Logf("test #%d: Running graphviz...", index) t.Logf("test #%d: Running graphviz...", index)
if err := fgraph.ExecGraphviz("/tmp/graphviz.dot"); err != nil { if err := fgraph.ExecGraphviz(context.TODO(), "/tmp/graphviz.dot"); err != nil {
t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: writing graph failed: %+v", index, err) t.Errorf("test #%d: writing graph failed: %+v", index, err)
return return

View File

@@ -1049,7 +1049,8 @@ func (obj *Main) Run() error {
obj.ge.Graph(): nil, obj.ge.Graph(): nil,
}, },
} }
if err := gv.Exec(); err != nil { // FIXME: is this the right ctx?
if err := gv.Exec(exitCtx); err != nil {
Logf("graphviz: %+v", err) Logf("graphviz: %+v", err)
} else { } else {
Logf("graphviz: successfully generated graph!") Logf("graphviz: successfully generated graph!")

View File

@@ -30,6 +30,7 @@
package pgraph // TODO: this should be a subpackage package pgraph // TODO: this should be a subpackage
import ( import (
"context"
"fmt" "fmt"
"html" "html"
"os" "os"
@@ -153,7 +154,7 @@ func (obj *Graphviz) Text() string {
// Exec writes out the graphviz data and runs the correct graphviz filter // Exec writes out the graphviz data and runs the correct graphviz filter
// command. // command.
func (obj *Graphviz) Exec() error { func (obj *Graphviz) Exec(ctx context.Context) error {
filter := "" filter := ""
switch obj.Filter { switch obj.Filter {
case "": case "":
@@ -195,7 +196,7 @@ func (obj *Graphviz) Exec() error {
} }
out := fmt.Sprintf("%s.png", filename) out := fmt.Sprintf("%s.png", filename)
cmd := exec.Command(path, "-Tpng", fmt.Sprintf("-o%s", out), filename) cmd := exec.CommandContext(ctx, path, "-Tpng", fmt.Sprintf("-o%s", out), filename)
if err1 == nil && err2 == nil { if err1 == nil && err2 == nil {
cmd.SysProcAttr = &syscall.SysProcAttr{} cmd.SysProcAttr = &syscall.SysProcAttr{}
@@ -286,7 +287,7 @@ func (obj *Graph) Graphviz() string {
// ExecGraphviz writes out the graphviz data and runs the correct graphviz // ExecGraphviz writes out the graphviz data and runs the correct graphviz
// filter command. // filter command.
func (obj *Graph) ExecGraphviz(filename string) error { func (obj *Graph) ExecGraphviz(ctx context.Context, filename string) error {
gv := &Graphviz{ gv := &Graphviz{
Graphs: map[*Graph]*GraphvizOpts{ Graphs: map[*Graph]*GraphvizOpts{
obj: nil, obj: nil,
@@ -296,5 +297,5 @@ func (obj *Graph) ExecGraphviz(filename string) error {
Filename: filename, Filename: filename,
//Hostname: hostname, //Hostname: hostname,
} }
return gv.Exec() return gv.Exec(ctx)
} }

View File

@@ -485,7 +485,7 @@ func TestTopoSort3(t *testing.T) {
G.AddEdge(v5, v6, e5) G.AddEdge(v5, v6, e5)
G.AddEdge(v4, v2, e6) // cycle G.AddEdge(v4, v2, e6) // cycle
G.ExecGraphviz("/tmp/g.dot") //G.ExecGraphviz(context.TODO(), "/tmp/g.dot")
_, err := G.TopologicalSort() _, err := G.TopologicalSort()
if err == nil { if err == nil {