Class: Cellect::Server::User

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Celluloid::Logger
Defined in:
lib/cellect/server/user.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, workflow_name: nil, ttl: nil) ⇒ User

Sets up a new user with an empty seen set, then loads the actual data



15
16
17
18
19
20
21
22
# File 'lib/cellect/server/user.rb', line 15

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

Instance Attribute Details

#idObject

Returns the value of attribute id.



11
12
13
# File 'lib/cellect/server/user.rb', line 11

def id
  @id
end

#seenObject

Returns the seen subjects set



35
36
37
# File 'lib/cellect/server/user.rb', line 35

def seen
  @seen
end

#stateObject

Returns the value of attribute state.



11
12
13
# File 'lib/cellect/server/user.rb', line 11

def state
  @state
end

#ttlObject

Returns the value of attribute ttl.



12
13
14
# File 'lib/cellect/server/user.rb', line 12

def ttl
  @ttl
end

#ttl_timerObject

Returns the value of attribute ttl_timer.



12
13
14
# File 'lib/cellect/server/user.rb', line 12

def ttl_timer
  @ttl_timer
end

#workflow_nameObject

Returns the value of attribute workflow_name.



11
12
13
# File 'lib/cellect/server/user.rb', line 11

def workflow_name
  @workflow_name
end

Instance Method Details

#cancel_ttl_timerObject

Releases the timer



47
48
49
50
# File 'lib/cellect/server/user.rb', line 47

def cancel_ttl_timer
  ttl_timer.cancel if ttl_timer
  self.ttl_timer = nil
end

#load_dataObject

Load the seen subjects for a user and restarts the TTL



25
26
27
28
29
30
31
32
# File 'lib/cellect/server/user.rb', line 25

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

#restart_ttl_timerObject

(Re)starts the inactivity countdown



41
42
43
44
# File 'lib/cellect/server/user.rb', line 41

def restart_ttl_timer
  self.ttl_timer ||= after(ttl){ ttl_expired! }
  ttl_timer.reset
end

#ttl_expired!Object

Removes the user from inactivity



53
54
55
56
57
# File 'lib/cellect/server/user.rb', line 53

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

#workflow_crashed(actor, reason) ⇒ Object

Handle errors and let the actor die



64
65
66
67
# File 'lib/cellect/server/user.rb', line 64

def workflow_crashed(actor, reason)
  cancel_ttl_timer
  terminate
end