Sha256: 3e7b55bb326781c10070b1aad9bf947cc0ae9fae6ad2d49b98937f13e157eaa9

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

module SandthornSequelProjection
  class Runner

    DEFAULT_INTERVAL = 0.5

    attr_reader :manifest, :interval

    def initialize(manifest, interval = DEFAULT_INTERVAL)
      @manifest = manifest
      @interval = interval
    end

    def run(infinite = true)
      @projections = manifest.projections.map do |projection_class|
        projection_class.new(db_connection)
      end
      migrate!
      if infinite
        start_loop
      else
        loop_once
      end
    end

  private

    def start_loop
      while true
        loop_once
      end
    end

    def loop_once
      @projections.each do |projection|
        projection.update!
      end
      sleep(interval)
    end

    def db_connection
      SandthornSequelProjection.configuration.db_connection
    end

    def migrate!
      @projections.each(&:migrate!)
    end

  end

  class CircularQueue < Queue

    # = CircularQueue
    # Automatically pushes popped elements to the back of the queue
    # In other words, can never be emptied by use of pop
    def pop
      super.tap do |el|
        self << el
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sandthorn_sequel_projection-0.1.0 lib/sandthorn_sequel_projection/runner.rb
sandthorn_sequel_projection-0.0.4 lib/sandthorn_sequel_projection/runner.rb
sandthorn_sequel_projection-0.0.3 lib/sandthorn_sequel_projection/runner.rb
sandthorn_sequel_projection-0.0.2 lib/sandthorn_sequel_projection/runner.rb
sandthorn_sequel_projection-0.0.1 lib/sandthorn_sequel_projection/runner.rb