// Publish serializes and writes the given task to the // shared task topic and creates the unique topic and channel // on which the task's results will be published. func (self *Taskd) Publish(t *task.Task) (err error) { taskBody, err := task.Encode(t) if err != nil { return } frameType, _, err := self.writer.Publish(self.options.MsgTopic, taskBody) if err != nil || frameType == nsq.FrameTypeError { return } err = msg.CreateTopicChannel(t.ResponseTopic, t.ResponseChan, self.options.MsgSinkHttpAddr) return }
// Start configures the NSQWriter and checks if the tasks topic exists. // The topic is created if it does not exist. func (self *Taskd) Start(wg *sync.WaitGroup) (err error) { self.writer = nsq.NewWriter(self.options.MsgSinkTcpAddr) topicExists, err := msg.TopicExists(self.options.MsgTopic, []string{self.options.MsgSourceHttpAddr}) if err != nil { log.Fatalf("Could not query topic existance: %s", err) } if !topicExists { err = msg.CreateTopicChannel(self.options.MsgTopic, self.options.MsgChannel, self.options.MsgSourceHttpAddr) if err != nil { log.Fatalf("Creating topic failed: %s", err) } } self.wg = wg self.wg.Add(1) return }