1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Progress Reporting To Long Running Script as a Ruby TIL

This commit is contained in:
jbranchaud
2023-12-05 16:28:05 -06:00
parent c591565058
commit dd13203ade
2 changed files with 48 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1355 TILs and counting..._
_1356 TILs and counting..._
---
@@ -1045,6 +1045,7 @@ _1355 TILs and counting..._
- [A Shorthand For Rerunning Failed Tests With RSpec](ruby/a-shorthand-for-rerunning-failed-tests-with-rspec.md)
- [Add Comments To Regex With Free-Spacing](ruby/add-comments-to-regex-with-free-spacing.md)
- [Add Linux As A Bundler Platform](ruby/add-linux-as-a-bundler-platform.md)
- [Add Progress Reporting To Long-Running Script](ruby/add-progress-reporting-to-long-running-script.md)
- [Are They All True?](ruby/are-they-all-true.md)
- [Assert About An Object's Attributes With RSpec](ruby/assert-about-an-objects-attributes-with-rspec.md)
- [Assoc For Hashes](ruby/assoc-for-hashes.md)

View File

@@ -0,0 +1,46 @@
# Add Progress Reporting To Long-Running Script
A script that is going to iterate over and process a ton of data can take a
long time to run. It can be hard to predict how long it will take to run. So,
it is nice to have some indication of it's progress as it goes.
Here is a small snippet that I add to the main loop so that I have some
feedback while the script runs:
```ruby
running_count = 0
tons_of_data.each do |item|
running_count += 1
if running_count % 1000 == 0
print '@'
elsif running_count % 100 == 0
print '*'
else
print '.'
end
# process this `item` ...
end
```
Then I get some output that looks like this over time as the script runs.
```
...............................................................................
....................*..........................................................
.........................................*.....................................
..............................................................*................
...............................................................................
....*..........................................................................
.........................*.....................................................
..............................................*................................
...................................................................*...........
...............................................................................
.........*.....................................................................
..............................*................................................
...................................................@..................
```
I can get a sense of how quickly it is processing each record and roughly count
how far along it is.