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
- #parsed_opts ⇒ Object
- #rdf_subject ⇒ Object
-
#run ⇒ Object
Runs the block, setting the start and end time of the run.
- #set_end_time ⇒ Object
- #set_start_time ⇒ Object
Instance Attribute Details
#agent ⇒ String
Returns 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_time ⇒ DateTime
Returns a datestamp marking the activity’s competion.
22 |
# File 'app/models/krikri/activity.rb', line 22 validate :agent_must_be_a_software_agent |
#opts ⇒ JSON
Returns 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_time ⇒ DateTime
Returns a datestamp marking the activity’s start.
22 |
# File 'app/models/krikri/activity.rb', line 22 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.
56 57 58 |
# File 'app/models/krikri/activity.rb', line 56 def agent_instance @agent_instance ||= agent.constantize.new(parsed_opts) end |
#agent_must_be_a_software_agent ⇒ Object
24 25 26 27 |
# File 'app/models/krikri/activity.rb', line 24 def agent_must_be_a_software_agent errors.add(:agent, 'does not represent a SoftwareAgent') unless agent.constantize < Krikri::SoftwareAgent end |
#parsed_opts ⇒ Object
60 61 62 |
# File 'app/models/krikri/activity.rb', line 60 def parsed_opts JSON.parse(opts, symbolize_names: true) end |
#rdf_subject ⇒ Object
64 65 66 67 |
# File 'app/models/krikri/activity.rb', line 64 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.
43 44 45 46 47 48 49 |
# File 'app/models/krikri/activity.rb', line 43 def run if block_given? set_start_time yield agent_instance, rdf_subject set_end_time end end |
#set_end_time ⇒ Object
33 34 35 36 37 38 |
# File 'app/models/krikri/activity.rb', line 33 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
29 30 31 |
# File 'app/models/krikri/activity.rb', line 29 def set_start_time update_attribute(:start_time, DateTime.now.utc) end |