Пример #1
0
// NewDaemon returns a pointer to a new Daemon struct with defaults set.
func NewDaemon(cfg *config.Config) *Daemon {
	var dbh *db.Handle
	logger := logrus.New()
	log.SetupLogger(logger)
	if *debugdb {
		logger.Level = logrus.DebugLevel
	}
	if cfg.DB.Type == "memory" {
		dbh = db.NewMemoryDBHandle(logger, false)
	} else {
		dbh = db.NewDBHandle(cfg.DB.Path, logger)
	}
	cc := make(chan *feedwatcher.FeedCrawlRequest, 1)
	rc := make(chan *feedwatcher.FeedCrawlResponse)
	mc := mail.CreateAndStartMailer(cfg).OutgoingMail

	return &Daemon{
		Config:    cfg,
		CrawlChan: cc,
		RespChan:  rc,
		MailChan:  mc,
		DBH:       dbh,
		Feeds:     make(map[string]*feedwatcher.FeedWatcher),
		PollFeeds: true,
		Logger:    logger,
	}
}
Пример #2
0
func (fc *feedCommand) runone(c *kingpin.ParseContext) error {
	fc.init()
	fc.Config.Mail.SendMail = true
	fc.Config.DB.UpdateDb = false

	mailer := mail.CreateAndStartMailer(fc.Config)

	feed, err := fc.DBH.GetFeedByURL(fc.FeedURL)
	if err != nil {
		return err
	}

	httpCrawlChannel := make(chan *feedwatcher.FeedCrawlRequest, 1)
	responseChannel := make(chan *feedwatcher.FeedCrawlResponse)

	// start crawler pool
	crawler.StartCrawlerPool(1, httpCrawlChannel)

	fw := feedwatcher.NewFeedWatcher(
		*feed,
		httpCrawlChannel,
		responseChannel,
		mailer.OutgoingMail,
		fc.DBH,
		[]string{},
		10,
		100,
	)
	feeds := make(map[string]*feedwatcher.FeedWatcher)
	feeds[fw.FeedInfo.URL] = fw
	if fc.Loops == -1 {
		for {
			resp := fw.CrawlFeed()
			err := fw.UpdateFeed(resp)
			if err != nil {
				fmt.Printf("Error when updating feed: %v\n", err)
			}
			time.Sleep(time.Second * time.Duration(fc.Config.Crawl.MinInterval))
		}
	} else if fc.Loops == 1 {
		resp := fw.CrawlFeed()
		err := fw.UpdateFeed(resp)
		if err != nil {
			fmt.Printf("Error when updating feed: %v\n", err)
		}
	} else {
		for i := 0; i < fc.Loops; i++ {
			resp := fw.CrawlFeed()
			err := fw.UpdateFeed(resp)
			if err != nil {
				fmt.Printf("Error when updating feed: %v\n", err)
			}
			time.Sleep(time.Second * time.Duration(fc.Config.Crawl.MinInterval))
		}
	}
	return nil
}