2021-05-26 19:22:11 +08:00
/*
** description("").
** copyright('open-im,www.open-im.io').
** author("fg,Gordon@open-im.io").
** time(2021/3/22 15:33).
*/
2023-02-20 10:13:29 +08:00
package push
2021-05-26 19:22:11 +08:00
import (
2023-02-23 19:15:30 +08:00
fcm "OpenIM/internal/push/fcm"
"OpenIM/internal/push/getui"
jpush "OpenIM/internal/push/jpush"
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/db/cache"
"OpenIM/pkg/common/prome"
"OpenIM/pkg/statistics"
2022-02-25 19:49:38 +08:00
"fmt"
2021-05-26 19:22:11 +08:00
)
2023-02-22 19:51:14 +08:00
type Push struct {
2022-08-30 22:04:13 +08:00
rpcServer RPCServer
2023-02-20 10:13:29 +08:00
pushCh ConsumerHandler
offlinePusher OfflinePusher
2022-08-30 22:04:13 +08:00
successCount uint64
2023-02-22 19:51:14 +08:00
}
2021-05-26 19:22:11 +08:00
2023-02-23 17:28:57 +08:00
func ( p * Push ) Init ( rpcPort int ) error {
redisClient , err := cache . NewRedis ( )
if err != nil {
return err
}
var cacheInterface cache . Cache = redisClient
2023-02-22 19:51:14 +08:00
p . rpcServer . Init ( rpcPort , cacheInterface )
p . pushCh . Init ( )
statistics . NewStatistics ( & p . successCount , config . Config . ModuleName . PushName , fmt . Sprintf ( "%d second push to msg_gateway count" , constant . StatisticsTimeInterval ) , constant . StatisticsTimeInterval )
2022-11-10 14:51:35 +08:00
if * config . Config . Push . Getui . Enable {
2023-02-22 19:51:14 +08:00
p . offlinePusher = getui . NewClient ( cacheInterface )
2022-05-25 15:48:48 +08:00
}
if config . Config . Push . Jpns . Enable {
2023-02-22 19:51:14 +08:00
p . offlinePusher = jpush . NewClient ( )
2022-05-25 15:48:48 +08:00
}
2022-07-25 17:07:47 +08:00
if config . Config . Push . Fcm . Enable {
2023-02-22 19:51:14 +08:00
p . offlinePusher = fcm . NewClient ( cacheInterface )
2022-07-25 17:07:47 +08:00
}
2023-02-23 17:28:57 +08:00
return nil
2021-05-26 19:22:11 +08:00
}
2023-02-22 19:51:14 +08:00
func ( p * Push ) initPrometheus ( ) {
2023-02-15 15:52:32 +08:00
prome . NewMsgOfflinePushSuccessCounter ( )
prome . NewMsgOfflinePushFailedCounter ( )
2022-09-15 12:07:28 +08:00
}
2023-02-22 19:51:14 +08:00
func ( p * Push ) Run ( prometheusPort int ) {
go p . rpcServer . run ( )
go p . pushCh . pushConsumerGroup . RegisterHandleAndConsumer ( & p . pushCh )
2022-09-12 19:32:24 +08:00
go func ( ) {
2023-02-22 19:51:14 +08:00
err := prome . StartPromeSrv ( prometheusPort )
2022-09-12 19:32:24 +08:00
if err != nil {
panic ( err )
}
} ( )
2021-05-26 19:22:11 +08:00
}