Mercurial > gemma
annotate pkg/misc/scheduler.go @ 1528:5874cedd7f91
Sounding result import: Accept *.txt files for XYZ data, too.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 07 Dec 2018 12:21:55 +0100 |
parents | 08e1b38a4a8b |
children |
rev | line source |
---|---|
1515
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 // |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package misc |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 "container/heap" |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 "sync" |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 "time" |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 ) |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 type schedulerFn struct { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 kind interface{} |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 d time.Duration |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 fn func(*Scheduler) |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 type entry struct { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 kind interface{} |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 due time.Time |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 fn func(*Scheduler) |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 type entries []*entry |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 // Scheduler manages function to be called later. |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 // They are keyed with a kind to it is possible |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 // To cancel them before they are executed. |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 type Scheduler struct { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 notify chan struct{} |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 cancel chan interface{} |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 add chan schedulerFn |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 queue entries |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 var ( |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 scheduler *Scheduler |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 schedulerOnce sync.Once |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 ) |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 func startScheduler() { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 scheduler = &Scheduler{ |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 notify: make(chan struct{}), |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 cancel: make(chan interface{}), |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 add: make(chan schedulerFn), |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 go scheduler.run() |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 // ScheduleFunc adds a keyed function to the global scheduler |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 // to be executed after a specified duration. |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 func ScheduleFunc(key interface{}, d time.Duration, fn func(*Scheduler)) { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 schedulerOnce.Do(startScheduler) |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 scheduler.Schedule(key, d, fn) |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 // Cancel cancels all functions in the global schedule waiting for |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 // execution with a given key. |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 func ScheduleCancel(key interface{}) { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 schedulerOnce.Do(startScheduler) |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 scheduler.Cancel(key) |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
72 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 // Schedule schedules a keyed function to be executed after a |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 // specified duration. |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 func (s *Scheduler) Schedule(kind interface{}, d time.Duration, fn func(*Scheduler)) { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 s.add <- schedulerFn{kind: kind, d: d, fn: fn} |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 // Cancel cancels all function waiting for execution with a given key. |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 func (s *Scheduler) Cancel(kind interface{}) { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 s.cancel <- kind |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 func (es *entries) Len() int { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 return len(*es) |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 func (es *entries) Swap(i, j int) { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
90 (*es)[i], (*es)[j] = (*es)[j], (*es)[i] |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
92 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
93 func (es *entries) Less(i, j int) bool { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 return (*es)[i].due.Before((*es)[j].due) |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
95 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
96 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
97 func (es *entries) Push(x interface{}) { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
98 *es = append(*es, x.(*entry)) |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
99 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
100 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
101 func (es *entries) Pop() interface{} { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
102 n := len(*es) - 1 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
103 x := (*es)[n] |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
104 (*es)[n] = nil |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
105 *es = (*es)[:n] |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
106 return x |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
107 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
108 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
109 func (s *Scheduler) run() { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
110 for { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
111 select { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
112 case sfn := <-s.add: |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
113 e := &entry{ |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
114 kind: sfn.kind, |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
115 due: time.Now().Add(sfn.d), |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
116 fn: sfn.fn, |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
117 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
118 heap.Push(&s.queue, e) |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
119 go func() { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
120 time.Sleep(sfn.d) |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
121 s.notify <- struct{}{} |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
122 }() |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
123 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
124 case <-s.notify: |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
125 for len(s.queue) > 0 && !s.queue[0].due.After(time.Now()) { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
126 go func(fn func(*Scheduler)) { fn(s) }(s.queue[0].fn) |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
127 heap.Pop(&s.queue) |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
128 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
129 |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
130 case k := <-s.cancel: |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
131 for i := len(s.queue) - 1; i >= 0; { |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
132 if s.queue[i].kind == k { |
1519
08e1b38a4a8b
Be aware of canceling the last scheduled function.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1515
diff
changeset
|
133 n := len(s.queue) - 1 |
08e1b38a4a8b
Be aware of canceling the last scheduled function.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1515
diff
changeset
|
134 if i < n { |
08e1b38a4a8b
Be aware of canceling the last scheduled function.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1515
diff
changeset
|
135 copy(s.queue[i:], s.queue[i+1:]) |
08e1b38a4a8b
Be aware of canceling the last scheduled function.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1515
diff
changeset
|
136 } |
08e1b38a4a8b
Be aware of canceling the last scheduled function.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1515
diff
changeset
|
137 s.queue[n] = nil |
08e1b38a4a8b
Be aware of canceling the last scheduled function.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1515
diff
changeset
|
138 s.queue = s.queue[:n] |
1515
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
139 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
140 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
141 heap.Init(&s.queue) |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
142 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
143 } |
43a32358a016
Backend: Added a scheduler which allows to schedule the execution of functions
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
144 } |