Guest User

Untitled

a guest
Jun 25th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. File.open("/tmp/indexer_lock.tmp",'w') do |f|
  2. exit unless f.flock(File::LOCK_EX)
  3. end
  4.  
  5. require 'rubygems'
  6. require 'daemons'
  7.  
  8. Daemons.run('indexer.rb')
  9.  
  10. loop do
  11. # code executing your indexing
  12.  
  13. sleep INDEXING_INTERVAL
  14. end
  15.  
  16. File.new("/tmp/foo.lock").flock( File::LOCK_NB | File::LOCK_EX )
  17.  
  18. #!/usr/local/bin/bash
  19. #
  20.  
  21. if ps -C $PROGRAM_NAME &> /dev/null ; then
  22. : #Program is already running.. appropriate action can be performed here (kill it?)
  23. else
  24. #Program is not running.. launch it.
  25. $PROGRAM_NAME
  26. fi
  27.  
  28. tmp_file = File.expand_path(File.dirname(__FILE__)) + "/indexer.lock"
  29. if File.exists?(tmp_file)
  30. puts "quitting"
  31. exit
  32. else
  33. `touch #{tmp_file}`
  34. end
  35.  
  36. .. do stuff ..
  37.  
  38. File.delete(tmp_file)
  39.  
  40. exit unless DATA.flock(File::LOCK_NB | File::LOCK_EX)
  41.  
  42. # your script here
  43.  
  44. __END__
  45. DO NOT REMOTE: required for the DATA object above.
  46.  
  47. def the_method_my_cron_job_calls
  48. # something really expensive
  49. end
  50. lock_method :the_method_my_cron_job_calls
  51.  
  52. LockMethod.config.storage = Redis.new([...]) # a remote RedisToGo instance, perhaps?
  53.  
  54. def the_method_my_cron_job_calls
  55. # something really expensive
  56. end
  57. lock_method :the_method_my_cron_job_calls, (60*60) # automatically expire lock after an hour
Add Comment
Please, Sign In to add comment