当前位置: 首页>>代码示例>>Golang>>正文


Golang Site.UpdateSiteStatus方法代码示例

本文整理汇总了Golang中github.com/turnkey-commerce/go-ping-sites/database.Site.UpdateSiteStatus方法的典型用法代码示例。如果您正苦于以下问题:Golang Site.UpdateSiteStatus方法的具体用法?Golang Site.UpdateSiteStatus怎么用?Golang Site.UpdateSiteStatus使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/turnkey-commerce/go-ping-sites/database.Site的用法示例。


在下文中一共展示了Site.UpdateSiteStatus方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestUpdateSiteStatus

// TestUpdateSiteStatus tests updating the up/down status of the site.
func TestUpdateSiteStatus(t *testing.T) {
	db, err := database.InitializeTestDB("")
	if err != nil {
		t.Fatal("Failed to create database:", err)
	}
	defer db.Close()

	// First create a site to update status.
	s := database.Site{SiteID: 1, Name: "Test", IsActive: true, URL: "http://www.google.com", PingIntervalSeconds: 60, TimeoutSeconds: 30}
	err = s.CreateSite(db)
	if err != nil {
		t.Fatal("Failed to create new site:", err)
	}

	// Update the status of the site to down
	err = s.UpdateSiteStatus(db, false)
	if err != nil {
		t.Fatal("Failed to update site status:", err)
	}

	//Get the saved site
	var updatedSite database.Site
	err = updatedSite.GetSite(db, s.SiteID)
	if err != nil {
		t.Fatal("Failed to retrieve updated site:", err)
	}

	if updatedSite.IsSiteUp != false {
		t.Errorf("Site status should be down.")
	}

	// Update the status of the site to up
	err = s.UpdateSiteStatus(db, true)
	if err != nil {
		t.Fatal("Failed to update site status:", err)
	}

	// Update the first ping time of the site.
	firstPingTime := time.Date(2015, time.November, 10, 23, 22, 22, 00, time.UTC)
	err = s.UpdateSiteFirstPing(db, firstPingTime)
	if err != nil {
		t.Fatal("Failed to update first ping time:", err)
	}

	err = updatedSite.GetSite(db, s.SiteID)
	if err != nil {
		t.Fatal("Failed to retrieve updated site:", err)
	}

	if updatedSite.IsSiteUp != true {
		t.Errorf("Site status should be up.")
	}

	if updatedSite.FirstPing != firstPingTime {
		t.Errorf("Site first ping time %s does not match input %s.", updatedSite.FirstPing, firstPingTime)
	}

}
开发者ID:turnkey-commerce,项目名称:go-ping-sites,代码行数:59,代码来源:database_test.go

示例2: ping

// ping does the actual pinging of the site and calls the notifications
func ping(s database.Site, db *sql.DB, requestURL URLRequester,
	sendEmail notifier.EmailSender, sendSms notifier.SmsSender, wg *sync.WaitGroup, stop chan struct{}) {
	defer wg.Done()
	// Initialize the previous state of site to the database value. On site creation will initialize to true.
	siteWasUp := s.IsSiteUp
	var statusChange bool
	var partialDetails string
	var partialSubject string
	for {
		// initialize statusChange to false and only notify on change of siteWasUp status
		statusChange = false
		// Check for a quit signal to stop the pinging
		select {
		case <-stop:
			log.Println("Stopping ", s.Name)
			return
		case <-time.After(time.Duration(s.PingIntervalSeconds) * time.Second):
			// Do nothing
		}
		if !s.IsActive {
			log.Println(s.Name, "Paused")
			continue
		}
		bodyContent, statusCode, responseTime, err := requestURL(s.URL, s.TimeoutSeconds)
		log.Println(s.Name, "Pinged")
		// Setup ping information for recording.
		p := database.Ping{SiteID: s.SiteID, TimeRequest: time.Now()}
		if err != nil {
			// Check if the error is due to the Internet not being Accessible
			if _, ok := err.(InternetAccessError); ok {
				log.Println(s.Name, "Unable to determine site status -", err)
				continue
			}
			log.Println(s.Name, "Error", err)
			if siteWasUp {
				statusChange = true
				partialSubject = "Site is Down"
				partialDetails = "Site is down, Error is " + err.Error()
			}
			siteWasUp = false

		} else if statusCode < 200 || statusCode > 299 { // Check if the status code is in the 2xx range.
			log.Println(s.Name, "Error - HTTP Status Code is", statusCode)
			if siteWasUp {
				statusChange = true
				partialSubject = "Site is Down"
				partialDetails = "Site is down, HTTP Status Code is " + strconv.Itoa(statusCode) + "."
			}
			siteWasUp = false

		} else {
			siteUp := true
			// if the site settings require check the content.
			if siteUp && s.ContentExpected != "" && !strings.Contains(bodyContent, s.ContentExpected) {
				siteUp = false
				log.Println(s.Name, "Error - required body content missing: ", s.ContentExpected)
				if siteWasUp {
					statusChange = true
					partialSubject = "Site is Down"
					partialDetails = "Site is Down, required body content missing: " + s.ContentExpected + "."
				}
			}
			if siteUp && s.ContentUnexpected != "" && strings.Contains(bodyContent, s.ContentUnexpected) {
				siteUp = false
				log.Println(s.Name, "Error - body content content has excluded content: ", s.ContentUnexpected)
				if siteWasUp {
					statusChange = true
					partialSubject = "Site is Down"
					partialDetails = "Site is Down, body content content has excluded content: " + s.ContentUnexpected + "."
				}
			}
			if siteUp && !siteWasUp {
				statusChange = true
				partialSubject = "Site is Up"
				partialDetails = fmt.Sprintf("Site is now up, response time was %v.", responseTime)
				siteWasUp = true
			}
			siteWasUp = siteUp
		}
		// Save the ping details
		p.Duration = int(responseTime.Nanoseconds() / 1e6)
		p.HTTPStatusCode = statusCode
		p.SiteDown = !siteWasUp
		// Save ping to db.
		err = p.CreatePing(db)
		if err != nil {
			log.Println("Error saving to ping to db:", err)
		}
		// Do the notifications if applicable
		if statusChange {
			// Update the site Status
			err = s.UpdateSiteStatus(db, siteWasUp)
			if err != nil {
				log.Println("Error updating site status:", err)
			}
			// Do the notifications if applicable
			subject := s.Name + ": " + partialSubject
			details := s.Name + " at " + s.URL + ": " + partialDetails
			log.Println("Will notify status change for", s.Name+":", details)
//.........这里部分代码省略.........
开发者ID:turnkey-commerce,项目名称:go-ping-sites,代码行数:101,代码来源:pinger.go


注:本文中的github.com/turnkey-commerce/go-ping-sites/database.Site.UpdateSiteStatus方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。