Class: Krikri::Activity

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/krikri/activity.rb

Overview

Activity wraps code execution with metadata about when it ran, which agents were responsible. It is designed to run a variety of different jobs, and its #run method is passed a block that performs the actual work. It records the start and end time of the job run, and provides the name of the agent to whomever needs it, but it does not care what kind of activity it is – harvest, enrichment, etc.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#agentString

Returns a string representing the Krikri::SoftwareAgent responsible for the activity.

Returns:

  • (String)

    a string representing the Krikri::SoftwareAgent responsible for the activity.



22
# File 'app/models/krikri/activity.rb', line 22

validate :agent_must_be_a_software_agent

#end_timeDateTime

Returns a datestamp marking the activity’s competion.

Returns:

  • (DateTime)

    a datestamp marking the activity’s competion



22
# File 'app/models/krikri/activity.rb', line 22

validate :agent_must_be_a_software_agent

#optsJSON

Returns the options to pass to the #agent class when running the activity.

Returns:

  • (JSON)

    the options to pass to the #agent class when running the activity



22
# File 'app/models/krikri/activity.rb', line 22

validate :agent_must_be_a_software_agent

#start_timeDateTime

Returns a datestamp marking the activity’s start.

Returns:

  • (DateTime)

    a datestamp marking the activity’s start



22
# File 'app/models/krikri/activity.rb', line 22

validate :agent_must_be_a_software_agent

Class Method Details

.base_uriRDF::URI

Returns the configured base URI for this class.

Examples:

building a valid URI from the base

Krikri::Activity.base_uri / 1

Returns:

  • (RDF::URI)

    the configured base URI for this class



29
30
31
32
# File 'app/models/krikri/activity.rb', line 29

def self.base_uri
  RDF::URI.intern(Krikri::Settings['marmotta']['ldp']) /
    Krikri::Settings['prov']['activity']
end

.from_uri(uri) ⇒ Krikri::Activity

Returns the activity with the given uri.

Parameters:

  • uri (#to_s)

    a uri for this activity

Returns:

Raises:

  • (RuntimeError)

    if the URI form does not match the activity

  • (ActiveRecord::RecordNotFound)

    if no activity is found



41
42
43
44
45
46
47
# File 'app/models/krikri/activity.rb', line 41

def self.from_uri(uri)
  raise "Cannot find #{self} from URI: #{uri}; " \
        "the requested uri does not match #{base_uri}" unless 
    uri.start_with? base_uri

  find(uri.to_s.sub(base_uri.to_s, '').sub('/', ''))
end

Instance Method Details

#agent_instanceAgent

Instantiates and returns an instance of the Agent class with the values in opts.

Returns:

  • (Agent)

    an instance of the class stored in Agent



107
108
109
# File 'app/models/krikri/activity.rb', line 107

def agent_instance
  @agent_instance ||= agent.constantize.new(parsed_opts)
end

#agent_must_be_a_software_agentObject



49
50
51
52
# File 'app/models/krikri/activity.rb', line 49

def agent_must_be_a_software_agent
  errors.add(:agent, 'does not represent a SoftwareAgent') unless
    agent.constantize < Krikri::SoftwareAgent
end

#ended?Boolean

Indicates whether the activity has ended. Does not distinguish between successful and failed completion states.

Returns:

  • (Boolean)

    ‘true` if the activity has been marked as ended, else `false`



98
99
100
# File 'app/models/krikri/activity.rb', line 98

def ended?
  !self.end_time.nil?
end

#entities(*args) ⇒ Enumerator

Return an Enumerator of entities (e.g. aggregations or original records) that have been affected by this activity.

The kind of object that is returned depends on the EntityBehavior class that is associated with the SoftwareAgent class that is represented by the Activity’s ‘agent’ field.

Parameters:

  • *args (Array<Object>)

    Arguments to pass along to EntityBehavior#entities

Returns:

  • (Enumerator)

    Objects



162
163
164
# File 'app/models/krikri/activity.rb', line 162

def entities(*args)
  agent.constantize.entity_behavior.entities(self, *args)
end

#entity_uris(include_invalidated = false) ⇒ Enumerator

Return an Enumerator of URI strings of entities (e.g. aggregations or original records) that pertain to this activity

Parameters:

  • include_invalidated (Boolean) (defaults to: false)

    Whether to include entities that have been invalidated with prov:invalidatedAtTime. Default: false

Returns:

  • (Enumerator)

    URI strings

See Also:

  • regarding invalidation.


142
143
144
145
146
147
148
149
# File 'app/models/krikri/activity.rb', line 142

def entity_uris(include_invalidated = false)
  activity_uri = RDF::URI(rdf_subject)  # This activity's LDP URI
  query = Krikri::ProvenanceQueryClient
    .find_by_activity(activity_uri, include_invalidated)
  query.each_solution.lazy.map do |s|
    s.record.to_s
  end
end

#parsed_optsHash

Returns the options parsed as JSON.

Returns:

  • (Hash)

    the options parsed as JSON



113
114
115
# File 'app/models/krikri/activity.rb', line 113

def parsed_opts
  JSON.parse(opts, symbolize_names: true)
end

#rdf_subjectRDF::URI Also known as: to_term

Returns the uri for this activity.

Returns:

  • (RDF::URI)

    the uri for this activity



119
120
121
# File 'app/models/krikri/activity.rb', line 119

def rdf_subject
  self.class.base_uri / id.to_s
end

#runObject

Runs the block, setting the start and end time of the run. The given block is passed an instance of the agent, and a URI representing this Activity.

Handles logging of activity start/stop and failure states.

Raises:

  • (RuntimeError)

    re-raises logged errors on Activity failure



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/krikri/activity.rb', line 72

def run
  if block_given?
    update_attribute(:end_time, nil) if ended?
    Krikri::Logger
      .log(:info, "Activity #{agent.constantize}-#{id} is running")
    set_start_time
    begin
      yield agent_instance, rdf_subject
    rescue => e
      Krikri::Logger.log(:error, "Error performing Activity: #{id}\n" \
                                 "#{e.message}\n#{e.backtrace}")
      raise e
    ensure
      set_end_time
      Krikri::Logger
        .log(:info, "Activity #{agent.constantize}-#{id} is done")
    end
  end
end

#set_end_timeObject



58
59
60
61
62
63
# File 'app/models/krikri/activity.rb', line 58

def set_end_time
  now = DateTime.now.utc
  fail 'Start time must exist and be before now to set an end time' unless
    self[:start_time] && (self[:start_time] <= now)
  update_attribute(:end_time, now)
end

#set_start_timeObject



54
55
56
# File 'app/models/krikri/activity.rb', line 54

def set_start_time
  update_attribute(:start_time, DateTime.now.utc)
end

#to_sString

Returns a string reprerestation of the activity.

Returns:

  • (String)

    a string reprerestation of the activity



126
127
128
# File 'app/models/krikri/activity.rb', line 126

def to_s
  inspect.to_s
end