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

Allow heartbeats to be sent in tests #6632

Merged
merged 2 commits into from
Feb 24, 2021

Conversation

danihodovic
Copy link
Contributor

@danihodovic danihodovic commented Feb 11, 2021

I'm writing a Prometheus exporter at
https://github.com/danihodovic/celery-exporter.

In order to test the worker events: worker-heartbeat, worker-online,
worker-offline I need to be able to enable heartbeats for the testing
worker.

The change allows for overriding the default value of heartbeat = False.

I'm writing a Prometheus exporter at
https://github.com/danihodovic/celery-exporter.

In order to test the worker events: worker-heartbeat, worker-online,
worker-offline I need to be able to enable heartbeats for the testing
worker.
@lgtm-com
Copy link

lgtm-com bot commented Feb 11, 2021

This pull request fixes 1 alert when merging b6376da into 948bb79 - view on LGTM.com

fixed alerts:

  • 1 for Non-exception in 'except' clause

@@ -119,7 +119,7 @@ def _start_worker_thread(app,
logfile=logfile,
# not allowed to override TestWorkController.on_consumer_ready
ready_callback=None,
without_heartbeat=True,
without_heartbeat=kwargs.pop("without_heartbeat", True),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't going to break the existing API write? shouldn't this overridable feature mentioned in the docs? and can we provide some sort of unit tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value is True, so this should be backwards compatible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add documentation for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, probably next week.

Copy link
Member

@thedrow thedrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solution means you'll need to overwrite the celery_worker/celery_session_worker fixtures to override the value of without_heartbeat.
This isn't the most comfortable API.

We should change https://github.com/celery/celery/blob/master/celery/contrib/pytest.py#L145 instead to return {"without_heartbeat": True} and ensure it is passed onwards.

@danihodovic
Copy link
Contributor Author

danihodovic commented Feb 14, 2021

We should change https://github.com/celery/celery/blob/master/celery/contrib/pytest.py#L145 instead to return {"without_heartbeat": True} and ensure it is passed onwards.

The question is if this breaks existing behavior for users that don't use the pytest fixture? By default the value is False -

without_heartbeat=False, heartbeat_interval=None, **kwargs):
.

WorkController behavior would silently change and heartsbeats would be enabled.

This solution means you'll need to overwrite the celery_worker/celery_session_worker fixtures to override the value of without_heartbeat.

No, you can override celery_worker_parameters as seen here:
https://github.com/danihodovic/celery-exporter/blob/e5430516de22b9f095da4d8d099169ec518b6670/conftest.py#L15-L18

@thedrow
Copy link
Member

thedrow commented Feb 15, 2021

We should change https://github.com/celery/celery/blob/master/celery/contrib/pytest.py#L145 instead to return {"without_heartbeat": True} and ensure it is passed onwards.

The question is if this breaks existing behavior for users that don't use the pytest fixture? By default the value is False -

without_heartbeat=False, heartbeat_interval=None, **kwargs):
.

WorkController behavior would silently change and heartsbeats would be enabled.

In that case we still need to check if celery_worker_parameters contains the without_heartbeat key and if it does not, we need to add it ourselves.

This solution means you'll need to overwrite the celery_worker/celery_session_worker fixtures to override the value of without_heartbeat.

No, you can override celery_worker_parameters as seen here:
https://github.com/danihodovic/celery-exporter/blob/e5430516de22b9f095da4d8d099169ec518b6670/conftest.py#L15-L18

I meant the solution proposed in this PR, not the solution I'm proposing.

@danihodovic
Copy link
Contributor Author

In that case we still need to check if celery_worker_parameters contains the without_heartbeat key and if it does not, we need to add it ourselves.

Hmm... I'm confused because that's what this PR does https://github.com/celery/celery/pull/6632/files#diff-fee53e249a978fd480b1cade781b4e997b18492ab090d76d0377786d99128b92R122

kwargs in _start_worker_thread is passed in from celery_worker_parameters

@thedrow
Copy link
Member

thedrow commented Feb 15, 2021

Gottcha 🤦
The lack of documentation (that's our fault) confused me.

@danihodovic
Copy link
Contributor Author

Gottcha facepalm
The lack of documentation (that's our fault) confused me.

No worries, happy we cleared it up. I'll be adding some docs later this week.

@lgtm-com
Copy link

lgtm-com bot commented Feb 21, 2021

This pull request introduces 1 alert and fixes 1 when merging 412d576 into 1eade4c - view on LGTM.com

new alerts:

  • 1 for Module is imported with 'import' and 'import from'

fixed alerts:

  • 1 for Non-exception in 'except' clause

@auvipy auvipy merged commit ad3ec27 into celery:master Feb 24, 2021
@auvipy
Copy link
Member

auvipy commented Feb 24, 2021

thanks!

@thedrow thedrow added this to Done in Celery 5.1.0 Feb 24, 2021
jeyrce pushed a commit to jeyrce/celery that referenced this pull request Aug 25, 2021
* Allow heartbeats to be sent in tests

I'm writing a Prometheus exporter at
https://github.com/danihodovic/celery-exporter.

In order to test the worker events: worker-heartbeat, worker-online,
worker-offline I need to be able to enable heartbeats for the testing
worker.

* Add docs on heartbeats in tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Celery 5.1.0
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants