// Publish is not used because of just example. func Publish(host, user, pass, subject, msg string, port int) error { lg.Info("Publish()") // Connection nc, err := Connection(host, user, pass, port) if err != nil { return err } defer nc.Close() // Publish message := []byte(msg) nc.Publish(subject, message) nc.Flush() if err := nc.LastError(); err != nil { return err } return nil }
//2.execMain() return value is whether executed scraping or not. func execMain(testFlg uint8) bool { lg.Info("getting teacher's information") var si *th.SiteInfo //m = new(sync.Mutex) if *jsPath != "" { //json si = th.LoadJSONFile(*jsPath) } else { //use build teacher data si = th.GetDefinedData() } for { //reset th.InitSavedTeachers() //scraping handleTeachers(si) //save checkSavedTeachers() //TODO:when integration test, send channel //execuite only once on heroku if herokuFlg == "1" { if rd.Get() != nil { rd.Get().RD.Close() } return true } else if testFlg == 1 { return true } time.Sleep(time.Duration(*interval) * time.Second) } }
// Connection is to connect NATS server func Connection(host, user, pass string, port int) (*nats.Conn, error) { lg.Info("Connection()") //nats://derek:pass@localhost:4222 //nats.DefaultURL var err error var nc *nats.Conn if host == "" { //"nats://*****:*****@%s:%d", user, pass, host, port)) } } if err != nil { return nil, err } return nc, nil }
// Subscribe is to subscribe to subject func (ch ChReceive) Subscribe(subject string) { lg.Info("Subscribe()") // Async Subscriber var counter int ch.Conn.Subscribe(subject, func(msg *nats.Msg) { counter++ //lg.Debugf("[%d]Received msg:%s", counter, msg) ch.ChCMsg <- msg }) ch.ChWait <- true //notification for being ready. //wait for finish <-ch.ChWait defer func() { ch.ChWait <- true //notification for finished }() ch.Conn.Flush() ch.Error = ch.Conn.LastError() return }