Skip to content

djanowski/batch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Batch

Keep your batch jobs under control.

Description

Say you have a thousand images to process. You write a script, fire it and go to bed, only to realize the morning after that an exception was raised and the script was aborted. Well, no more frustration: now you can use Batch to make sure your script continues working despite those exceptions, and you can now get a nice report to read while you drink your morning coffee.

Usage

require "batch"

Batch.each(Model.all) do |model|
  # do something with model
  # and see the overall progress
end

Given that Model.all responds to each and size, you'll get a nice progress report:

  0% ...........................................................................
 25% ...........................................................................
 50% ...........................................................................
 75% ...........................................................................
100%

If errors occur, they are handled so that your long-running scripts don't get interrupted right after you go to bed:

  0% .......E...................................................................
 25% ..........................................E................................
 50% ....................E......................................................
 75% ...........................................................................
100%

Some errors occured:

# ... detailed exceptions here

You can determine the line width by setting the environment variable BATCH_WIDTH, which defaults to 75.

Disabling output

On some environments, like a non-interactive shell, you probably want Batch to still run your stuff and skip errors, but you don't want all the progress output. For this purpose you can tweak BATCH_INTERACTIVE:

$ BATCH_INTERACTIVE=0 rake foo

It's probably useful to have BATCH_INTERACTIVE set to 0 on your crontabs.

Debugging

If you want Batch to halt as soon as there's an exception (just like a regular each loop would do), set BATCH_DEBUG:

$ BATCH_DEBUG=1 ruby <your-batch-script>

Batch will also honor the $DEBUG flag and halt if an exception is caught.

Installation

$ gem install batch

About

Iterate over Enumerables with progress reporting.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages