diff options
author | bodqhrohro <bodqhrohro@gmail.com> | 2019-11-19 23:25:14 +0300 |
---|---|---|
committer | bodqhrohro <bodqhrohro@gmail.com> | 2019-11-19 23:25:14 +0300 |
commit | f0c0d0ba94c923ca06b49211c70db47616e5a8e1 (patch) | |
tree | 23c097b6cb8249d4f978f7b22bd1b9877b0a5bba /xmpp/component.go | |
parent | a09817976ee5cdb7d1bca25c80cdef3d1b7867f7 (diff) |
Save sessions on exit
Diffstat (limited to 'xmpp/component.go')
-rw-r--r-- | xmpp/component.go | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/xmpp/component.go b/xmpp/component.go index d82f4bb..fa1a650 100644 --- a/xmpp/component.go +++ b/xmpp/component.go @@ -25,19 +25,19 @@ var db persistence.SessionsYamlDB // NewComponent starts a new component and wraps it in // a stream manager that you should start yourself -func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.StreamManager, error) { +func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.StreamManager, *xmpp.Component, error) { var err error jid, err = xmpp.NewJid(conf.Jid) if err != nil { - return nil, err + return nil, nil, err } tgConf = tc err = loadSessions(conf.Db) if err != nil { - return nil, err + return nil, nil, err } options := xmpp.ComponentOptions{ @@ -54,14 +54,14 @@ func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.Strea component, err := xmpp.NewComponent(options, router) if err != nil { - return nil, err + return nil, nil, err } - cm := xmpp.NewStreamManager(component, nil) + sm := xmpp.NewStreamManager(component, nil) go heartbeat(component) - return cm, nil + return sm, component, nil } func logPresence(err error, presence *stanza.Presence) { @@ -228,3 +228,22 @@ func sendPresence(component *xmpp.Component, to string, args ...args.V) error { return nil } + +// Close gracefully terminates the component and saves active sessions +func Close(component *xmpp.Component) { + log.Error("Disconnecting...") + + for _, session := range sessions { + session.Disconnect() + } + + db.Transaction(func() bool { + for jid, session := range sessions { + db.Data.Sessions[jid] = *session.Session + } + + return true + }, persistence.SessionMarshaller) + + component.Disconnect() +} |