Module: Taskinator::Persistence::InstanceMethods

Defined in:
lib/taskinator/persistence.rb

Instance Method Summary collapse

Instance Method Details

#errorObject

retrieves the error type, message and backtrace and returns an array with 3 subscripts respectively



137
138
139
140
141
142
143
144
# File 'lib/taskinator/persistence.rb', line 137

def error
  @error ||= Taskinator.redis do |conn|
    error_type, error_message, error_backtrace =
      conn.hmget(self.key, :error_type, :error_message, :error_backtrace)

    [error_type, error_message, JSON.parse(error_backtrace || '[]')]
  end
end

#fail(error = nil) ⇒ Object

persists the error information



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/taskinator/persistence.rb', line 121

def fail(error=nil)
  return unless error && error.is_a?(Exception)

  Taskinator.redis do |conn|
    conn.hmset(
      self.key,
      :error_type, error.class.name,
      :error_message, error.message,
      :error_backtrace, JSON.generate(error.backtrace || []),
      :updated_at, Time.now.utc
    )
  end
end

#keyObject

the persistence key



72
73
74
# File 'lib/taskinator/persistence.rb', line 72

def key
  @key ||= self.class.key_for(self.uuid)
end

#load_workflow_stateObject

retrieves the workflow state this method is called from the workflow gem



90
91
92
93
94
95
# File 'lib/taskinator/persistence.rb', line 90

def load_workflow_state
  state = Taskinator.redis do |conn|
    conn.hget(self.key, :state) || 'initial'
  end
  state.to_sym
end

#persist_workflow_state(new_state) ⇒ Object

persists the workflow state this method is called from the workflow gem



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/taskinator/persistence.rb', line 99

def persist_workflow_state(new_state)
  @updated_at = Time.now.utc
  Taskinator.redis do |conn|
    process_key = self.process_key
    conn.multi do
      conn.hmset(
        self.key,
        :state, new_state,
        :updated_at, @updated_at
      )

      # also update the "root" process
      conn.hset(
        process_key,
        :updated_at, @updated_at
      )
    end
  end
  new_state
end

#process_keyObject

the root process persistence key associated with this process or task



84
85
86
# File 'lib/taskinator/persistence.rb', line 84

def process_key
  @process_key ||= Taskinator::Process.key_for(process_uuid)
end

#process_optionsObject

retrieves the process options of the root process this is so that meta data of the process can be maintained and accessible to instrumentation subscribers



188
189
190
191
192
193
194
195
# File 'lib/taskinator/persistence.rb', line 188

def process_options
  @process_options ||= begin
    yaml = Taskinator.redis do |conn|
      conn.hget(self.process_key, :options)
    end
    yaml ? Taskinator::Persistence.deserialize(yaml) : {}
  end
end

#process_uuidObject

the root process uuid associated with this process or task



77
78
79
80
81
# File 'lib/taskinator/persistence.rb', line 77

def process_uuid
  @process_uuid ||= Taskinator.redis do |conn|
    conn.hget(self.key, :process_uuid)
  end
end

#saveObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/taskinator/persistence.rb', line 54

def save
  Taskinator.redis do |conn|
    conn.multi do
      visitor = RedisSerializationVisitor.new(conn, self).visit
      conn.hmset(
        Taskinator::Process.key_for(uuid),
        :tasks_count,      visitor.task_count,
        :tasks_failed,     0,
        :tasks_processing, 0,
        :tasks_completed,  0,
        :tasks_cancelled,  0,
      )
      true
    end
  end
end

#tasks_countObject



146
147
148
149
150
151
152
153
# File 'lib/taskinator/persistence.rb', line 146

def tasks_count
  @tasks_count ||= begin
    count = Taskinator.redis do |conn|
      conn.hget self.process_key, :tasks_count
    end
    count.to_i
  end
end