Skip to content

Instantly share code, notes, and snippets.

@rafamvc
Created April 8, 2011 20:43
Show Gist options
  • Save rafamvc/910698 to your computer and use it in GitHub Desktop.
Save rafamvc/910698 to your computer and use it in GitHub Desktop.

Revisions

  1. Rafael Cardoso revised this gist Apr 13, 2011. 1 changed file with 39 additions and 22 deletions.
    61 changes: 39 additions & 22 deletions test_obj.rb
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,21 @@

    class TestObj

    # All requests coming for #long_background_processing_method will be handled in the background
    run_async :method1

    handle_async :method1
    # This needs access to be a class method
    run :method2, :every => :day, :at => '12:55am'
    schedule :method2, :every => :day, :at => '12:55am'

    # The multiple will not allow two #method3 to run at the same time
    run :method3, :every => 30.minutes, :overlap => false
    schedule :method3, :every => 30.minutes, :overlap => false

    # The retroactive will process the summary from every 10 minutes since it ran last time.
    run :method4, :every => 10.minutes, :retroactive => true
    schedule :method4, :every => 10.minutes, :retroactive => true

    # Same as above, but it will run the the retroactive one after the other,
    # rather than all at the same time.
    run :method5, :every => 30.minutes,
    schedule :method5, :every => 30.minutes,
    :retroactive => true,
    :overlap => false

    @@ -27,35 +28,51 @@ class TestObj

    # This will use threads instead of forking.
    daemon :another_daemon, :style => :threaded


    def method1
    puts "Something"
    TestObj.mockable_method
    end
    def method2
    puts "Something"
    def method2(*args)
    TestObj.mockable_method
    end
    def method3
    puts "Something"
    TestObj.mockable_method
    end
    def method4
    puts "Something"
    TestObj.mockable_method
    end
    def method5
    puts "Something"
    TestObj.mockable_method
    end
    def my_awesome_daemon
    puts "Something"
    TestObj.mockable_method
    end
    def another_daemon
    puts "Something"
    end
    TestObj.mockable_method
    end
    def self.mockable_method
    end
    end


    ## You can also run:

    a = TestObj.new

    a.delay.method1
    a.delay(:in => 5.minutes).method1
    # ## You can also run:
    #
    # a = TestObj.new
    #
    # a.delay.method2
    # a.delay(:for => 5.minutes).method2
    #
    #
    # # using run_async
    #
    # a = TestObj.new
    # # Method 2 will not be executed in the current server, but it will push
    # # this request to the queue and will be processed in the background
    # a.method1
    #
    # # This will create 4 jobs in the queue
    # a.method1
    # a.method1
    # a.method1
    # a.method2
  2. Rafael Cardoso revised this gist Apr 10, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions test_obj.rb
    Original file line number Diff line number Diff line change
    @@ -23,10 +23,10 @@ class TestObj
    # The daemons support some signaling to stop and pause
    # The interval is the time between every execution of the method.
    # dameons are ran as a forked and detached process, while a run is only a forked process.
    run :as => :daemon, :my_awesome_daemon, :number_of_instances => 2, :interval => 5.seconds
    daemon :my_awesome_daemon, :number_of_instances => 2, :interval => 5.seconds

    # This will use threads instead of forking.
    run :as => :daemon, :another_daemon, :style => :threaded
    daemon :another_daemon, :style => :threaded


    def method1
  3. Rafael Cardoso revised this gist Apr 8, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions test_obj.rb
    Original file line number Diff line number Diff line change
    @@ -57,5 +57,5 @@ def another_daemon

    a = TestObj.new

    a.delay.slow_method
    a.delay(:in => 5.minutes).slow_method
    a.delay.method1
    a.delay(:in => 5.minutes).method1
  4. Rafael Cardoso revised this gist Apr 8, 2011. 1 changed file with 14 additions and 17 deletions.
    31 changes: 14 additions & 17 deletions test_obj.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    class TestObj
    attr_accessor :meaninful_variable

    # All requests coming for #long_background_processing_method will be handled in the background
    run_async :method1
    @@ -30,29 +29,27 @@ class TestObj
    run :as => :daemon, :another_daemon, :style => :threaded


    def initialize
    self.meaninful_variable = "something"
    def method1
    puts "Something"
    end

    def slow_method
    TestObj.mockable_method
    def method2
    puts "Something"
    end

    def slow_method_with_args(bool_arg)
    TestObj.mockable_method(bool_arg)
    def method3
    puts "Something"
    end

    def long_background_processing_method
    def method4
    puts "Something"
    end

    def self.long_consistency_check
    def method5
    puts "Something"
    end

    def self.mockable_method(bool_arg = true)
    end

    def my_awesome_daemon
    puts "Something"
    end
    def another_daemon
    puts "Something"
    end
    end


  5. Rafael Cardoso revised this gist Apr 8, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion test_obj.rb
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ class TestObj
    # rather than all at the same time.
    run :method5, :every => 30.minutes,
    :retroactive => true,
    :multiple => false
    :overlap => false

    # It will spawn the method my_awesome_daemon as a daemon.
    # As default, daemons are set not run overlap, but it can be override by setting the number_of_instances of the deamons to run.
  6. Rafael Cardoso revised this gist Apr 8, 2011. 1 changed file with 12 additions and 11 deletions.
    23 changes: 12 additions & 11 deletions test_obj.rb
    Original file line number Diff line number Diff line change
    @@ -2,31 +2,32 @@ class TestObj
    attr_accessor :meaninful_variable

    # All requests coming for #long_background_processing_method will be handled in the background
    run_async :long_background_processing_method
    run_async :method1

    # This needs access to be a class method
    run :long_consistency_check, :every => :day, :at => '12:55am'
    run :method2, :every => :day, :at => '12:55am'

    # The multiple will not allow two methods to run at the same time
    run :download_updated_tests, :every => 30.minutes, :multiple => false
    # The multiple will not allow two #method3 to run at the same time
    run :method3, :every => 30.minutes, :overlap => false

    # The retroactive will process the summary from every 10 minutes since it ran last time.
    run :calculate_summary_for_every_10_min, :every => 10.minutes, :retroactive => true
    run :method4, :every => 10.minutes, :retroactive => true

    # Same as above, but it will run the the retroactive one after the other,
    # rather than all at the same time.
    run :calculate_summary_of_summary_for_every_30_min, :every => 30.minutes,
    :retroactive => true,
    :multiple => false
    run :method5, :every => 30.minutes,
    :retroactive => true,
    :multiple => false

    # It will spawn the method my_awesome_daemon as a daemon.
    # As default, daemons are set not run multiples, but it can be override.
    # As default, daemons are set not run overlap, but it can be override by setting the number_of_instances of the deamons to run.
    # The daemons support some signaling to stop and pause
    # The interval is the time between every execution of the method.
    # dameons are ran as a forked and detached process, while a run is only a forked process.
    daemon :my_awesome_daemon, :multiple => true, :interval => 5.seconds
    run :as => :daemon, :my_awesome_daemon, :number_of_instances => 2, :interval => 5.seconds

    daemon :another_daemon, :style => (:forked || :threaded)
    # This will use threads instead of forking.
    run :as => :daemon, :another_daemon, :style => :threaded


    def initialize
  7. Rafael Cardoso revised this gist Apr 8, 2011. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions test_obj.rb
    Original file line number Diff line number Diff line change
    @@ -53,3 +53,11 @@ def self.mockable_method(bool_arg = true)
    def my_awesome_daemon
    end
    end


    ## You can also run:

    a = TestObj.new

    a.delay.slow_method
    a.delay(:in => 5.minutes).slow_method
  8. Rafael Cardoso revised this gist Apr 8, 2011. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions test_obj.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,9 @@
    class TestObj
    attr_accessor :meaninful_variable

    # All requests coming for #long_background_processing_method will be handled in the background
    run_async :long_background_processing_method

    # This needs access to be a class method
    run :long_consistency_check, :every => :day, :at => '12:55am'

  9. Rafael Cardoso renamed this gist Apr 8, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  10. Rafael Cardoso renamed this gist Apr 8, 2011. 1 changed file with 0 additions and 0 deletions.
  11. Rafael Cardoso renamed this gist Apr 8, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  12. Rafael Cardoso created this gist Apr 8, 2011.
    53 changes: 53 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    class TestObj
    attr_accessor :meaninful_variable

    run_async :long_background_processing_method
    # This needs access to be a class method
    run :long_consistency_check, :every => :day, :at => '12:55am'

    # The multiple will not allow two methods to run at the same time
    run :download_updated_tests, :every => 30.minutes, :multiple => false

    # The retroactive will process the summary from every 10 minutes since it ran last time.
    run :calculate_summary_for_every_10_min, :every => 10.minutes, :retroactive => true

    # Same as above, but it will run the the retroactive one after the other,
    # rather than all at the same time.
    run :calculate_summary_of_summary_for_every_30_min, :every => 30.minutes,
    :retroactive => true,
    :multiple => false

    # It will spawn the method my_awesome_daemon as a daemon.
    # As default, daemons are set not run multiples, but it can be override.
    # The daemons support some signaling to stop and pause
    # The interval is the time between every execution of the method.
    # dameons are ran as a forked and detached process, while a run is only a forked process.
    daemon :my_awesome_daemon, :multiple => true, :interval => 5.seconds

    daemon :another_daemon, :style => (:forked || :threaded)


    def initialize
    self.meaninful_variable = "something"
    end

    def slow_method
    TestObj.mockable_method
    end

    def slow_method_with_args(bool_arg)
    TestObj.mockable_method(bool_arg)
    end

    def long_background_processing_method
    end

    def self.long_consistency_check
    end

    def self.mockable_method(bool_arg = true)
    end

    def my_awesome_daemon
    end
    end