github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/monitors/scraper.go (about) 1 package monitorsPkg 2 3 // Copyright 2021 The TrueBlocks Authors. All rights reserved. 4 // Use of this source code is governed by a license that can 5 // be found in the LICENSE file. 6 7 import ( 8 "fmt" 9 "os" 10 "path/filepath" 11 "time" 12 13 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger" 14 ) 15 16 type Scraper struct { 17 Running bool `json:"Running"` 18 SleepSecs float64 `json:"SleepSecs"` 19 Name string `json:"Name"` 20 } 21 22 func NewScraper(color, name string, secs float64, logLev uint64) Scraper { 23 scraper := new(Scraper) 24 scraper.Name = name 25 scraper.SleepSecs = secs 26 scraper.Running = false 27 return *scraper 28 } 29 30 func (scraper *Scraper) ChangeState(onOff bool, tmpPath string) bool { 31 prev := scraper.Running 32 scraper.Running = onOff 33 str := "false" 34 fileName := filepath.Join(tmpPath, scraper.Name+".txt") 35 err := os.WriteFile(fileName, []byte(str), 0644) // Uses os.O_WRONLY|os.O_CREATE|os.O_TRUNC 36 if err != nil { 37 logger.Fatal(err) 38 } 39 return prev 40 } 41 42 func (scraper *Scraper) Pause() { 43 halfSecs := scraper.SleepSecs * 2 44 state := scraper.Running 45 for i := 0; i < int(halfSecs); i++ { 46 if state != scraper.Running { 47 break 48 } 49 50 sleep := .5 51 if sleep > 0 { 52 ms := time.Duration(sleep*1000) * time.Millisecond 53 if false { // opts.Globals.TestMode { 54 logger.Info(fmt.Sprintf("Sleeping for %g seconds", sleep)) 55 } 56 time.Sleep(ms) 57 } 58 } 59 }