Class: BanterMessage

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
lib/banter/web/models/banter_message.rb

Constant Summary collapse

STATUS_STARTED =
'started'
STATUS_SUCCESS =
'success'
STATUS_ERROR =
'error'
STATUS_VALIDATION_FAILED =
'validation_failed'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.message_failed(error, status) ⇒ Object

Records the current message as errored or validation failed with error message. Clears the BanterMessage.current

Parameters:

  • error_message (String)
  • status (String)


75
76
77
78
79
80
81
82
83
# File 'lib/banter/web/models/banter_message.rb', line 75

def self.message_failed(error, status)
  current.status          = status
  current.done_at         = Time.now
  current.error_message   = error.message
  current.error_backtrace = error.backtrace
  current.save!
  BanterWorker.clear_current_message!(false)
  @@current = nil
end

.message_received(subscriber_class, worker, payload) ⇒ Object

Creates a BanterMessage to record a message that is received by a subscriber. It records the created message as BanterMessage.current

Parameters:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/banter/web/models/banter_message.rb', line 41

def self.message_received(subscriber_class, worker, payload)
  return unless BanterWorker.current

  obj = new(
    subscriber_class: subscriber_class.name,
    queue_name:       subscriber_class.subscribed_queue,
    subscribed_key:   subscriber_class.subscribed_key,
    payload_key:      worker.routing_key,
    # routing_data:     worker.delivery_routing_data.to_hash,
    # properties:       worker.delivery_properties.to_hash,
    payload:          payload,
    context:          worker.context,
    started_at:       Time.now,
    status:           STATUS_STARTED,
    worker_id:        BanterWorker.current.id
  )

  obj.save!
  @@current = obj
  BanterWorker.record_current!(obj)
end

.message_successfulObject

Records the current message as successful. Clears the BanterMessage.current



64
65
66
67
68
69
70
# File 'lib/banter/web/models/banter_message.rb', line 64

def self.message_successful
  current.status  = STATUS_SUCCESS
  current.done_at = Time.now
  current.save!
  BanterWorker.clear_current_message!(true)
  @@current = nil
end

.update_progress(object) ⇒ Object

Parameters:

  • object (Object)

    Any object that indicates the progress of the message



102
103
104
105
106
# File 'lib/banter/web/models/banter_message.rb', line 102

def self.update_progress(object)
  return unless current
  current.progress = object
  current.save!
end

.update_progress_at(num) ⇒ Object

Records the current progress as a number for the current message.

Parameters:

  • num (Integer)

    Current progress



87
88
89
90
91
# File 'lib/banter/web/models/banter_message.rb', line 87

def self.update_progress_at(num)
  return unless current
  current.progress_at = num
  current.save!
end

.update_progress_total(num) ⇒ Object

Records the number of objects expected to be processed for the current message.

Parameters:

  • num (Integer)

    Total number of objects expected to be processed.



95
96
97
98
99
# File 'lib/banter/web/models/banter_message.rb', line 95

def self.update_progress_total(num)
  return unless current
  current.progress_total = num
  current.save!
end

Instance Method Details

#executing?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/banter/web/models/banter_message.rb', line 108

def executing?
  self.status == STATUS_STARTED
end

#failed?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/banter/web/models/banter_message.rb', line 116

def failed?
  self.status == STATUS_ERROR || self.status == STATUS_VALIDATION_FAILED
end

#succeeded?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/banter/web/models/banter_message.rb', line 112

def succeeded?
  self.status == STATUS_SUCCESS
end

#workerBanterWorker

Returns The worker that is responsible for executing the current message.

Returns:

  • (BanterWorker)

    The worker that is responsible for executing the current message



121
122
123
# File 'lib/banter/web/models/banter_message.rb', line 121

def worker
  @worker ||= BanterWorker.find(self['worker_id'].to_s)
end