Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Add support for resuming a ProgressBar #46242

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

Merged
merged 1 commit into from
Jul 23, 2022
Merged

[Console] Add support for resuming a ProgressBar #46242

merged 1 commit into from
Jul 23, 2022

Conversation

yivi
Copy link
Contributor

@yivi yivi commented May 3, 2022

Add an (int) $resumeAt argument for ProgressBar#start(), so that the progress bar is initialized at a specific progress level, with a new property so that getEstimated() and getRemaining() report the right number.

Q A
Branch? 6.1
Bug fix? no
New feature? yes
Deprecations? no
License MIT

As pointed out on a recently posted SO question), when working on a longish task one may want to create a progress bar for a resumed task.

Calling advance() works to increase the progress bar counter, but the results of getRemaining() and getEstimated() will be "broken", since whatever steps one "skipped" will be considered to have taken 0 seconds. For a longer running task, the estimation will be very inaccurate, and will remain so for a long while. Using the example code from the linked question:

$itemCount = 1_000_000;
$startItem = 150_000;

$progressBar = new ProgressBar($output, $itemCount);
$progressBar->setFormat(
    $progressBar->getFormatDefinition(ProgressBar::FORMAT_DEBUG)
);

$progressBar->start();

if ($startItem !== 0) {
    // unfortunately taken into account in ETA calculation
    $progressBar->advance($startItem);
}

for ($i = $startItem; $i < $itemCount; $i++) {
    usleep(50_000);
    $progressBar->advance();
}

This will output this to begin with:

150038/1000000 [====>-----------------------]  15% 2 secs/13 secs 20.0 MiB

The estimated time will keep growing as the tasks progresses, but the estimation won't be remotely useful for a long while.

This PR adds a $resumeAt parameter to ProgresBar#start(), so that's possible to write:

$bar = new ProgressBar($output, 1_200_000, 0);
$bar->start(null, 300_000);

And calls to $bar->getEstimated() and $bar->getRemaining() will return sane results (and calling the initial ->advance() would be no longer necessary).

TODO:

  • submit changes to the documentation (if the PR is well received)

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.1 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot
Copy link

Hey!

I think @boesing has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@chalasr chalasr modified the milestones: 6.1, 6.2 May 4, 2022
@fabpot
Copy link
Member

fabpot commented Jul 23, 2022

Thank you @yivi.

@fabpot fabpot merged commit 9b6dba1 into symfony:6.2 Jul 23, 2022
fabpot added a commit that referenced this pull request Aug 5, 2022
…uiluz)

This PR was squashed before being merged into the 6.2 branch.

Discussion
----------

[Console] Rename some arguments in ProgressBar

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

While documenting #46242, I thought something was odd: the code and docs talk about "resuming progress bars". But that's only a use-case of this feature. There are others: e.g. some task with optional steps; if you skip some step, you might want to show the progress at a certain starting point instead of `0`.

So, this PR propose to rename some arguments. In any case, this is minor, so feel free to close if you think this is not relevant enough.

Commits
-------

c03e815 [Console] Rename some arguments in ProgressBar
@fabpot fabpot mentioned this pull request Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants