Sha256: 285165aea464b3617cce49bd9b8cae8898fe30ae68caab26371747d81e296191

Contents?: true

Size: 1.71 KB

Versions: 16

Compression:

Stored size: 1.71 KB

Contents

module Cellect
  module Server
    class User
      include Celluloid
      include Celluloid::Logger

      # Gracefully exit when the actor dies
      trap_exit :workflow_crashed
      finalizer :cancel_ttl_timer

      attr_accessor :id, :workflow_name, :seen, :state
      attr_accessor :ttl, :ttl_timer

      # Sets up a new user with an empty seen set, then loads the actual data
      def initialize(id, workflow_name: nil, ttl: nil)
        self.id = id
        self.workflow_name = workflow_name
        self.seen = DiffSet::RandomSet.new
        monitor Workflow[workflow_name]
        @ttl = ttl
        load_data
      end

      # Load the seen subjects for a user and restarts the TTL
      def load_data
        data = Cellect::Server.adapter.load_user(workflow_name, id) || []
        data.each do |subject_id|
          @seen.add subject_id
        end
        self.state = :ready
        restart_ttl_timer
      end

      # Returns the seen subjects set
      def seen
        restart_ttl_timer
        @seen
      end

      # (Re)starts the inactivity countdown
      def restart_ttl_timer
        self.ttl_timer ||= after(ttl){ ttl_expired! }
        ttl_timer.reset
      end

      # Releases the timer
      def cancel_ttl_timer
        ttl_timer.cancel if ttl_timer
        self.ttl_timer = nil
      end

      # Removes the user from inactivity
      def ttl_expired!
        debug "User #{ id } TTL expired"
        cancel_ttl_timer
        Workflow[workflow_name].async.remove_user(id)
      end

      def ttl
        @ttl || 60 * 15 # 15 minutes
      end

      # Handle errors and let the actor die
      def workflow_crashed(actor, reason)
        cancel_ttl_timer
        terminate
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
cellect-server-2.1.1 lib/cellect/server/user.rb
cellect-server-2.1.0 lib/cellect/server/user.rb
cellect-server-2.0.1 lib/cellect/server/user.rb
cellect-server-2.0.0 lib/cellect/server/user.rb
cellect-server-2.0.0.beta4 lib/cellect/server/user.rb
cellect-server-2.0.0.beta3 lib/cellect/server/user.rb
cellect-server-2.0.0.beta2 lib/cellect/server/user.rb
cellect-server-2.0.0.beta1 lib/cellect/server/user.rb
cellect-server-1.3.3 lib/cellect/server/user.rb
cellect-server-1.3.2 lib/cellect/server/user.rb
cellect-server-1.3.1 lib/cellect/server/user.rb
cellect-server-1.3.0 lib/cellect/server/user.rb
cellect-server-1.2.0 lib/cellect/server/user.rb
cellect-server-1.1.0 lib/cellect/server/user.rb
cellect-server-1.0.1 lib/cellect/server/user.rb
cellect-server-1.0.0 lib/cellect/server/user.rb