views:

66

answers:

1

In my app a user can set the status of a Post to different flags like 'v' - visible, 'd' - mark for delete etc.

These flags are set via controller actions.

I have a batch process that runs and cleans all the posts marked for delete.

Post.find(:all, :conditions => ['status = ?', 'd']).each do |p| p.destroy end

This batch process runs every x many minutes.

Let's say a user marks the post with 'd' => batch process runs at some points => while the process is running the user marks the post as 'v'. Now inside the batch process the record is already targeted for delete and will be when the do loop is done, but the flag has changed via the controller action.

Ideally, if this happens I would like to not delete that post in the batch process.

What's the best way to handle this?

+1  A: 

The way you described it, there is a race condition. -- The user can mark the post v when it is already in the process of being deleted. And the user's request will be ignored.

Lots of ways to change this depending on your goals:

1) Change from

Post.find(:all, :conditions => ['status = ?', 'd']).each do |p| p.destroy end
to
Post.find(:all, :conditions => ['status = ?', 'd']).each do |p|
   p.reload
   p.destroy if p.status == 'd' 
end

This will minimize the window of time between the status being checked and the post deleted.

2) Change the display of the posts to only show the posts if the status is not "d." This will effectively speedup the deletion since others will have much less time to mark a to-be-deleted post as "v"

3) To give people a longer period to change their minds, only run the batch job once a day, late at night. Once a post is marked as d (to-be-deleted), show the post in a special way to warn users that the post is about to be deleted. This gives them max time to undo the delete request.

4) Never actually destroy the data. (DBMS storage is cheap.) When a post is marked for deletion, just change a flag and don't show it again. This enables users to "undelete" the post whenever they want to--no deadlines to worry about, nor any race conditions.

Larry K