Skip to content

Commit f39d360

Browse files
committed
Add blank plugins: email, telegram
1 parent 5b9aa41 commit f39d360

File tree

8 files changed

+223
-1
lines changed

8 files changed

+223
-1
lines changed

src/app/app.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package app
2+
3+
import (
4+
log "github.com/Sirupsen/logrus"
5+
"github.com/jmoiron/sqlx"
6+
_ "github.com/lib/pq"
7+
"github.com/postgres-ci/notifier/src/common"
8+
"github.com/postgres-ci/notifier/src/plugins/email"
9+
"github.com/postgres-ci/notifier/src/plugins/telegram"
10+
11+
"os"
12+
"runtime"
13+
"time"
14+
)
15+
16+
const (
17+
MaxOpenConns = 5
18+
MaxIdleConns = 2
19+
ConnMaxLifetime = time.Hour
20+
)
21+
22+
func New(config common.Config) *app {
23+
24+
connect, err := sqlx.Connect("postgres", config.Connect.DSN())
25+
26+
if err != nil {
27+
28+
log.Fatalf("Could not connect to database server: %v", err)
29+
}
30+
31+
log.Debugf("Connect to postgresql server. DSN(%s)", config.Connect.DSN())
32+
33+
connect.SetMaxOpenConns(MaxOpenConns)
34+
connect.SetMaxIdleConns(MaxIdleConns)
35+
connect.SetConnMaxLifetime(ConnMaxLifetime)
36+
37+
app := app{
38+
config: config,
39+
connect: connect,
40+
plugins: []plugin{
41+
email.New(config),
42+
telegram.New(config.Telegram.Token, connect),
43+
},
44+
}
45+
46+
return &app
47+
}
48+
49+
type plugin interface {
50+
Send(common.Build) error
51+
}
52+
53+
type app struct {
54+
config common.Config
55+
connect *sqlx.DB
56+
plugins []plugin
57+
debug bool
58+
}
59+
60+
func (a *app) SetDebugMode() {
61+
62+
a.debug = true
63+
}
64+
65+
func (a *app) Run() {
66+
67+
log.Info("Postgres-CI notifier started")
68+
log.Debugf("Runtime version: %s. Pid: %d", runtime.Version(), os.Getpid())
69+
70+
if a.debug {
71+
72+
go a.debugInfo()
73+
}
74+
75+
t := make(chan struct{})
76+
77+
<-t
78+
}

src/app/app_debug_info.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package app
2+
3+
import (
4+
log "github.com/Sirupsen/logrus"
5+
_ "net/http/pprof"
6+
7+
"net/http"
8+
"runtime"
9+
"time"
10+
)
11+
12+
func (a *app) debugInfo() {
13+
14+
go func() {
15+
16+
log.Info("Worker running in debug mode")
17+
log.Infof("pprof: http://127.0.0.1:8081/debug/pprof")
18+
log.Error(http.ListenAndServe("127.0.0.1:8081", nil))
19+
}()
20+
21+
tick := time.Tick(time.Minute)
22+
23+
for {
24+
25+
var memStats runtime.MemStats
26+
27+
runtime.ReadMemStats(&memStats)
28+
29+
log.Debugf(
30+
"gorutines: %d, num gc: %d, alloc: %d, mallocs: %d, frees: %d, heap alloc: %d, stack inuse: %d",
31+
runtime.NumGoroutine(),
32+
memStats.NumGC,
33+
memStats.Alloc,
34+
memStats.Mallocs,
35+
memStats.Frees,
36+
memStats.HeapAlloc,
37+
memStats.StackInuse,
38+
)
39+
40+
<-tick
41+
}
42+
}
File renamed without changes.

src/common/build.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package common
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"time"
7+
)
8+
9+
type Build struct {
10+
ID int32 `db:"build_id"`
11+
Status string `db:"status"`
12+
Branch string `db:"branch"`
13+
Error string `db:"build_error"`
14+
CreatedAt time.Time `db:"build_created_at"`
15+
StartedAt time.Time `db:"build_started_at"`
16+
FinishedAt time.Time `db:"build_finished_at"`
17+
CommitSHA string `db:"commit_sha"`
18+
CommitMessage string `db:"commit_message"`
19+
CommittedAt time.Time `db:"committed_at"`
20+
CommitterName string `db:"committer_name"`
21+
CommitterEmail string `db:"committer_email"`
22+
CommitAuthorName string `db:"commit_author_name"`
23+
CommitAuthorEmail string `db:"commit_author_email"`
24+
SendTo recipients `db:"send_to"`
25+
}
26+
27+
type recipient struct {
28+
Name string `json:"user_name"`
29+
Method string `json:"notify_method"`
30+
TextID string `json:"notify_text_id"`
31+
IntID int64 `json:"notify_int_id"`
32+
}
33+
34+
type recipients []recipient
35+
36+
func (r *recipients) Scan(src interface{}) error {
37+
38+
var source []byte
39+
40+
switch src.(type) {
41+
case string:
42+
source = []byte(src.(string))
43+
case []byte:
44+
source = src.([]byte)
45+
default:
46+
return fmt.Errorf("Incompatible type for recipients")
47+
}
48+
49+
return json.Unmarshal(source, r)
50+
}

src/plugins/email/email.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package email
2+
3+
import (
4+
"github.com/postgres-ci/notifier/src/common"
5+
)
6+
7+
func New(config common.Config) *email {
8+
return &email{}
9+
}
10+
11+
type email struct {
12+
}
13+
14+
func (e *email) Send(common.Build) error {
15+
return nil
16+
}

src/plugins/telegram/bot.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package telegram
2+
3+
import (
4+
"github.com/jmoiron/sqlx"
5+
"github.com/postgres-ci/notifier/src/common"
6+
)
7+
8+
func New(token string, connect *sqlx.DB) *bot {
9+
return &bot{
10+
token: token,
11+
connect: connect,
12+
}
13+
}
14+
15+
type bot struct {
16+
token string
17+
connect *sqlx.DB
18+
}
19+
20+
func (b *bot) Send(common.Build) error {
21+
return nil
22+
}

vendor/manifest

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
"revision": "ee1442bda7bd1b6a84e913bdb421cb1874ec629d",
2020
"branch": "master"
2121
},
22+
{
23+
"importpath": "github.com/tucnak/telebot",
24+
"repository": "https://github.com/tucnak/telebot",
25+
"revision": "68ad7fd5dcdbfdc4f6513e0195ad6edb2c3e0573",
26+
"branch": "master"
27+
},
2228
{
2329
"importpath": "gopkg.in/airbrake/gobrake.v2",
2430
"repository": "https://gopkg.in/airbrake/gobrake.v2",

worker.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
log "github.com/Sirupsen/logrus"
5+
"github.com/postgres-ci/notifier/src/app"
56
"github.com/postgres-ci/notifier/src/common"
67

78
"flag"
@@ -87,5 +88,12 @@ func main() {
8788
log.SetLevel(config.LogLevel())
8889
}
8990

90-
log.Debug(config)
91+
app := app.New(config)
92+
93+
if debug {
94+
95+
app.SetDebugMode()
96+
}
97+
98+
app.Run()
9199
}

0 commit comments

Comments
 (0)