Shopify / job-iteration
Makes your background jobs interruptible and resumable by design.
AI Architecture Analysis
This repository is indexed by RepoMind. By analyzing Shopify/job-iteration in our AI interface, you can instantly generate complete architecture diagrams, visualize control flows, and perform automated security audits across the entire codebase.
Our Agentic Context Augmented Generation (Agentic CAG) engine loads full source files into context on-demand, avoiding the fragmentation of traditional RAG systems. Ask questions about the architecture, dependencies, or specific features to see it in action.
Repository Overview (README excerpt)
Crawler viewJob Iteration API Meet Iteration, an extension for ActiveJob that makes your jobs interruptible and resumable, saving all progress that the job has made (aka checkpoint for jobs). Background Imagine the following job: The job would run fairly quickly when you only have a hundred records. But as the number of records grows, it will take longer for a job to iterate over all Users. Eventually, there will be millions of records to iterate and the job will end up taking hours or even days. With frequent deploys and worker restarts, it would mean that a job will be either lost or restarted from the beginning. Some records (especially those in the beginning of the relation) will be processed more than once. Cloud environments are also unpredictable, and there's no way to guarantee that a single job will have reserved hardware to run for hours and days. What if AWS diagnosed the instance as unhealthy and will restart it in 5 minutes? What if a Kubernetes pod is getting evicted? Again, all job progress will be lost. At Shopify, we also use it to interrupt workloads safely when moving tenants between shards and move shards between regions. Software that is designed for high availability must be resilient to interruptions that come from the infrastructure. That's exactly what Iteration brings to ActiveJob. It's been developed at Shopify to safely process long-running jobs, in Cloud, and has been working in production since May 2017. We recommend that you watch one of our conference talks about the ideas and history behind Iteration API. Getting started Add this line to your application's Gemfile: And then execute: $ bundle In the job, include module and start describing the job with two methods ( and ) instead of : will be called for each model in relation. The relation will be ordered by primary key, exactly like does. Check out more examples of Iterations: Iteration hooks into most popular queue adapters out of the box to support graceful interruption. No extra configuration is required. Supported dependencies Job-iteration currently supports the following queue adapters (in order of implementation): • Resque • Sidekiq • GoodJob • Solid Queue • Amazon SQS • Delayed::Job It supports the following platforms: • Ruby 3.1 and later • Rails 7.0 and later Support for older platforms that have reached end of life may occasionally be dropped if maintaining backwards compatibility is cumbersome. Guides • Iteration: how it works • Job argument semantics • Best practices • Writing custom enumerator • Throttling For more detailed documentation, see rubydoc. Requirements ActiveJob is the primary requirement for Iteration. While there's nothing that prevents it, Iteration is not yet compatible with vanilla Sidekiq API. API Iteration job must respond to and methods. must return Enumerator object that respects the value. Sidekiq adapter Unless you are running on Heroku, we recommend you to tune Sidekiq's timeout option from the default 8 seconds to 25-30 seconds, to allow the last to complete and gracefully shutdown. Resque adapter There a few configuration assumptions that are required for Iteration to work with Resque. must be enabled (giving the job ability to gracefully interrupt), and is recommended to be disabled (set to ). FAQ **Why can't I just iterate in method and do whatever I want?** You can, but then your job has to comply with a long list of requirements, such as the ones above. This creates leaky abstractions more easily, when instead we can expose a more powerful abstraction for developers--without exposing the underlying infrastructure. **What happens when my job is interrupted?** A checkpoint will be persisted to Redis after the current , and the job will be re-enqueued. Once it's popped off the queue, the worker will work off from the next iteration. **What happens with retries?** An interruption of a job does not count as a retry. If an exception occurs, the job will retry or be discarded as normal using Active Job configuration for the job. If the job retries, it processes the iteration that originally failed and progress will continue from there on if successful. **What happens if my iteration takes a long time?** We recommend that a single should take no longer than 30 seconds. In the future, this may raise an exception. **Why is it important that takes less than 30 seconds?** When the job worker is scheduled for restart or shutdown, it gets a notice to finish remaining unit of work. To guarantee that no progress is lost we need to make sure that completes within a reasonable amount of time. **Why do I use have to use this ugly helper in ? Why can't you automatically infer it?** This is how the first version of the API worked. We checked the type of object returned by , and whether it was ActiveRecord Relation or an Array, we used the matching adapter. This caused opaque type branching in Iteration internals and it didn’t allow developers to craft their own Enumerators and control the cursor value. We made a decision to _always_ return Enumerator instance from . Now we provide explicit helpers to convert ActiveRecord Relation or an Array to Enumerator, and for more complex iteration flows developers can build their own objects. **What is the difference between Enumerable and Enumerator?** We recomend this post to learn more about Enumerators in Ruby. **My job has a complex flow. How do I write my own Enumerator?** Iteration API takes care of persisting the cursor (that you may use to calculate an offset) and controlling the job state. The power of Enumerator object is that you can use the cursor in any way you want. One example is a cursorless job that pops records from a datastore until the job is interrupted: Credits This project would not be possible without these individuals (in alphabetical order): • Daniella Niyonkuru • Emil Stolarsky • Florian Weingarten • Guillaume Malette • Hormoz Kheradmand • Mohamed-Adam Chaieb • Simon Eskildsen Develop…