diff --git a/server/server.go b/server/server.go index cbd70a99f..b68829c4b 100644 --- a/server/server.go +++ b/server/server.go @@ -285,7 +285,7 @@ func (m *Command) SetupServer() error { m.logger.Printf("%s", pilosa.VersionInfo()) - m.trialVersion() + trialVersion(m.logger) // If the pilosa command line uses -tx to override the // PILOSA_TXSRC env variable, then we must also correct diff --git a/server/trial.go b/server/trial.go index 8feb6bfa4..868a177c5 100644 --- a/server/trial.go +++ b/server/trial.go @@ -1,4 +1,4 @@ -// Copyright 2017 Pilosa Corp. +// Copyright 2021 Pilosa Corp. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -27,54 +27,56 @@ import ( "github.com/pilosa/pilosa/v2" ) -func (m *Command) trialVersion() { +func trialVersion(logger loggerLogger) { if pilosa.TrialDeadline != "" { - endTime, err := time.Parse("2006-01-02", pilosa.TrialDeadline) + startTime, err := ntpServerTime(4, logger) if err != nil { - m.logger.Printf("parsing trial deadline: %v", err) + logger.Printf("reading ntp server time %v", err) os.Exit(1) } - go m.dailyCheck(endTime) + endTime, err := time.Parse("2006-01-02", pilosa.TrialDeadline) + if err != nil { + logger.Printf("parsing trial deadline: %v", err) + os.Exit(1) + } + maxDuration := endTime.Sub(startTime) + go expireAfter(maxDuration, logger) + go dailyCheck(maxDuration, logger) } } const trialCheckInterval = 24 * time.Hour // dailyCheck runs in the background while a trial version of Molecula is being run, displaying daily reminders of the remaining days -func (m *Command) dailyCheck(endTime time.Time) { +func dailyCheck(maxDuration time.Duration, logger loggerLogger) { + startTime := time.Now() // we get a new start time here to ensure that it has a monotonic clock + remaining := maxDuration - time.Since(startTime) + logger.Printf("Current time remaining in trial: %d days %v", remaining/(time.Hour*24), remaining%(time.Hour*24)) ticker := time.NewTicker(trialCheckInterval) - startTime, err := m.ntpServerTime(4) - if err != nil { - m.logger.Printf("reading ntp server time %v", err) - os.Exit(1) - } - runDuration := endTime.Sub(startTime) - if runDuration <= 0 { - m.logger.Printf("Trial edition of Molecula has expired, exiting now!") - os.Exit(1) - } for range ticker.C { - runningDuration := time.Since(startTime) - m.logger.Printf("Current time remaining in trial: %v", runDuration-runningDuration) - if runningDuration >= runDuration { - m.logger.Printf("Trial edition of Molecula has expired, exiting now!") - os.Exit(1) - } + remaining := maxDuration - time.Since(startTime) + logger.Printf("Current time remaining in trial: %d days %v", remaining/(time.Hour*24), remaining%(time.Hour*24)) } } -const ntpurl = "0.beevik-ntp.pool.ntp.org" +const ntpURL = "0.beevik-ntp.pool.ntp.org" const ntpRetryDelay = 100 * time.Millisecond // ntpServerTime attempts to reach ntp servers with delays between each attempt, returning the time value of the first connected server -func (m *Command) ntpServerTime(retries int) (time.Time, error) { - t, err := ntp.Time(ntpurl) +func ntpServerTime(retries int, logger loggerLogger) (time.Time, error) { + t, err := ntp.Time(ntpURL) if err != nil && retries <= 0 { return t, err } if err != nil { time.Sleep(ntpRetryDelay) - return m.ntpServerTime(retries - 1) + return ntpServerTime(retries-1, logger) } return t, nil } + +func expireAfter(maxDuration time.Duration, logger loggerLogger) { + time.Sleep(maxDuration) + logger.Printf("Trial edition of Molecula has expired, exiting now!") + os.Exit(1) +} diff --git a/version.go b/version.go index 7c3f2361b..ec27e5c6f 100644 --- a/version.go +++ b/version.go @@ -53,8 +53,10 @@ func VersionInfo() string { case buildTime != "": suffix += " (" + buildTime + ")" } - suffix += " " + GoVersion + " limited time trial ends at: " + TrialDeadline - + suffix += " " + GoVersion + if TrialDeadline != "" { + suffix += " limited time trial ends at: " + TrialDeadline + } return prefix + "Pilosa" + suffix }