Skip to content

Commit

Permalink
perf: rebuild website search index in background (#17974)
Browse files Browse the repository at this point in the history
* perf: rebuild website search index in background

* refactor: allow enqueueing jobs during migrate

This was added a long time ago to handle missing redis during migrate.
It is not the case right now as redis HAS to be availabe during
migration.

ref: #2988

* ci: pass correct build type

* chore: warn about redis unavailability
  • Loading branch information
ankush committed Aug 26, 2022
1 parent 86c6244 commit fbee80f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ui-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
run: |
python "${GITHUB_WORKSPACE}/.github/helper/roulette.py"
env:
TYPE: "server"
TYPE: "ui"
PR_NUMBER: ${{ github.event.number }}
REPO_NAME: ${{ github.repository }}

Expand Down
4 changes: 2 additions & 2 deletions frappe/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ def tearDown(self):
json.dump(list(frappe.flags.touched_tables), f, sort_keys=True, indent=4)

if not self.skip_search_index:
print(f"Building search index for {frappe.local.site}")
build_index_for_all_routes()
print(f"Queued rebuilding of search index for {frappe.local.site}")
frappe.enqueue(build_index_for_all_routes, queue="long")

frappe.publish_realtime("version-update")
frappe.flags.touched_tables.clear()
Expand Down
14 changes: 10 additions & 4 deletions frappe/utils/background_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
from collections import defaultdict
from functools import lru_cache
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any
from uuid import uuid4

import redis
Expand Down Expand Up @@ -55,7 +55,7 @@ def enqueue(
*,
at_front=False,
**kwargs,
) -> "Job":
) -> "Job" | Any:
"""
Enqueue method to be executed using a background worker
Expand All @@ -78,11 +78,17 @@ def enqueue(
)
)

call_directly = now or frappe.flags.in_migrate or (not is_async and not frappe.flags.in_test)
call_directly = now or (not is_async and not frappe.flags.in_test)
if call_directly:
return frappe.call(method, **kwargs)

q = get_queue(queue, is_async=is_async)
try:
q = get_queue(queue, is_async=is_async)
except ConnectionError:
# If redis is not available for queueing execute the job directly
print(f"Redis queue is unreachable: Executing {method} synchronously")
return frappe.call(method, **kwargs)

if not timeout:
timeout = get_queues_timeout().get(queue) or 300
queue_args = {
Expand Down

0 comments on commit fbee80f

Please sign in to comment.