func (r *cronRuleset) loadMemory(self *bot.Self) { log.Println("cron: reading from memory") v := self.MemoryRead("cron", "attached") if err := json.Unmarshal(v, &r.attachedCrons); err != nil { log.Println("cron: error reading memory:", err, v) return } log.Println("cron: memory read") r.start() }
// Boot runs preparatory steps for ruleset execution func (r *rpcRuleset) Boot(self *bot.Self) { r.memoryRead = self.MemoryRead r.memorySave = self.MemorySave r.outCh = self.MessageProviderOut() r.mux.HandleFunc("/pop", r.httpPop) r.mux.HandleFunc("/send", r.httpSend) r.mux.HandleFunc("/memoryRead", r.httpMemoryRead) r.mux.HandleFunc("/memorySave", r.httpMemorySave) log.Println("rpc: listening", r.listener.Addr()) srv := &http.Server{Handler: r.mux} srv.SetKeepAlivesEnabled(false) go srv.Serve(r.listener) }
func (r *cronRuleset) attach(self bot.Self, ruleName, room string) string { r.mu.Lock() defer r.mu.Unlock() if _, ok := r.cronRules[ruleName]; !ok { return ruleName + " not found" } for _, rn := range r.attachedCrons[room] { if rn == ruleName { return ruleName + " already attached to this room" } } r.attachedCrons[room] = append(r.attachedCrons[room], ruleName) b, err := json.Marshal(r.attachedCrons) if err != nil { return fmt.Sprintf("error attaching %s: %v", ruleName, err) } self.MemorySave("cron", "attached", b) return ruleName + " attached to this room" }
func (r *cronRuleset) detach(self bot.Self, ruleName, room string) string { r.mu.Lock() defer r.mu.Unlock() if _, ok := r.attachedCrons[room]; !ok { return "room not found in cron memory" } var newRoom []string for _, rn := range r.attachedCrons[room] { if rn == ruleName { continue } newRoom = append(newRoom, rn) } r.attachedCrons[room] = newRoom b, err := json.Marshal(r.attachedCrons) if err != nil { return fmt.Sprintf("error detaching %s: %v", ruleName, err) } self.MemorySave("cron", "attached", b) return ruleName + " detached to this room" }
// Boot runs preparatory steps for ruleset execution func (r *pluginRuleset) Boot(self *bot.Self) { for _, pluginBin := range r.pluginBins { l, err := net.Listen("tcp4", "0.0.0.0:0") if err != nil { log.Println("could not start plugin %s. error while setting listener: %v", pluginBin, err) continue } log.Printf("plugin: starting %s", pluginBin) rs := rpc.New(l) rs.Boot(self) cmd := exec.Command(pluginBin) cmd.Env = os.Environ() cmd.Env = append(cmd.Env, fmt.Sprintf("GOCHATBOT_RPC_BIND=%s", l.Addr())) cmd.Env = append(cmd.Env, fmt.Sprintf("GOCHATBOT_NAME=%s", self.Name())) cmd.Stderr = os.Stderr if err := cmd.Start(); err != nil { log.Printf("plugin: %s - error: %v", pluginBin, err) log.Printf("plugin: %s closing listener.", pluginBin) l.Close() continue } r.plugins = append(r.plugins, rs) } }
// Boot runs preparatory steps for ruleset execution func (r *cronRuleset) Boot(self *bot.Self) { r.outCh = self.MessageProviderOut() r.loadMemory(self) }