-
Notifications
You must be signed in to change notification settings - Fork 5
/
SlackFileDeleter.go
50 lines (44 loc) · 925 Bytes
/
SlackFileDeleter.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
package main
import (
"github.com/nlopes/slack"
"sync"
"time"
"log"
)
type SlackFileDeleter struct {
DeleteQue chan string
StopChan chan struct{}
deleteWaitGroup sync.WaitGroup
}
func newInstance() SlackFileDeleter {
return SlackFileDeleter{
DeleteQue: make(chan string, 1000),
StopChan: make(chan struct{}),
deleteWaitGroup: sync.WaitGroup{},
}
}
func (s *SlackFileDeleter) delete(fileId string) {
s.deleteWaitGroup.Add(1)
go func() {
s.DeleteQue <- fileId
}()
}
func (s *SlackFileDeleter) run(api *slack.Client) {
for {
select {
case fileId := <-s.DeleteQue:
if err := api.DeleteFile(fileId); err != nil {
log.Println(err)
}
log.Printf("Delete %s from Slack.\n", fileId)
s.deleteWaitGroup.Done()
time.Sleep(1 * time.Second)
case <-s.StopChan:
return
}
}
}
func (s *SlackFileDeleter) stop() {
s.deleteWaitGroup.Wait()
close(s.StopChan)
}