Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7892153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:47:54+00:00 2026-06-03T06:47:54+00:00

Context: Many of the operations I’m doing require lengthy web accesses. Sometimes a web

  • 0

Context: Many of the operations I’m doing require lengthy web accesses. Sometimes a web access fails and the process needs to be restarted. And it’s a pain to restart the process from scratch.

So I’ve written a number of ad-hoc approaches to checkpointing: when you restart the process, it looks to see if checkpoint data is available and re-initializes state from that, otherwise it creates fresh state. In the course of operation, the process periodically writes checkpoint data somewhere (to a file or to the db). And when it’s finished, it cleans up the checkpoint data.

I’d like a simple, DRY, general-purpose checkpointing mechanism. How would you write it? Or is there a module that already does this? (Though it’s not an issue yet, extra stars awarded for thread-safe implementations!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:47:55+00:00Added an answer on June 3, 2026 at 6:47 am

    After mulling it over, I deciding that I’m willing to make this specific to ActiveRecord. By exploiting ruby’s ensure facility and the destroyed? and changed? methods in ActiveRecord, the design becomes simple:

    define Checkpoint model with :name and :state

    # file db/migrate/xyzzy_create_checkpoints.rb
    class CreateCheckpoints < ActiveRecord::Migration
      def change
        create_table :checkpoints do |t|
          t.string :name
          t.string :state
        end
        add_index :checkpoints, :name, :unique => true
      end 
    end
    
    # file app/models/checkpoint.rb
    class Checkpoint < ActiveRecord::Base
      serialize :state
    end
    

    define WithCheckpoint module

    # file lib/with_checkpoint.rb
    module WithCheckpoint
    
      def with_checkpoint(name, initial_state, &body)
        r = Checkpoint.where(:name => name)
        # fetch existing or create fresh checkpoint
        checkpoint = r.exists? ? r.first : r.new(:state => initial_state)
        begin
          yield(checkpoint)
        ensure
          # upon leaving the body, save the checkpoint iff needed
          checkpoint.save if (!(checkpoint.destroyed?) && checkpoint.changed?)
        end
      end
    end
    

    sample usage

    Here’s a somewhat contrived example that randomly blows up after some number of iterations. A more common case might be a lengthy network or file access that can fail at any point. Note: We store the state in an array only to show that ‘state’ needn’t be a simple integer.

    class TestCheck
      extend WithCheckpoint
    
      def self.do_it
        with_checkpoint(:fred, [0]) {|ckp|
          puts("intial state = #{ckp.state}")
          while (ckp.state[0] < 200) do
            raise RuntimeError if rand > 0.99
            ckp.state = [ckp.state[0]+1]
          end
          puts("completed normally, deleting checkpoint")
          ckp.delete
        }
      end
    
    end
    

    When you run TestCheck.do_it, it might randomly blow up after some number of iterations. But you can re-start it until it completes properly:

    >> TestCheck.do_it
    intial state = [0]
    RuntimeError: RuntimeError
            from sketches/checkpoint.rb:40:in `block in do_it'
            from sketches/checkpoint.rb:22:in `with_checkpoint'
            ...
    >> TestCheck.do_it
    intial state = [122]
    completed normally, deleting checkpoint
    => #<Checkpoint id: 3, name: "fred", state: [200]>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to see how many context switches each thread generates? (both
I have to use context in many places of my code such as database
Many common filesystems do not offer atomic operations, yet writing files in an atomic
Context: From my javascript web UI, I launch a long-running (several minutes) operation that
I am trying to build a class that does many photo operations, one method
Context: I have a photo-uploading website set up. I need to perform operations on
I am afraid, this would seem duplication of many questions on this same context,
This design question needs a bit of context, so please bear with me. I
We have a content delivery system that delivers many different content types to devices.
I'm trying to decide whether to create many classes for each content type I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.