Skip to content

Use celery app timezone - #1351

Open
woutdenolf wants to merge 4 commits into
mher:masterfrom
woutdenolf:support_enable_utc
Open

Use celery app timezone#1351
woutdenolf wants to merge 4 commits into
mher:masterfrom
woutdenolf:support_enable_utc

Conversation

@woutdenolf

@woutdenolf woutdenolf commented Jan 22, 2024

Copy link
Copy Markdown

Closes #1132

Changes

  • Introduce the flower config parameter browser_local_time (bool) to show the time in the Celery app timezone or the browser local timezone.
  • When using the Celery app timezone, respect the Celery option enable_utc (when Celery option timezone is missing the celery app uses UTC or the system TZ).
  • The timezone is shown as a tooltip when hovering over the time.
  • The full time + timezone is shown as a tooltip when hovering over the time when natural_time=True.

Implementation

The task started and received times in the task table are derived from the raw celery events which provide times as number of seconds passed since the epoch.

time += '-' + str(capp.conf.timezone)

This code takes timezone into account but not enable_utc.

https://docs.celeryq.dev/en/stable/userguide/configuration.html#time-and-date-settings

Instead of using the time and date settings from the celery configuration directly, this PR proposes to use the timezone provided by the celery app

https://github.com/celery/celery/blob/8f389997887232500d4aa1a2b0ae0c7320c4c84a/celery/app/base.py#L1360

This allows using an explicit timezone (e.g. timezone = "Europe/Paris") or the local timezone (enable_utc = False).

Repository owner deleted a comment Jan 29, 2024
@woutdenolf
woutdenolf marked this pull request as ready for review March 28, 2024 13:16
@woutdenolf

woutdenolf commented Apr 7, 2025

Copy link
Copy Markdown
Author

@mher Let me know if you need something. The main idea here is the use capp.timezone instead of capp.conf.timezone so that we can take enable_utc = False into account.

@auvipy
auvipy requested review from auvipy and Copilot August 12, 2025 16:36

This comment was marked as outdated.

@auvipy auvipy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

is it possible to add some tests to verify this?

@auvipy
auvipy requested a review from Copilot August 12, 2025 16:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR improves timezone handling in Flower's task view by using the celery app's configured timezone instead of directly accessing configuration settings. The change ensures proper timezone display for task timestamps whether using explicit timezone settings or local timezone when UTC is disabled.

  • Updates timezone detection logic to use capp.timezone.tzname() instead of capp.conf.timezone
  • Refactors time type variable naming for better clarity
  • Adds datetime import to support timezone name resolution

Comment thread flower/views/tasks.py Outdated
@woutdenolf

woutdenolf commented Aug 12, 2025

Copy link
Copy Markdown
Author

I added UTC and non-UTC tests. copilot broke the PR so I fixed that as well.

@woutdenolf

woutdenolf commented Aug 12, 2025

Copy link
Copy Markdown
Author

I will try to add tests for enable_utc as well.

@woutdenolf
woutdenolf force-pushed the support_enable_utc branch 2 times, most recently from 00f9b8d to 2110525 Compare August 12, 2025 18:36
@woutdenolf

Copy link
Copy Markdown
Author

Ok I tested all permutations of capp.conf.timezone and capp.conf.enable_utc. @auvipy Can you review again?

@woutdenolf

woutdenolf commented Aug 12, 2025

Copy link
Copy Markdown
Author

The front-end does not actually show the timezone.

image

Do you think this is required for this PR or can this be another PR?

Edit: I modified flower.js so the timezone is shown as a tooltip when hovering over the time.

@woutdenolf
woutdenolf force-pushed the support_enable_utc branch 4 times, most recently from a8cee47 to 59424c3 Compare August 13, 2025 03:56
Comment thread flower/views/tasks.py Outdated
@woutdenolf
woutdenolf force-pushed the support_enable_utc branch 5 times, most recently from 2b2f7ca to 5ac407f Compare August 13, 2025 06:23
@auvipy

