Skip to content

Commit 7b80236

Browse files
committed
Rename SensibleDuration to ReadableDuration
1 parent cbb1b2e commit 7b80236

File tree

6 files changed

+35
-22
lines changed

6 files changed

+35
-22
lines changed

background.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818
"syscall"
1919
)
2020

21-
// AlreadyRunning is the error returned if a job of the given name
22-
// is already running.
21+
// AlreadyRunning is the error returned by RunInBackground if a job with
22+
// the given name is already running.
2323
type AlreadyRunning struct {
2424
Name string
2525
Pid int
@@ -30,7 +30,8 @@ func (a AlreadyRunning) Error() string {
3030
return fmt.Sprintf("Job '%s' already running with PID %d", a.Name, a.Pid)
3131
}
3232

33-
// RunInBackground executes cmd in the background.
33+
// RunInBackground executes cmd in the background. It returns an
34+
// AlreadyRunning error if a job of the same name is already running.
3435
func RunInBackground(jobName string, cmd *exec.Cmd) error {
3536
if IsRunning(jobName) {
3637
pid, _ := getPid(jobName)
@@ -82,6 +83,7 @@ func IsRunning(jobName string) bool {
8283
return true
8384
}
8485

86+
// Save PID a job-specific file.
8587
func savePid(jobName string, pid int) error {
8688
p := pidFile(jobName)
8789
if err := ioutil.WriteFile(p, []byte(fmt.Sprintf("%d", pid)), 0600); err != nil {
@@ -90,6 +92,7 @@ func savePid(jobName string, pid int) error {
9092
return nil
9193
}
9294

95+
// Return PID for job.
9396
func getPid(jobName string) (int, error) {
9497
p := pidFile(jobName)
9598
data, err := ioutil.ReadFile(p)
@@ -103,7 +106,7 @@ func getPid(jobName string) (int, error) {
103106
return pid, nil
104107
}
105108

106-
// Path to PID file for jobName
109+
// Path to PID file for job.
107110
func pidFile(jobName string) string {
108111
dir := EnsureExists(filepath.Join(awCacheDir(), "jobs"))
109112
return filepath.Join(dir, jobName+".pid")

doc.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ Upcoming features
3636
3737
These features are planned:
3838
39-
TODO: Add support for Alfred v3.2 feedback-level variables
40-
TODO: Add support for Alfred v3.2 re-run feature
41-
TODO: Starting and managing background processes
4239
TODO: Caching and storing data
4340
TODO: Alfred/AppleScript helpers?
4441
TODO: Implement standard-compliant pre-release comparison in SemVer?
@@ -161,6 +158,19 @@ You can use your own backend by implementing the Releaser interface.
161158
The only hard requirement is support for (mostly) semantic version numbers.
162159
See http://semver.org for details.
163160
161+
See examples/update for one possible way to use this API.
162+
163+
164+
Background jobs
165+
166+
AwGo provides a simple API to start/stop background processes via the
167+
RunInBackground(), IsRunning() and Kill() functions. This is useful
168+
for running checks for updates and other jobs that hit the network or
169+
take a significant amount of time to complete, allowing you to keep
170+
your Script Filters extremely responsive.
171+
172+
See examples/update for one possible way to use this API.
173+
164174
165175
Performance
166176

update.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func (u *Updater) CheckDue() bool {
150150
return true
151151
}
152152
elapsed := time.Now().Sub(u.LastCheck)
153-
log.Printf("%s since last check for update.", SensibleDuration(elapsed))
153+
log.Printf("%s since last check for update", ReadableDuration(elapsed))
154154
return elapsed.Nanoseconds() > u.UpdateInterval.Nanoseconds()
155155
}
156156

util.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ func Pad(str, pad string, n int) string {
126126
}
127127
}
128128

129-
// SensibleDuration returns a sensibly-formatted string for
129+
// ReadableDuration returns a sensibly-formatted string for
130130
// non-benchmarking purposes.
131-
func SensibleDuration(d time.Duration) string {
131+
func ReadableDuration(d time.Duration) string {
132132
if d.Hours() >= 72 { // 3 days
133133
return fmt.Sprintf("%dd", int(d.Hours()/24))
134134
}

util_test.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,17 @@ func ExamplePad() {
9595
// Output: ---wow----
9696
}
9797

98-
func ExampleSensibleDuration() {
99-
fmt.Println(SensibleDuration(time.Hour * 96))
100-
fmt.Println(SensibleDuration(time.Hour * 48))
101-
fmt.Println(SensibleDuration(time.Hour * 12))
102-
fmt.Println(SensibleDuration(time.Minute * 130))
103-
fmt.Println(SensibleDuration(time.Minute * 90))
104-
fmt.Println(SensibleDuration(time.Second * 315))
105-
fmt.Println(SensibleDuration(time.Second * 70))
106-
fmt.Println(SensibleDuration(time.Second * 5))
107-
fmt.Println(SensibleDuration(time.Millisecond * 320))
108-
fmt.Println(SensibleDuration(time.Millisecond * 50))
98+
func ExampleReadableDuration() {
99+
fmt.Println(ReadableDuration(time.Hour * 96))
100+
fmt.Println(ReadableDuration(time.Hour * 48))
101+
fmt.Println(ReadableDuration(time.Hour * 12))
102+
fmt.Println(ReadableDuration(time.Minute * 130))
103+
fmt.Println(ReadableDuration(time.Minute * 90))
104+
fmt.Println(ReadableDuration(time.Second * 315))
105+
fmt.Println(ReadableDuration(time.Second * 70))
106+
fmt.Println(ReadableDuration(time.Second * 5))
107+
fmt.Println(ReadableDuration(time.Millisecond * 320))
108+
fmt.Println(ReadableDuration(time.Millisecond * 50))
109109
// Output: 4d
110110
// 48h
111111
// 12h0m

workflow.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ func (wf *Workflow) awCacheDir() string {
903903
// finishLog outputs the workflow duration
904904
func finishLog(fatal bool) {
905905
elapsed := time.Now().Sub(startTime)
906-
s := Pad(fmt.Sprintf(" %s ", SensibleDuration(elapsed)), "-", 50)
906+
s := Pad(fmt.Sprintf(" %s ", ReadableDuration(elapsed)), "-", 50)
907907
if fatal {
908908
log.Fatalln(s)
909909
} else {

0 commit comments

Comments
 (0)