github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/sub/scanner/scand.go (about)

     1  package scanner
     2  
     3  import (
     4  	"runtime"
     5  	"syscall"
     6  	"time"
     7  
     8  	"github.com/Cloud-Foundations/Dominator/lib/log"
     9  )
    10  
    11  var disableScanRequest chan bool
    12  var disableScanAcknowledge chan bool
    13  
    14  func startScannerDaemon(rootDirectoryName string, cacheDirectoryName string,
    15  	configuration *Configuration, logger log.Logger) (
    16  	<-chan *FileSystem, func(disableScanner bool)) {
    17  	fsChannel := make(chan *FileSystem)
    18  	disableScanRequest = make(chan bool, 1)
    19  	disableScanAcknowledge = make(chan bool)
    20  	go scannerDaemon(rootDirectoryName, cacheDirectoryName, configuration,
    21  		fsChannel, logger)
    22  	return fsChannel, doDisableScanner
    23  }
    24  
    25  func startScanning(rootDirectoryName string, cacheDirectoryName string,
    26  	configuration *Configuration, logger log.Logger,
    27  	mainFunc func(<-chan *FileSystem, func(disableScanner bool))) {
    28  	fsChannel := make(chan *FileSystem)
    29  	disableScanRequest = make(chan bool, 1)
    30  	disableScanAcknowledge = make(chan bool)
    31  	go mainFunc(fsChannel, doDisableScanner)
    32  	scannerDaemon(rootDirectoryName, cacheDirectoryName, configuration,
    33  		fsChannel, logger)
    34  }
    35  
    36  func scannerDaemon(rootDirectoryName string, cacheDirectoryName string,
    37  	configuration *Configuration, fsChannel chan<- *FileSystem,
    38  	logger log.Logger) {
    39  	runtime.LockOSThread()
    40  	loweredPriority := false
    41  	var oldFS FileSystem
    42  	var sleepUntil time.Time
    43  	for ; ; time.Sleep(time.Until(sleepUntil)) {
    44  		sleepUntil = time.Now().Add(time.Second)
    45  		fs, err := scanFileSystem(rootDirectoryName, cacheDirectoryName,
    46  			configuration, &oldFS)
    47  		if err != nil {
    48  			if err.Error() == "DisableScan" {
    49  				disableScanAcknowledge <- true
    50  				<-disableScanAcknowledge
    51  				continue
    52  			}
    53  			logger.Printf("Error scanning: %s\n", err)
    54  		} else {
    55  			oldFS.InodeTable = fs.InodeTable
    56  			oldFS.DirectoryInode = fs.DirectoryInode
    57  			fsChannel <- fs
    58  			runtime.GC()
    59  			if !loweredPriority {
    60  				syscall.Setpriority(syscall.PRIO_PROCESS, 0, 15)
    61  				loweredPriority = true
    62  			}
    63  			configuration.RestoreCpuLimit(logger) // Reset after scan.
    64  		}
    65  	}
    66  }
    67  
    68  func doDisableScanner(disableScanner bool) {
    69  	if disableScanner {
    70  		disableScanRequest <- true
    71  		<-disableScanAcknowledge
    72  	} else {
    73  		disableScanAcknowledge <- true
    74  	}
    75  }
    76  
    77  func checkScanDisableRequest() bool {
    78  	if len(disableScanRequest) > 0 {
    79  		<-disableScanRequest
    80  		return true
    81  	}
    82  	return false
    83  }