Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from lockfile to filelock #180

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ ADDITIONAL CONTRIBUTORS include:
* Jaap Roes
* Ed Davison
* Sander Smits
* Steve Kowalik
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ package_dir = =src
python_requires = >=3.8
install_requires =
Django >= 2.2
lockfile >= 0.8
filelock >= 3.5

[options.packages.find]
where = src
Expand Down
13 changes: 7 additions & 6 deletions src/mailer/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import time
from socket import error as socket_error

import lockfile
from django import VERSION as DJANGO_VERSION
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
Expand All @@ -13,6 +12,7 @@
from django.core.mail.utils import DNS_NAME
from django.db import DatabaseError, NotSupportedError, OperationalError, transaction
from django.utils.module_loading import import_string
import filelock

from mailer.models import RESULT_FAILURE, RESULT_SUCCESS, Message, MessageLog, get_message_id

Expand Down Expand Up @@ -153,14 +153,15 @@ def acquire_lock():
else:
lock_file_path = "send_mail"

lock = lockfile.FileLock(lock_file_path)
lock = filelock.FileLock(lock_file_path)

try:
lock.acquire(LOCK_WAIT_TIMEOUT)
except lockfile.AlreadyLocked:
if lock.is_locked:
logger.error("lock already in place. quitting.")
return False, lock
except lockfile.LockTimeout:

try:
lock.acquire(timeout=LOCK_WAIT_TIMEOUT)
except filelock.Timeout:
logger.error("waiting for the lock timed out. quitting.")
return False, lock
logger.debug("acquired.")
Expand Down
28 changes: 15 additions & 13 deletions tests/test_mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from unittest.mock import Mock, PropertyMock, patch

import django
import lockfile
import filelock
import mailer
from django.core import mail
from django.core.exceptions import ImproperlyConfigured
Expand Down Expand Up @@ -438,16 +438,18 @@ class CustomError(Exception):
self.CustomError = CustomError

self.lock_mock = Mock()
is_locked = PropertyMock(return_value=False)
type(self.lock_mock).is_locked = is_locked

self.patcher_lock = patch("lockfile.FileLock", return_value=self.lock_mock)
self.patcher_lock = patch("filelock.FileLock", return_value=self.lock_mock)
self.patcher_prio = patch("mailer.engine.prioritize", side_effect=CustomError)

self.lock = self.patcher_lock.start()
self.prio = self.patcher_prio.start()

def test(self):
self.assertRaises(self.CustomError, engine.send_all)
self.lock_mock.acquire.assert_called_once_with(engine.LOCK_WAIT_TIMEOUT)
self.lock_mock.acquire.assert_called_once_with(timeout=engine.LOCK_WAIT_TIMEOUT)
self.lock.assert_called_once_with("send_mail")
self.prio.assert_called_once()

Expand All @@ -458,21 +460,19 @@ def tearDown(self):

class LockLockedTest(TestCase):
def setUp(self):
config = {
"acquire.side_effect": lockfile.AlreadyLocked,
}
self.lock_mock = Mock(**config)
lock_mock = Mock()
self.is_locked = PropertyMock(return_value=True)
type(lock_mock).is_locked = self.is_locked

self.patcher_lock = patch("lockfile.FileLock", return_value=self.lock_mock)
self.patcher_lock = patch("filelock.FileLock", return_value=lock_mock)
self.patcher_prio = patch("mailer.engine.prioritize", side_effect=Exception)

self.lock = self.patcher_lock.start()
self.prio = self.patcher_prio.start()

def test(self):
engine.send_all()
self.lock_mock.acquire.assert_called_once_with(engine.LOCK_WAIT_TIMEOUT)
self.lock.assert_called_once_with("send_mail")
self.is_locked.assert_called_once_with()
self.prio.assert_not_called()

def tearDown(self):
Expand All @@ -483,19 +483,21 @@ def tearDown(self):
class LockTimeoutTest(TestCase):
def setUp(self):
config = {
"acquire.side_effect": lockfile.LockTimeout,
"acquire.side_effect": filelock.Timeout("send_mail"),
}
self.lock_mock = Mock(**config)
is_locked = PropertyMock(return_value=False)
type(self.lock_mock).is_locked = is_locked

self.patcher_lock = patch("lockfile.FileLock", return_value=self.lock_mock)
self.patcher_lock = patch("filelock.FileLock", return_value=self.lock_mock)
self.patcher_prio = patch("mailer.engine.prioritize", side_effect=Exception)

self.lock = self.patcher_lock.start()
self.prio = self.patcher_prio.start()

def test(self):
engine.send_all()
self.lock_mock.acquire.assert_called_once_with(engine.LOCK_WAIT_TIMEOUT)
self.lock_mock.acquire.assert_called_once_with(timeout=engine.LOCK_WAIT_TIMEOUT)
self.lock.assert_called_once_with("send_mail")
self.prio.assert_not_called()

Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ envlist =
py39-django{30,31,32,40}-test,
py310-django{40,41,42}-test,
py311-django42-test,
py312-django42-test,
flake-py39-django32,
checkmanifest-py39,

Expand All @@ -15,6 +16,7 @@ basepython =
py39: python3.9
py310: python3.10
py311: python3.11
py312: python3.12

commands =
test: pytest --cov=mailer
Expand Down