Skip to content

alexander-ziegler/intensifier

Repository files navigation

Intensifier

This gem provides a flexible mechanism for deciding when a task has grown too large for in-memory execution and should be offloaded to a background job system. It offers a lightweight interface for delegating work, integrating cleanly with any job processor without imposing infrastructure choices. By decoupling heavy workloads from the request path, it helps applications stay responsive under data-intensive conditions.

Installation

To install the gem, add it to your application's Gemfile:

bundle add intensifier

If bundler is not being used to manage dependencies, install the gem by executing:

gem install intensifier

Usage

To use the intensifier gem, you can create an instance of Intensifier for each processing job, allowing for specific configurations such as execution time thresholds and background job handlers.

Instance Configuration

Create an instance of Intensifier with specific configurations:

intensifier_csv_importer = Intensifier.new(
  background_job: SideKiqCsvImporter,
  execution_time_threshold: 3 # overrides general configuration
)

Running a Job

Use the run method to execute a block of code and handle any intensity checks:

intensifier_csv_importer.run do |intensity|
  # Simulate a long-running task
  sleep(10)
  if intensity.too_intense?
    intensity.offload! do |background_job|
      background_job.perform_async(csv_data)
    end
  else
    puts "Completed CSV Import"
  end
end

Execution Time Monitoring

The intensifier gem allows you to monitor the execution time of processes and determine if they are too intense based on a configurable threshold.

Configuration

To enable execution time monitoring, set the execution_time_threshold in seconds. If a process takes longer than this threshold, it will be considered too intense.

intensifier_csv_importer = Intensifier.new(
  background_job: SideKiqCsvImporter,
  execution_time_threshold: 2.0 # Set the threshold to 2 seconds
)

Memory Usage Monitoring

The intensifier gem allows you to monitor the memory usage of processes and determine if they are too intense based on a configurable threshold.

Configuration

To enable memory usage monitoring, set the memory_usage_threshold in megabytes (MB). If a process uses more memory than this threshold, it will be considered too intense.

intensifier_csv_importer = Intensifier.new(
  background_job: SideKiqCsvImporter,
  memory_usage_threshold: 150 # Set the threshold to 150MB
)

Using Intensifier.run as a Transaction Block

The Intensifier.run method now acts as a transaction block, yielding an IntensityContext object to the block. This allows you to check process intensity and offload processes if necessary.

Methods Available

  • too_intense?: Checks if the process is too intense based on configured thresholds.
  • offload!: Offloads the process to a background job if it is too intense.

Development

After checking out the repo, run bin/setup to install dependencies. Then, you can run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/your_username/intensifier. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

About

gem to manage costly processes

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors