-
Notifications
You must be signed in to change notification settings - Fork 0
/
running.go
140 lines (119 loc) · 3.51 KB
/
running.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
// This file is part of goimapnotify
// Copyright (C) 2017-2023 Jorge Javier Araya Navarro
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import (
"fmt"
"github.com/sirupsen/logrus"
"sync"
"time"
)
type RunningBox struct {
debug bool
wait int
/*
* Use map to create a different timer for each
* username-mailbox combination
*/
timer map[string]*time.Timer
mutex map[string]*sync.RWMutex
}
func NewRunningBox(debug bool, wait int) *RunningBox {
return &RunningBox{
debug: debug,
wait: wait,
timer: make(map[string]*time.Timer),
mutex: make(map[string]*sync.RWMutex),
}
}
func (r *RunningBox) schedule(rsp IDLEEvent, done <-chan struct{}) {
if shouldSkip(rsp) {
logrus.Warnf("[%s:%s] No command for %s, skipping scheduling...", rsp.Alias, rsp.Mailbox, rsp.Reason)
return
}
key := rsp.Alias + rsp.Mailbox
wait := time.Duration(r.wait) * time.Second
when := time.Now().Add(wait).Format(time.RFC850)
format := fmt.Sprintf("[%s:%s] %%s syncing '%s' for %s (%s in the future)", rsp.Alias, rsp.Mailbox, rsp.Reason, when, wait)
r.mutex[key].Lock()
_, exists := r.timer[key]
main := true // main is true for the goroutine that will run sync
if exists {
// Stop should be called before Reset according to go docs
if r.timer[key].Stop() {
main = false // stopped running timer -> main is another goroutine
}
r.timer[key].Reset(wait)
} else {
r.timer[key] = time.NewTimer(wait)
}
r.mutex[key].Unlock()
if main {
logrus.Infof(format, "Scheduled")
select {
case <-r.timer[key].C:
r.run(rsp)
case <-done:
//just get out
}
} else {
logrus.Infof(format, "Rescheduled")
}
}
func (r *RunningBox) run(rsp IDLEEvent) {
if r.debug {
logrus.Infof("[%s:%s] Running synchronization...",
rsp.Alias,
rsp.Mailbox)
}
var err error
if rsp.Reason == NEWMAIL {
err = prepareAndRun(rsp.OnNewMail, rsp.OnNewMailPost, rsp.Reason, rsp, r.debug)
} else if rsp.Reason == DELETEDMAIL {
err = prepareAndRun(rsp.OnDeletedMail, rsp.OnDeletedMailPost, rsp.Reason, rsp, r.debug)
}
if err != nil {
logrus.Error(err)
}
}
func prepareAndRun(on, onpost string, kind EventType, event IDLEEvent, debug bool) error {
callKind := "New"
if kind == DELETEDMAIL {
callKind = "Deleted"
}
if on == "SKIP" || on == "" {
return nil
}
call := PrepareCommand(on, event, debug)
err := call.Run()
if err != nil {
return fmt.Errorf("[%s:%s] On%sMail command failed: %v", event.Alias, event.Mailbox, callKind, err)
}
if onpost == "SKIP" || onpost == "" {
return nil
}
call = PrepareCommand(onpost, event, debug)
err = call.Run()
if err != nil {
return fmt.Errorf("[%s:%s] On%sMailPost command failed: %v", event.Alias, event.Mailbox, callKind, err)
}
return nil
}
func shouldSkip(event IDLEEvent) bool {
switch event.Reason {
case NEWMAIL:
return event.OnNewMail == "" || event.OnNewMail == "SKIP"
case DELETEDMAIL:
return event.OnDeletedMail == "" || event.OnDeletedMail == "SKIP"
}
return false
}