Q:

Using interrupt and continue signals in ruby

I have this program, that uploads photos to a database via an api. However, it takes quite a long time to process and upload all those images and I want to be able to stop and resume the process at random times. I thought the interrupt and continue signals might be good(temporary!) solution to this.
def upload
  photos.each do |photo| # photos is an array of all photos.
    api_client.upload_photos(photo.to_h)
  end
end

def to_h
 # Returns an hash of photos with an id and photo url
 # { id: 123, url: "http://www.example.com/img/1"
end

How do I go about this?

A:

You don't have to do anything in Ruby for sending SIGTSTP and SIGCONT signals.
To stop a running process just hit Control+Z:
$ ruby -e "loop { puts Time.now; sleep 1}"
2013-09-11 12:37:47 +0200
2013-09-11 12:37:48 +0200
2013-09-11 12:37:49 +0200
2013-09-11 12:37:50 +0200
2013-09-11 12:37:51 +0200
^Z
[1]  + 13764 suspended (signal)  ruby -e "loop { puts Time.now; sleep 1}"
$

To continue use the fg command:
$ fg
[1]  + 13764 continued  ruby -e "loop { puts Time.now; sleep 1}"
2013-09-11 12:38:28 +0200
2013-09-11 12:38:29 +0200
2013-09-11 12:38:30 +0200
2013-09-11 12:38:31 +0200

See http://en.wikipedia.org/wiki/Job_control_(Unix) for details.
UPDATE
To handle this signals in Ruby:
trap("TSTP") {
  # clean up
  Process.kill("STOP", Process.pid) # really stop this process
}
trap("CONT") {
  # handle restart
}

