forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reapply "Refactor the Celery Beat integration (getsentry#3105)" (gets…
…entry#3144) (getsentry#3175) This reverts the revert that was done to mitigate the regression error with Crons not being sending ok/error checkins. This reapplies the refactoring and also fixes the root cause of the regression and also adds integration tests to make sure it does not happen again.
- Loading branch information
1 parent
d9fb440
commit 25b75af
Showing
10 changed files
with
463 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,8 @@ jobs: | |
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Start Redis | ||
uses: supercharge/[email protected] | ||
- name: Setup Test Env | ||
run: | | ||
pip install coverage tox | ||
|
@@ -108,6 +110,8 @@ jobs: | |
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Start Redis | ||
uses: supercharge/[email protected] | ||
- name: Setup Test Env | ||
run: | | ||
pip install coverage tox | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,11 @@ | |
- uses: getsentry/action-clickhouse-in-ci@v1 | ||
{% endif %} | ||
|
||
{% if needs_redis %} | ||
- name: Start Redis | ||
uses: supercharge/[email protected] | ||
{% endif %} | ||
|
||
- name: Setup Test Env | ||
run: | | ||
pip install coverage tox | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import os | ||
import signal | ||
import tempfile | ||
import threading | ||
import time | ||
|
||
from celery.beat import Scheduler | ||
|
||
from sentry_sdk.utils import logger | ||
|
||
|
||
class ImmediateScheduler(Scheduler): | ||
""" | ||
A custom scheduler that starts tasks immediately after starting Celery beat. | ||
""" | ||
|
||
def setup_schedule(self): | ||
super().setup_schedule() | ||
for _, entry in self.schedule.items(): | ||
self.apply_entry(entry) | ||
|
||
def tick(self): | ||
# Override tick to prevent the normal schedule cycle | ||
return 1 | ||
|
||
|
||
def kill_beat(beat_pid_file, delay_seconds=1): | ||
""" | ||
Terminates Celery Beat after the given `delay_seconds`. | ||
""" | ||
logger.info("Starting Celery Beat killer...") | ||
time.sleep(delay_seconds) | ||
pid = int(open(beat_pid_file, "r").read()) | ||
logger.info("Terminating Celery Beat...") | ||
os.kill(pid, signal.SIGTERM) | ||
|
||
|
||
def run_beat(celery_app, runtime_seconds=1, loglevel="warning", quiet=True): | ||
""" | ||
Run Celery Beat that immediately starts tasks. | ||
The Celery Beat instance is automatically terminated after `runtime_seconds`. | ||
""" | ||
logger.info("Starting Celery Beat...") | ||
pid_file = os.path.join(tempfile.mkdtemp(), f"celery-beat-{os.getpid()}.pid") | ||
|
||
t = threading.Thread( | ||
target=kill_beat, | ||
args=(pid_file,), | ||
kwargs={"delay_seconds": runtime_seconds}, | ||
) | ||
t.start() | ||
|
||
beat_instance = celery_app.Beat( | ||
loglevel=loglevel, | ||
quiet=quiet, | ||
pidfile=pid_file, | ||
) | ||
beat_instance.run() |
Oops, something went wrong.