github.com/erda-project/erda-infra@v1.0.9/providers/cassandra/examples/main.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  	"time"
    21  
    22  	"github.com/erda-project/erda-infra/base/logs"
    23  	"github.com/erda-project/erda-infra/base/servicehub"
    24  	"github.com/erda-project/erda-infra/providers/cassandra"
    25  )
    26  
    27  type config struct {
    28  	Session cassandra.SessionConfig `file:"session"`
    29  }
    30  
    31  type provider struct {
    32  	Client cassandra.Interface
    33  	Logger logs.Logger
    34  	Cfg    *config
    35  
    36  	s *cassandra.Session
    37  }
    38  
    39  func (p *provider) Init(ctx servicehub.Context) error {
    40  	session, err := p.Client.NewSession(&p.Cfg.Session)
    41  	p.s = session
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  func (p *provider) Run(ctx context.Context) error {
    50  	ticker := time.NewTicker(time.Second * 5)
    51  	defer ticker.Stop()
    52  	for {
    53  		select {
    54  		case <-ctx.Done():
    55  			return nil
    56  		case <-ticker.C:
    57  			data, err := p.s.Session().Query("SELECT cql_version FROM system.local").Iter().SliceMap()
    58  			if err != nil {
    59  				p.Logger.Errorf("query error: %s", err)
    60  				continue
    61  			}
    62  			p.Logger.Infof("data: %+v\n", data)
    63  		}
    64  	}
    65  }
    66  
    67  func init() {
    68  	servicehub.Register("example", &servicehub.Spec{
    69  		Services:     []string{"example"},
    70  		Dependencies: []string{"cassandra"},
    71  		Description:  "example",
    72  		ConfigFunc: func() interface{} {
    73  			return &config{}
    74  		},
    75  		Creator: func() servicehub.Provider {
    76  			return &provider{}
    77  		},
    78  	})
    79  }
    80  
    81  func main() {
    82  	hub := servicehub.New()
    83  	hub.Run("examples", "", os.Args...)
    84  }
    85  
    86  // OUTPUT:
    87  // INFO[2021-04-08 17:49:03.504] provider cassandra initialized
    88  // keyspace name: system
    89  // INFO[2021-04-08 17:49:05.031] provider example (depends [cassandra]) initialized
    90  // INFO[2021-04-08 17:49:05.031] signals to quit: [hangup interrupt terminated quit]