Skip to content

Commit

Permalink
service stop should not trigger daily checks and tasks. Closes #745
Browse files Browse the repository at this point in the history
  • Loading branch information
binaek authored and kaidaguerre committed Aug 5, 2021
1 parent e69ea40 commit 2005165
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
31 changes: 19 additions & 12 deletions task/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,17 @@ func (r *Runner) Run() {
waitGroup := sync.WaitGroup{}

// check whether an updated version is available
waitGroup.Add(1)
go r.runAsyncJob(func() {
runJobAsync(func() {
versionNotificationLines = checkSteampipeVersion(r.currentState.InstallationID)
}, &waitGroup)

// check whether an updated version is available
waitGroup.Add(1)
go r.runAsyncJob(func() {
runJobAsync(func() {
pluginNotificationLines = checkPluginVersions(r.currentState.InstallationID)
}, &waitGroup)

// remove log files older than 7 days
waitGroup.Add(1)
go r.runAsyncJob(func() { db.TrimLogs() }, &waitGroup)
runJobAsync(func() { db.TrimLogs() }, &waitGroup)

// wait for all jobs to complete
waitGroup.Wait()
Expand All @@ -73,9 +70,12 @@ func (r *Runner) Run() {
}
}

func (r *Runner) runAsyncJob(job func(), wg *sync.WaitGroup) {
job()
wg.Done()
func runJobAsync(job func(), wg *sync.WaitGroup) {
wg.Add(1)
go func() {
job()
wg.Done()
}()
}

// determines whether the task runner should run at all
Expand All @@ -87,9 +87,8 @@ func (r *Runner) shouldRun() bool {

cmd := viper.Get(constants.ConfigKeyActiveCommand).(*cobra.Command)
cmdArgs := viper.GetStringSlice(constants.ConfigKeyActiveCommandArgs)
if cmd.Name() == "query" && len(cmdArgs) > 0 {
// this is query batch mode
// we will not run scheduled tasks in this mode
if isServiceStopCmd(cmd) || isBatchQueryCmd(cmd, cmdArgs) {
// no scheduled tasks for `service stop` and `query <sql>`
return false
}

Expand All @@ -105,3 +104,11 @@ func (r *Runner) shouldRun() bool {

return minutesElapsed > minimumMinutesBetweenChecks
}

func isServiceStopCmd(cmd *cobra.Command) bool {
return cmd.Parent() != nil && cmd.Parent().Name() == "service" && cmd.Name() == "stop"
}

func isBatchQueryCmd(cmd *cobra.Command, cmdArgs []string) bool {
return cmd.Name() == "query" && len(cmdArgs) > 0
}
19 changes: 18 additions & 1 deletion tests/acceptance/test_files/002.service.bats
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,21 @@ load "$LIB_BATS_SUPPORT/load.bash"
assert_equal $(netstat -an tcp | grep LISTEN | grep tcp | grep 127.0.0.1 | grep 8765 | wc -l) 1
assert_equal $(netstat -an tcp | grep LISTEN | grep tcp | grep ::1 | grep 8765 | wc -l) 1
steampipe service stop --force
}
}

@test "steampipe service stop should not trigger daily checks and tasks" {
run steampipe service start

# set the `lastChecked` date in the update-check.json file to a past date
echo $(cat $STEAMPIPE_INSTALL_DIR/internal/update-check.json | jq '.lastChecked="2021-04-10T17:53:40+05:30"') > $STEAMPIPE_INSTALL_DIR/internal/update-check.json

# get the content of the current update-check.json file
checkFileContent=$(cat $STEAMPIPE_INSTALL_DIR/internal/update-check.json)

run steampipe service stop

# get the content of the new update-check.json file
newCheckFileContent=$(cat $STEAMPIPE_INSTALL_DIR/internal/update-check.json)

assert_equal "$(echo $newCheckFileContent | jq '.lastChecked')" '"2021-04-10T17:53:40+05:30"'
}

0 comments on commit 2005165

Please sign in to comment.