Skip to content

Commit b246a8a

Browse files
authored
Merge pull request #310 from akrherz/emailz
Address thread safety issue
2 parents ebd7eee + 5bc70bc commit b246a8a

File tree

7 files changed

+26
-17
lines changed

7 files changed

+26
-17
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ repos:
1010
types: [python]
1111

1212
- repo: https://github.com/astral-sh/ruff-pre-commit
13-
rev: "v0.14.3"
13+
rev: "v0.14.4"
1414
hooks:
1515
- id: ruff
1616
args: [--fix, --exit-non-zero-on-fix]
1717
- id: ruff-format
1818

1919
- repo: https://github.com/tox-dev/pyproject-fmt
20-
rev: 'v2.11.0'
20+
rev: 'v2.11.1'
2121
hooks:
2222
- id: pyproject-fmt
2323

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ authors = [
1919
]
2020
classifiers = [
2121
"Programming Language :: Python :: 3 :: Only",
22-
"Programming Language :: Python :: 3.9",
2322
"Programming Language :: Python :: 3.10",
2423
"Programming Language :: Python :: 3.11",
2524
"Programming Language :: Python :: 3.12",
2625
"Programming Language :: Python :: 3.13",
26+
"Programming Language :: Python :: 3.14",
2727
]
2828
dynamic = [
2929
"version",

src/pywwa/common.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import socket
99
import sys
10+
import threading
1011
import traceback
1112
from email.mime.text import MIMEText
1213
from io import StringIO
@@ -27,6 +28,7 @@
2728
# http://bugs.python.org/issue7980
2829
datetime.datetime.strptime("2013", "%Y")
2930
EMAIL_TIMESTAMPS = []
31+
EMAIL_TIMESTAMPS_LOCK = threading.Lock()
3032

3133

3234
class CustomFormatter(logging.Formatter):
@@ -126,23 +128,28 @@ def load_settings():
126128
cursor.close()
127129

128130

129-
def should_email():
131+
def should_email() -> bool:
130132
"""Prevent email bombs
131133
132134
Use the setting `pywwa_email_limit` to threshold the number of emails
133135
permitted within the past hour
134136
135-
@return boolean if we should email or not
137+
Returns:
138+
bool: True if an email should be sent, False if not
136139
"""
137-
EMAIL_TIMESTAMPS.insert(0, utc())
138-
delta = EMAIL_TIMESTAMPS[0] - EMAIL_TIMESTAMPS[-1]
139-
email_limit = int(SETTINGS.get("pywwa_email_limit", 10))
140-
if len(EMAIL_TIMESTAMPS) < email_limit:
141-
return True
142-
while len(EMAIL_TIMESTAMPS) > email_limit:
143-
EMAIL_TIMESTAMPS.pop()
144-
145-
return delta > datetime.timedelta(hours=1)
140+
with EMAIL_TIMESTAMPS_LOCK:
141+
# Insert current timestamp into the beginning of the list
142+
EMAIL_TIMESTAMPS.insert(0, utc())
143+
# Figure out the amount of time from now to the end of the list
144+
delta = EMAIL_TIMESTAMPS[0] - EMAIL_TIMESTAMPS[-1]
145+
# Figure out the allowed limit over the hour interval
146+
email_limit = int(SETTINGS.get("pywwa_email_limit", 10))
147+
if len(EMAIL_TIMESTAMPS) < email_limit:
148+
return True
149+
while len(EMAIL_TIMESTAMPS) > email_limit:
150+
EMAIL_TIMESTAMPS.pop()
151+
152+
return delta > datetime.timedelta(hours=1)
146153

147154

148155
def email_error(exp, message="", trimstr=100):

src/pywwa/data/pirep_navaids.tbl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ MYCC Cat Cays -- BH 2555 -7928 0 0
44
# Sites found while parsing real data
55
CAA2 Saint Andre Avellin -- CA 4574 -7507 0 0
66
CAA4 Saint Apollinaire -- CA 4659 -7156 0 0
7+
CAA6 Smithers BC CA 5478 -12713 0 0
78
CAA8 Invermere -- CA 5052 -11600 0 0
89
CAA9 Port Alberni BC CA 4928 -12495 0 0
910
CAB3 Bedwell Harbour -- CA 4875 -12323 0 0
@@ -449,6 +450,7 @@ CMA2 Mattawa ON CA 4630 -7875 0 0
449450
CMB2 Meadowbank -- CA 6503 -9607 0 0
450451
CMB7 Maxville -- CA 4525 -7481 0 0
451452
CMB9 Port Renfrew Mill Bay -- CA 4856 -12441 0 0
453+
CMC5 Blackie McElroy Ranch AB CA 5063 -11359 0 0
452454
CMF2 Edmonton Calmar Maplelane AB CA 5318 -11374 0 0
453455
CMH3 Lacombe AB CA 5238 -11382 0 0
454456
CML2 Quamichan Lake Raven BC CA 4882 -12365 0 0

src/pywwa/workflows/spe.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
def real_process(txn, raw):
1616
"""Do work please"""
17-
_sqlraw = raw.replace("\015\015\012", "\n")
1817
prod = product.TextProduct(raw, ugc_provider={}, utcnow=common.utcnow())
1918

2019
product_id = prod.get_product_id()

util/gen_channels_html_table.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ def load_dicts():
320320
nwsli_dict["MLU"] = NWSLI("MLU", lat=32.52, lon=-92.03)
321321
nwsli_dict["IGB"] = NWSLI("MLU", lat=33.48, lon=-88.52)
322322
nwsli_dict["MEI"] = NWSLI("MLU", lat=32.38, lon=-88.80)
323+
pgconn.close()
323324

324325

325326
def do_generic(fh):

util/rotate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def handler(fnbase: str, fmt: str, bio: BytesIO):
2323

2424
# Create the uncompressed version
2525
bio.seek(0)
26-
with gzip.open(bio, "rb") as fh:
27-
data = fh.read()
26+
with gzip.open(bio, "rb") as gzfp:
27+
data = gzfp.read()
2828
fmt = "tif"
2929
else:
3030
data = bio.getvalue()

0 commit comments

Comments
 (0)