コード例 #1
0
ファイル: scan.go プロジェクト: rafaeljusto/shelter
// FinishAndSaveScan was created to alert that the scan being executed finished. This
// function is necessary to garantee concurrency acces for the current scan information.
// Will set all necessary information and save the scan into the database for future
// reports. Only a part of the scan is saved into the database, because some information
// is only useful during the execution
func FinishAndSaveScan(hadErrors bool, f func(*Scan) error) error {
	shelterCurrentScanLock.Lock()
	defer shelterCurrentScanLock.Unlock()

	if hadErrors {
		shelterCurrentScan.Status = ScanStatusExecutedWithErrors
	} else {
		shelterCurrentScan.Status = ScanStatusExecuted
	}

	shelterCurrentScan.FinishedAt = time.Now()

	// Save the scan
	err := f(&shelterCurrentScan.Scan)

	// Change the current scan state to prepare for the next scan
	shelterCurrentScan = CurrentScan{
		Scan: Scan{
			Status:               ScanStatusWaitingExecution,
			NameserverStatistics: make(map[string]uint64),
			DSStatistics:         make(map[string]uint64),
		},
		LastModifiedAt: time.Now(),
	}

	// We only check the err after reseting the shelterCurrentScan variable because we want
	// to change the variable even if we had an error
	if err != nil {
		return err
	}

	// Retrieve the scan next execution. We only do this here because we want to be the last
	// thing from the method, avoiding that an error of this action prevent other actions to
	// run
	nextExecution, err := scheduler.NextExecutionByType(scheduler.JobTypeScan)
	if err != nil {
		// Didn't find a scan job in the scheduler, really strange! Return the error to report
		// the problem (probably by log messages)
		return err
	}
	shelterCurrentScan.ScheduledAt = nextExecution

	return nil
}
コード例 #2
0
ファイル: scan.go プロジェクト: rafaeljusto/shelter
// Function to fill current scan variable for the first time. Should run after the
// scheduler registered the scan job, to determinate the next execution time. Returns an
// error if this function is executed before the scheduler register the scan job
func InitializeCurrentScan() error {
	shelterCurrentScanLock.Lock()
	defer shelterCurrentScanLock.Unlock()

	nextExecution, err := scheduler.NextExecutionByType(scheduler.JobTypeScan)

	shelterCurrentScan = CurrentScan{
		Scan: Scan{
			Status:               ScanStatusWaitingExecution,
			NameserverStatistics: make(map[string]uint64),
			DSStatistics:         make(map[string]uint64),
		},
		ScheduledAt:    nextExecution,
		LastModifiedAt: time.Now(),
	}

	// If err from different from nil we didn't find a scan job in the scheduler! Propably
	// this function was executed before the scheduler registered the scan job
	return err
}