2019-05-15 08:17:20 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package notification
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/common"
|
2022-02-22 14:01:29 +00:00
|
|
|
"agola.io/agola/internal/errors"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/etcd"
|
|
|
|
"agola.io/agola/internal/services/config"
|
2019-07-31 13:39:07 +00:00
|
|
|
csclient "agola.io/agola/services/configstore/client"
|
|
|
|
rsclient "agola.io/agola/services/runservice/client"
|
2019-05-15 08:17:20 +00:00
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
"github.com/rs/zerolog"
|
2019-05-15 08:17:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type NotificationService struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
|
|
|
gc *config.Config
|
|
|
|
c *config.Notification
|
2019-05-15 08:17:20 +00:00
|
|
|
|
|
|
|
e *etcd.Store
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
runserviceClient *rsclient.Client
|
|
|
|
configstoreClient *csclient.Client
|
2019-05-15 08:17:20 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewNotificationService(ctx context.Context, log zerolog.Logger, gc *config.Config) (*NotificationService, error) {
|
2019-05-15 08:17:20 +00:00
|
|
|
c := &gc.Notification
|
2020-01-15 11:01:46 +00:00
|
|
|
|
2019-05-15 08:17:20 +00:00
|
|
|
if c.Debug {
|
2022-02-21 17:07:58 +00:00
|
|
|
log = log.Level(zerolog.DebugLevel)
|
2019-05-15 08:17:20 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
e, err := common.NewEtcd(&c.Etcd, log, "notification")
|
2019-05-15 08:17:20 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-05-15 08:17:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
configstoreClient := csclient.NewClient(c.ConfigstoreURL)
|
|
|
|
runserviceClient := rsclient.NewClient(c.RunserviceURL)
|
2019-05-15 08:17:20 +00:00
|
|
|
|
|
|
|
return &NotificationService{
|
2022-02-21 17:07:58 +00:00
|
|
|
log: log,
|
2019-05-15 08:17:20 +00:00
|
|
|
gc: gc,
|
|
|
|
c: c,
|
|
|
|
e: e,
|
|
|
|
runserviceClient: runserviceClient,
|
|
|
|
configstoreClient: configstoreClient,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NotificationService) Run(ctx context.Context) error {
|
|
|
|
go n.runEventsHandlerLoop(ctx)
|
|
|
|
|
2019-07-02 13:54:47 +00:00
|
|
|
<-ctx.Done()
|
2022-02-21 17:07:58 +00:00
|
|
|
n.log.Info().Msgf("notification service exiting")
|
2019-07-02 13:54:47 +00:00
|
|
|
|
|
|
|
return nil
|
2019-05-15 08:17:20 +00:00
|
|
|
}
|