Class: Krikri::Activity
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Krikri::Activity
- 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
-
#agent ⇒ String
A string representing the Krikri::SoftwareAgent responsible for the activity.
-
#end_time ⇒ DateTime
A datestamp marking the activity’s competion.
-
#opts ⇒ JSON
The options to pass to the #agent class when running the activity.
-
#start_time ⇒ DateTime
A datestamp marking the activity’s start.
Instance Method Summary collapse
-
#agent_instance ⇒ Agent
Instantiates and returns an instance of the Agent class with the values in opts.
- #agent_must_be_a_software_agent ⇒ Object
-
#ended? ⇒ Boolean
Indicates whether the activity has ended.
-
#entities ⇒ Enumerator
Return an Enumerator of entities (e.g. aggregations or original records) that have been affected by this activity.
-
#entity_uris ⇒ Enumerator
Return an Enumerator of URI strings of entities (e.g. aggregations or original records) that pertain to this activity.
- #parsed_opts ⇒ Object
-
#rdf_subject ⇒ RDF::URI
(also: #to_term)
The uri for this activity.
-
#run ⇒ Object
Runs the block, setting the start and end time of the run.
- #set_end_time ⇒ Object
- #set_start_time ⇒ Object
-
#to_s ⇒ String
A string reprerestation of the activity.
Instance Attribute Details
#agent ⇒ String
Returns a string representing the Krikri::SoftwareAgent responsible for the activity.
21 |
# File 'app/models/krikri/activity.rb', line 21 validate :agent_must_be_a_software_agent |
#end_time ⇒ DateTime
Returns a datestamp marking the activity’s competion.
21 |
# File 'app/models/krikri/activity.rb', line 21 validate :agent_must_be_a_software_agent |
#opts ⇒ JSON
Returns the options to pass to the #agent class when running the activity.
21 |
# File 'app/models/krikri/activity.rb', line 21 validate :agent_must_be_a_software_agent |
#start_time ⇒ DateTime
Returns a datestamp marking the activity’s start.
21 |
# File 'app/models/krikri/activity.rb', line 21 validate :agent_must_be_a_software_agent |
Instance Method Details
#agent_instance ⇒ Agent
Instantiates and returns an instance of the Agent class with the values in opts.
81 82 83 |
# File 'app/models/krikri/activity.rb', line 81 def agent_instance @agent_instance ||= agent.constantize.new(parsed_opts) end |
#agent_must_be_a_software_agent ⇒ Object
23 24 25 26 |
# File 'app/models/krikri/activity.rb', line 23 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.
72 73 74 |
# File 'app/models/krikri/activity.rb', line 72 def ended? !self.end_time.nil? end |
#entities ⇒ 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 that is represented by the Activity’s ‘agent’ field.
126 127 128 |
# File 'app/models/krikri/activity.rb', line 126 def entities agent_instance.entity_behavior.entities(self) end |
#entity_uris ⇒ Enumerator
Return an Enumerator of URI strings of entities (e.g. aggregations or original records) that pertain to this activity
109 110 111 112 113 114 115 |
# File 'app/models/krikri/activity.rb', line 109 def entity_uris activity_uri = RDF::URI(rdf_subject) # This activity's LDP URI query = Krikri::ProvenanceQueryClient.find_by_activity(activity_uri) query.each_solution.lazy.map do |s| s.record.to_s end end |
#parsed_opts ⇒ Object
85 86 87 |
# File 'app/models/krikri/activity.rb', line 85 def parsed_opts JSON.parse(opts, symbolize_names: true) end |
#rdf_subject ⇒ RDF::URI Also known as: to_term
Returns the uri for this activity.
91 92 93 94 |
# File 'app/models/krikri/activity.rb', line 91 def rdf_subject RDF::URI(Krikri::Settings['marmotta']['ldp']) / Krikri::Settings['prov']['activity'] / id.to_s end |
#run ⇒ Object
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.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/models/krikri/activity.rb', line 46 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.}\n#{e.backtrace}") raise e ensure set_end_time Krikri::Logger .log(:info, "Activity #{agent.constantize}-#{id} is done") end end end |
#set_end_time ⇒ Object
32 33 34 35 36 37 |
# File 'app/models/krikri/activity.rb', line 32 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_time ⇒ Object
28 29 30 |
# File 'app/models/krikri/activity.rb', line 28 def set_start_time update_attribute(:start_time, DateTime.now.utc) end |
#to_s ⇒ String
Returns a string reprerestation of the activity.
100 101 102 |
# File 'app/models/krikri/activity.rb', line 100 def to_s inspect.to_s end |