// // Activate network by sending out all IIPs // func (r *Runtime) Activate() { if len(r.iips) > 0 { // Connect to ports of IIP (so the components can resume execution) senders := make([]*zmq.Socket, len(r.iips)) for i, iip := range r.iips { senders[i], _ = zmq.NewSocket(zmq.PUSH) senders[i].Connect(iip.Socket) } defer func() { for i := range senders { senders[i].Close() } }() // Give some time to the network to deploy //TODO: replace this with info from processes (check all entries in processes hash map) time.Sleep(2 * time.Second) // Send IIPs out! log.SystemOutput("Activating processes by sending IIPs...") for i, iip := range r.iips { log.SystemOutput(fmt.Sprintf("Sending '%s' to socket '%s'", iip.Payload, iip.Socket)) senders[i].SendMessageDontwait(NewPacket([]byte(iip.Payload))) } } }
// // Start the network based on the current graph // func (r *Runtime) Start(dry bool) { err := r.prepareProcesses() if err != nil { log.ErrorOutput("Failed to create a process: " + err.Error()) r.Done <- true return } if len(r.processes) == 0 { log.SystemOutput("No processes to start") r.Done <- true return } if dry { r.Done <- true return } log.SystemOutput("Starting processes...") idx := 0 for name, ps := range r.processes { shutdownMutex.Lock() procWaitGroup.Add(1) ps.Stdin = nil ps.Stdout = log.DefaultFactory.CreateLog(name, idx, false) ps.Stderr = log.DefaultFactory.CreateLog(name, idx, true) ps.Start() go func(name string, ps *Process) { ps.Wait() procWaitGroup.Done() delete(r.processes, name) fmt.Fprintln(ps.Stdout, "Stopped") // Shutdown when no processes left, otherwise network should collapse // in a cascade way... //if !ps.cmd.ProcessState.Success() || len(r.processes) == 0 { if len(r.processes) == 0 { fmt.Fprintln(ps.Stdout, "I was the last running process. Calling runtime to SHUTDOWN") r.Shutdown() } }(name, ps) shutdownMutex.Unlock() idx++ } r.Activate() procWaitGroup.Wait() }
// // Shutdown the network // func (r *Runtime) Shutdown() { shutdownMutex.Lock() log.SystemOutput("Shutdown...") for name, ps := range r.processes { log.SystemOutput(fmt.Sprintf("terminating %s", name)) ps.cmd.Process.Signal(os.Kill) } shutdownMutex.Unlock() os.Exit(1) }
// // Shutdown the network // func (r *Runtime) Shutdown() { log.SystemOutput("Shutdown...") shutdownMutex.Lock() for name, ps := range r.processes { log.SystemOutput(fmt.Sprintf("sending SIGTERM to %s", name)) ps.Signal(syscall.SIGTERM) } if len(r.processes) == 0 { r.Done <- true } go func() { time.Sleep(3 * time.Second) for name, ps := range r.processes { log.SystemOutput(fmt.Sprintf("sending SIGKILL to %s", name)) ps.Signal(syscall.SIGKILL) } r.Done <- true }() shutdownMutex.Unlock() }