You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.0 KiB

  1. /*
  2. * Minio Cloud Storage, (C) 2016 Minio, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package cmd
  17. import (
  18. "fmt"
  19. "io/ioutil"
  20. "net/rpc"
  21. "github.com/Sirupsen/logrus"
  22. )
  23. type listenerConn struct {
  24. TargetAddr string
  25. ListenerARN string
  26. BMSClient BucketMetaState
  27. }
  28. type listenerLogger struct {
  29. log *logrus.Logger
  30. lconn listenerConn
  31. }
  32. func newListenerLogger(listenerArn, targetAddr string) (*listenerLogger, error) {
  33. bmsClient := globalS3Peers.GetPeerClient(targetAddr)
  34. if bmsClient == nil {
  35. return nil, fmt.Errorf(
  36. "Peer %s was not initialized - bug!",
  37. targetAddr,
  38. )
  39. }
  40. lc := listenerConn{
  41. TargetAddr: targetAddr,
  42. ListenerARN: listenerArn,
  43. BMSClient: bmsClient,
  44. }
  45. lcLog := logrus.New()
  46. lcLog.Out = ioutil.Discard
  47. lcLog.Formatter = new(logrus.JSONFormatter)
  48. lcLog.Hooks.Add(lc)
  49. return &listenerLogger{lcLog, lc}, nil
  50. }
  51. // send event to target server via rpc client calls.
  52. func (lc listenerConn) Fire(entry *logrus.Entry) error {
  53. notificationEvent, ok := entry.Data["Records"].([]NotificationEvent)
  54. if !ok {
  55. // If the record is not of the expected type, silently
  56. // discard.
  57. return nil
  58. }
  59. // Send Event RPC call and return error
  60. arg := EventArgs{Event: notificationEvent, Arn: lc.ListenerARN}
  61. err := lc.BMSClient.SendEvent(&arg)
  62. // In case connection is shutdown, retry once.
  63. if err != nil {
  64. if err.Error() == rpc.ErrShutdown.Error() {
  65. err = lc.BMSClient.SendEvent(&arg)
  66. }
  67. }
  68. return err
  69. }
  70. func (lc listenerConn) Levels() []logrus.Level {
  71. return []logrus.Level{
  72. logrus.InfoLevel,
  73. }
  74. }