auvipy commented Aug 13, 2025

Copy link
Copy Markdown
Collaborator

************* Module flower.views.tasks
flower/views/tasks.py:3:0: W0611: Unused datetime imported from datetime (unused-import)

Comment thread tests/unit/views/test_tasks.py Outdated
@woutdenolf
woutdenolf requested a review from auvipy August 13, 2025 19:15
@woutdenolf
woutdenolf force-pushed the support_enable_utc branch 3 times, most recently from a3538ea to 01d1b6f Compare August 13, 2025 20:43
@woutdenolf

woutdenolf commented Aug 17, 2025

Copy link
Copy Markdown
Author

Thanks for running the pipeline and sorry for not checking all permutations locally. The tox.ini is broken. This would fix it:

[tox]
envlist =
    # Celery 5.2: only py38–py311 (py312 excluded)
    {py38,py39,py310,py311}-celery52-{tornado60,tornado61,tornado62,tornado63,tornado64,tornado65},
    # Celery 5.3: py38–py312
    {py38,py39,py310,py311,py312}-celery53-{tornado60,tornado61,tornado62,tornado63,tornado64,tornado65},
    # Celery 5.4: py38–py312
    {py38,py39,py310,py311,py312}-celery54-{tornado60,tornado61,tornado62,tornado63,tornado64,tornado65},
    # Celery 5.5: py38–py312
    {py38,py39,py310,py311,py312}-celery55-{tornado60,tornado61,tornado62,tornado63,tornado64,tornado65},
    lint
skip_missing_interpreters = true

[testenv]
deps =
    -r requirements/dev.txt
    celery52: celery==5.2.*
    celery53: celery==5.3.*
    celery54: celery==5.4.*
    celery55: celery==5.5.*
    tornado60: tornado==6.0.*
    tornado61: tornado==6.1.*
    tornado62: tornado==6.2.*
    tornado63: tornado==6.3.*
    tornado64: tornado==6.4.*
    tornado65: tornado==6.5.*
commands =
    python -m flower --version
    python -m tests.unit

[testenv:lint]
deps = pylint
commands = pylint flower --rcfile .pylintrc

Do you think it is worth fixing? If yes I'll make an issue and a PR.

@auvipy

auvipy commented Aug 17, 2025

Copy link
Copy Markdown
Collaborator

I have opened a PR based on your suggestion #1451

@loichuder

loichuder commented Sep 3, 2026

Copy link
Copy Markdown

Since #1451 is merged, I guess this is now ready for review again?

We would love to have this in flower 🙂

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The implementation is coherent and CI passes, with only a nonblocking test assertion weakness remaining.

Review details
  • Files reviewed: 7/7 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment thread tests/unit/views/test_tasks.py Outdated
@auvipy
auvipy requested a review from mher September 3, 2026 17:23
@mher

mher commented Sep 4, 2026

Copy link
Copy Markdown
Owner

#1132 (comment) shows how to set the local time zone. I don't think we need an additional configuration option.

@woutdenolf

woutdenolf commented Sep 5, 2026

Copy link
Copy Markdown
Author

Setting timezone in a celeryconfig.py works, but this PR covers much more (see the PR description).

These two have been causing us trouble for years:

  • Browser local time. Opt-in when the user cares about its own timezone, not the timezone celery happens to be configured in.
  • Showing which zone is in use. Today the table prints 2024-01-01 12:00:00.000 with no zone anywhere, so you can't tell what you're looking at — that's really why Flower show wrong time if timezone set #1132 reads as "Flower subtracts hours". The tooltip fixes that, and with natural_time, where the cell only says "2 minutes ago", it also gives you the actual timestamp.

The enable_utc part is a smaller fix along the way. With timezone unset and enable_utc = False, Celery means local time. Flower renders the table twice: the initial HTML goes through current_app.timezone and shows local time, then the DataTables refresh re-renders the same rows in JS, where the fallback is a hardcoded 'UTC'. So the times shift as soon as the page updates.

@woutdenolf

Copy link
Copy Markdown
Author

