sanbornm / go-selfupdate

Enable your Go applications to self update
MIT License
1.52k stars 173 forks source link

What happens when CheckTime=0 and RandomizeTime=0,I set but no change. #46

Closed im-lauson closed 1 year ago

im-lauson commented 1 year ago

What happens when CheckTime=0 and RandomizeTime=0,I set but no change.Is it possible to customize the update time so far? How did you do it?

im-lauson commented 1 year ago

I want to set it to be updated every three hours, is it possible at present?

im-lauson commented 1 year ago

@sanbornm

im-lauson commented 1 year ago

@sanbornm I read the closed PR and issue, and found that it should be possible to customize the update cycle now, is it true?If it is true, besides setting checkTime and randomizeTime, is there anything else I need to do?

sanbornm commented 1 year ago

What happens when CheckTime=0 and RandomizeTime=0

I believe it will trigger an update time.Now() + random time which hard codes adding one hour: https://github.com/sanbornm/go-selfupdate/blob/master/selfupdate/selfupdate.go#L150

If you find out please create a PR and update the documentation in the README

I want to set it to be updated every three hours, is it possible at present?

I believe you set the check time to 3 hours.

im-lauson commented 1 year ago

@sanbornm Thank you very much for your reply, I also want to double check, if I want to check for updates about every two hours (one hour plus a random time of one hour), I just need to set it like below and it will work right? And I want to know how long is the default automatic check time if CheckTime and RandomizeTime are not set?

var updater = &selfupdate.Updater{
    CurrentVersion: version,
    ApiURL:         "http://updates.yourdomain.com/",
    BinURL:         "http://updates.yourdomain.com/",
    DiffURL:        "http://updates.yourdomain.com/",
    Dir:            "update/",
    CmdName:        "myapp", // app name
        CheckTime:       1,                        // Time in hours before next check
    RandomizeTime:   1,
}

if updater != nil {
    go updater.BackgroundRun()
}
sanbornm commented 1 year ago

The logic is as follows:

// SetUpdateTime writes the next update time to the state file
func (u *Updater) SetUpdateTime() bool {
    path := u.getExecRelativeDir(u.Dir + upcktimePath)
    wait := time.Duration(u.CheckTime) * time.Hour
    // Add 1 to random time since max is not included
    waitrand := time.Duration(rand.Intn(u.RandomizeTime+1)) * time.Hour

    return writeTime(path, time.Now().Add(wait+waitrand))
}

If you want every two hours set CheckTime to 2 and RandomizeTime to 0. This will cause rand.Intn(0+1) to always return 0.

If both are 0 it would basically set the update time to time.Now() (time.Now().Add(0+0)). It generates a cktime file so you can inspect the timestamp for next update check for proof.

im-lauson commented 1 year ago

Thank you for your patient reply, I am a student, and my business ability is a bit poor.^-^