/
proc.go
138 lines (125 loc) · 3.22 KB
/
proc.go
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
// Written in 2014 by Petar Maymounkov.
//
// It helps future understanding of past knowledge to save
// this notice, so peers of other times and backgrounds can
// see history clearly.
package circuit
import (
"fmt"
"log"
"sync"
"github.com/gocircuit/circuit/client"
"github.com/gocircuit/escher/be"
. "github.com/gocircuit/escher/circuit"
)
// Process
type Process struct {
sync.Once // start backloop once
spawn chan interface{} // notify loop of spawn memes
}
func (p *Process) Spark(*be.Eye, Circuit, ...interface{}) Value {
p.spawn = make(chan interface{})
return nil
}
func (p *Process) CognizeCommand(eye *be.Eye, dvalue interface{}) {
p.Once.Do(
func() {
back := &processBack{
eye: eye,
cmd: cognizeProcessCommand(dvalue),
spawn: p.spawn,
}
go back.loop()
},
)
}
func (p *Process) CognizeSpawn(eye *be.Eye, dvalue interface{}) {
p.spawn <- dvalue
log.Printf("circuit process spawning (%v)", String(dvalue))
}
func (p *Process) CognizeExit(eye *be.Eye, dvalue interface{}) {}
func (p *Process) CognizeIO(eye *be.Eye, dvalue interface{}) {}
// {
// Env {
// "PATH=/abc:/bin"
// "LESS=less"
// }
// Dir "/home/petar"
// Path "/bin/ls"
// Args { "-l", "/" }
// }
//
func cognizeProcessCommand(v interface{}) *client.Cmd {
img, ok := v.(Circuit)
if !ok {
panic(fmt.Sprintf("Non-image sent to Process.Command (%v)", v))
}
cmd := &client.Cmd{}
cmd.Path = img.StringAt("Path") // mandatory
if dir, ok := img.StringOptionAt("Dir"); ok {
cmd.Dir = dir
}
if env, ok := img.CircuitOptionAt("Env"); ok {
for _, key := range env.Numbers() {
cmd.Env = append(cmd.Env, env.StringAt(key))
}
}
if args, ok := img.CircuitOptionAt("Args"); ok {
for _, key := range args.Numbers() {
cmd.Args = append(cmd.Args, args.StringAt(key))
}
}
log.Printf("circuit process command (%v)", QuickPrint("", "t", -1, img))
return cmd
}
type processBack struct {
eye *be.Eye
cmd *client.Cmd
spawn <-chan interface{}
}
func (p *processBack) loop() {
for {
spwn := <-p.spawn
x := New().Grow("Spawn", spwn)
if exit := p.spawnProcess(spwn); exit != nil {
x.Grow("Exit", 1)
p.eye.Show("Exit", x)
} else {
x.Grow("Exit", 0)
p.eye.Show("Exit", x)
}
log.Printf("circuit process exit meme sent (%v)", Linearize(fmt.Sprintf("%v", x)))
}
}
func (p *processBack) spawnProcess(spwn interface{}) error {
// anchor determination
s := spwn.(Circuit)
anchor := program.Client.Walk(
[]string{
s.StringAt("Server"), // server name
s.StringAt("Name"), // (dynamic) execution name
})
//
proc, err := anchor.MakeProc(*p.cmd)
if err != nil {
panic("invalid command argument")
}
defer anchor.Scrub()
g := New().
Grow("Spawn", spwn).
Grow("Stdin", proc.Stdin()).
Grow("Stdout", proc.Stdout()).
Grow("Stderr", proc.Stderr())
log.Printf("circuit process io (%v)", Linearize(fmt.Sprintf("%v", spwn)))
p.eye.Show("IO", g)
log.Printf("circuit process waiting (%v)", Linearize(fmt.Sprintf("%v", spwn)))
stat, err := proc.Wait()
if err != nil {
panic("process wait aborted by user")
}
log.Printf("circuit process (%v) exited", Linearize(fmt.Sprintf("%v", spwn)))
if stat.Exit != nil {
log.Printf("circuit process exit error: %v", stat.Exit)
}
return stat.Exit
}