Skip to content

Commit

Permalink
Add option for customizing single-file timeout (#688)
Browse files Browse the repository at this point in the history
* Promoting singlefile timeout to env variable

* Promoting singlefile timeout to env variable

* add tests

---------

Co-authored-by: Sascha Ißbrücker <sascha.issbruecker@gmail.com>
  • Loading branch information
pettijohn and sissbruecker committed Apr 7, 2024
1 parent 5e8f5b2 commit 2d22d68
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bookmarks/services/singlefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def create_snapshot(url: str, filepath: str):
try:
# Use start_new_session=True to create a new process group
process = subprocess.Popen(args, start_new_session=True)
process.wait(timeout=60)
process.wait(timeout=settings.LD_SINGLEFILE_TIMEOUT_SEC)

# check if the file was created
if not os.path.exists(temp_filepath):
Expand Down
23 changes: 22 additions & 1 deletion bookmarks/tests/test_singlefile_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import subprocess
from unittest import mock

from django.test import TestCase
from django.test import TestCase, override_settings

from bookmarks.services import singlefile

Expand Down Expand Up @@ -50,3 +50,24 @@ def test_create_snapshot_failure(self):
with mock.patch("subprocess.Popen"):
with self.assertRaises(singlefile.SingeFileError):
singlefile.create_snapshot("http://example.com", self.html_filepath)

def test_create_snapshot_default_timeout_setting(self):
mock_process = mock.Mock()
mock_process.wait.return_value = 0
self.create_test_file()

with mock.patch("subprocess.Popen", return_value=mock_process):
singlefile.create_snapshot("http://example.com", self.html_filepath)

mock_process.wait.assert_called_with(timeout=60)

@override_settings(LD_SINGLEFILE_TIMEOUT_SEC=120)
def test_create_snapshot_custom_timeout_setting(self):
mock_process = mock.Mock()
mock_process.wait.return_value = 0
self.create_test_file()

with mock.patch("subprocess.Popen", return_value=mock_process):
singlefile.create_snapshot("http://example.com", self.html_filepath)

mock_process.wait.assert_called_with(timeout=120)
7 changes: 7 additions & 0 deletions docs/Options.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,10 @@ See the default URL for how to insert the placeholder to the favicon provider UR

Alternative favicon providers:
- DuckDuckGo: `https://icons.duckduckgo.com/ip3/{domain}.ico`


### `LD_SINGLEFILE_TIMEOUT_SEC`

Values: `Float` | Default = 60.0

When creating archive snapshots, control the timeout for how long to wait for `single-file` to complete, in `seconds`. Defaults to 60 seconds; on lower-powered hardware you may need to increase this value.
1 change: 1 addition & 0 deletions siteroot/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@
)
LD_SINGLEFILE_PATH = os.getenv("LD_SINGLEFILE_PATH", "single-file")
LD_SINGLEFILE_OPTIONS = os.getenv("LD_SINGLEFILE_OPTIONS", "")
LD_SINGLEFILE_TIMEOUT_SEC = float(os.getenv("LD_SINGLEFILE_TIMEOUT_SEC", 60))

# Monolith isn't used at the moment, as the local snapshot implementation
# switched to single-file after the prototype. Keeping this around in case
Expand Down

0 comments on commit 2d22d68

Please sign in to comment.