#1132 (comment) shows how to set the local time zone. I don't think we need an additional configuration option.

You refer to server-side timezone. The additional browser_local_time option refers to client-side timezone.

@woutdenolf

woutdenolf commented Sep 6, 2026

Copy link
Copy Markdown
Author

Rebased on master to

  • resolve conflicts
  • drop two commits related to python 3.8 (flower requires >=3.10 by now)

@woutdenolf

Copy link
Copy Markdown
Author

An example of what the user sees in the tooltip when hovering over the time

image

Which timezone is used

browser_local_time Celery timezone Celery enable_utc Timezone used
False (default) set any (ignored) the configured zone
False (default) unset True (default) UTC
False (default) unset False the Flower host's local zone
True any (ignored) any (ignored) the viewer's browser zone

What is shown

Example: a task received at 11:30:00 UTC, page viewed 5 minutes later, timezone in use Asia/Shanghai

natural_time Cell Tooltip
False (default) 2026-09-06 19:30:00.000 5 minutes ago (Asia/Shanghai)
True 5 minutes ago 2026-09-06 19:30:00.000 (Asia/Shanghai)

Same task with the other zone choices, natural_time = False:

Timezone used Cell Tooltip
UTC 2026-09-06 11:30:00.000 5 minutes ago (UTC)
Europe/Brussels (Flower host) 2026-09-06 13:30:00.000 5 minutes ago (Europe/Brussels)
America/New_York (browser) 2026-09-06 07:30:00.000 5 minutes ago (America/New_York)

On master the cell text is the same, but there is no tooltip and no zone shown anywhere on the page. Of the last table only the UTC row is reachable: the JS table falls back to a hardcoded UTC, so neither the Flower host's local zone nor the viewer's browser zone can be selected.

@mher

mher commented Sep 6, 2026

Copy link
Copy Markdown
Owner

#1132 (comment) shows how to set the local time zone. I don't think we need an additional configuration option.

You refer to server-side timezone. The additional browser_local_time option refers to client-side timezone.

If it is a client-side time zone, it should not be configured on the server side. Some clients may want to use the browser time zone, others may not.

@woutdenolf

Copy link
Copy Markdown
Author

If it is a client-side time zone, it should not be configured on the server side.

Indeed but Flower still needs the option (--browser-local-time ) to show the client-side time zone. Otherwise you only see sever-side timezone.

See the last table of #1351 (comment) for an example.

@woutdenolf

Copy link
Copy Markdown
Author

So in terms of parameters we now have these:

  • --natural-time: Flower display option
  • --browser-local-time: Flower display option (NEW)
  • timezone: Celery config
  • enable_utc: Celery config

@woutdenolf

woutdenolf commented Sep 6, 2026

Copy link
Copy Markdown
Author

Some clients may want to use the browser time zone, others may not.

Sure but the same thing can be said for --natural-time.

We could make both --browser-local-time and --natural-time something the user can toggle in their browser (and the flower-slide flag says what the default is). I can make a follow-up PR if you want.

@mher

mher commented Sep 6, 2026

Copy link
Copy Markdown
Owner

Indeed but Flower still needs the option (--browser-local-time ) to show the client-side time zone. Otherwise you only see sever-side timezone.

See the last table of #1351 (comment) for an example.

Why are you adding the time zone to relative-time tooltips? Clients already know what time zone they're in, so there's no need to keep reminding them.

@woutdenolf

woutdenolf commented Sep 6, 2026

Copy link
Copy Markdown
Author

Why are you adding the time zone to relative-time tooltips?

Because it isn't necessarily the client's timezone, it can be the Flower timezone or the Celery configured one.

Lets say you see

2026-09-06 07:30:00.000

and the tooltip says

5 minutes ago

The user would need to do some math to figure out that the timezone of the absolute time shown is (America/New_York). So that's why I'm showing it in the tooltip. Hope it is clear, timezone stuff can be tricky and confusing. We fell over this so many times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flower show wrong time if timezone set

5 participants