Skip to content

Commit

Permalink
Merge pull request #80 from akrherz/gh79_ping_task
Browse files Browse the repository at this point in the history
Fix ping timer
  • Loading branch information
akrherz authored Mar 5, 2024
2 parents 39f848b + 851e413 commit f7867b2
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ci:
autoupdate_schedule: quarterly
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.1.8"
rev: "v0.3.0"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
1 change: 1 addition & 0 deletions iembot.tac
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Our script that is exec'd from twistd via run.sh"""

# Base Python
import json

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

[tool.ruff]
line-length = 79
select = ["E", "F", "I"]
lint.select = ["E", "F", "I"]
target-version = "py39"

1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""our setup file."""

from setuptools import setup

setup(use_scm_version={"version_scheme": "post-release"})
1 change: 1 addition & 0 deletions src/iembot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Placeholder."""

from ._version import get_version # noqa: E402

__version__ = get_version()
21 changes: 11 additions & 10 deletions src/iembot/basicbot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Basic iembot/nwsbot implementation. """
"""Basic iembot/nwsbot implementation."""

import copy
import datetime
import os
Expand All @@ -16,9 +17,8 @@
from twisted.internet.task import LoopingCall
from twisted.python import log
from twisted.python.logfile import DailyLogFile
from twisted.words.protocols.jabber import client, error, jid, xmlstream
from twisted.words.protocols.jabber import client, jid, xmlstream
from twisted.words.xish import domish, xpath
from twisted.words.xish.xmlstream import STREAM_END_EVENT

import iembot.util as botutil

Expand Down Expand Up @@ -78,6 +78,7 @@ def __init__(
self.ingestjid = None
self.conference = None
self.email_timestamps = []
self.keepalive_lc = None # Keepalive LoopingCall
fn = os.path.join(DATADIR, "startrek")
with open(fn, "r", encoding="utf-8") as fp:
self.fortunes = fp.read().split("\n%\n")
Expand Down Expand Up @@ -113,9 +114,10 @@ def authd(self, _xs=None):
self.load_chatrooms(True)
self.load_webhooks()

lc = LoopingCall(self.housekeeping)
lc.start(60)
self.xmlstream.addObserver(STREAM_END_EVENT, lambda _x: lc.stop)
# Start the keepalive loop
if self.keepalive_lc is None:
self.keepalive_lc = LoopingCall(self.housekeeping)
self.keepalive_lc.start(60)

def next_seqnum(self):
"""
Expand Down Expand Up @@ -236,9 +238,8 @@ def housekeeping(self):
"Logging out of Chat!", self, "IQ error limit reached..."
)
if self.xmlstream is not None:
# Unsure of the proper code that a client should generate
exc = error.StreamError("gone")
self.xmlstream.sendStreamError(exc)
# send a disconnect
self.xmlstream.sendFooter()
return
if self.xmlstream is None:
log.msg("xmlstream is None, not sending ping")
Expand All @@ -247,7 +248,7 @@ def housekeeping(self):
ping = domish.Element((None, "iq"))
ping["to"] = self.myjid.host
ping["type"] = "get"
pingid = f"{utcnow:%Y%m%d%H%M}"
pingid = f"{utcnow:%Y%m%d%H%M.%S}"
ping["id"] = pingid
ping.addChild(domish.Element(("urn:xmpp:ping", "ping")))
self.outstanding_pings.append(pingid)
Expand Down
3 changes: 2 additions & 1 deletion src/iembot/iemchatbot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Chat bot implementation of IEMBot """
"""Chat bot implementation of IEMBot"""

import datetime
import re

Expand Down
1 change: 1 addition & 0 deletions src/iembot/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utility functions for IEMBot"""

# pylint: disable=protected-access
import copy
import datetime
Expand Down
1 change: 1 addition & 0 deletions src/iembot/webhooks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Send content to various webhooks."""

import json
from io import BytesIO

Expand Down
1 change: 1 addition & 0 deletions src/iembot/webservices.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Our web services"""

import datetime
import json
import re
Expand Down
1 change: 1 addition & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests, gasp"""

import tempfile
from unittest import mock

Expand Down
1 change: 1 addition & 0 deletions tests/test_xml.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Some random tests"""

from iembot.basicbot import basicbot
from iembot.util import safe_twitter_text

Expand Down

0 comments on commit f7867b2

Please sign in to comment.