Module: Krikri::SoftwareAgent::ClassMethods

Defined in:
lib/krikri/software_agent.rb

Overview

Class methods for extension by ActiveSupport::Concern

Instance Method Summary collapse

Instance Method Details

#agent_nameObject

Returns a string representation of this SoftwareAgent class

Returns:

  • a string representation of this SoftwareAgent class


49
50
51
# File 'lib/krikri/software_agent.rb', line 49

def agent_name
  to_s
end

#enqueue(*args) ⇒ Boolean

Enqueue a job.

A worker process must be started to process jobs in the “harvest” queue, either before or after they are enqueued:

shell$ QUEUE=harvest bundle exec rake environment resque:work

This depends on Redis and Marmotta being available and properly configured (if necessary) in the Rails app.

Examples:

MyAgent.enqueue(:name => my_job)
Krikri::Harvesters::OAIHarvester.enqueue(
  :harvest,
  opts = {
    uri: 'http://vcoai.lib.harvard.edu/vcoai/vc',
    oai: { set: 'dag', metadata_prefix: 'mods' }
  }
)

Parameters:

  • queue_name (#to_s)

    the Resque queue name

  • opts (Hash)

    a hash of options that will be used to initialize the agent (an instance of this class).

Returns:

  • (Boolean)

See Also:


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/krikri/software_agent.rb', line 93

def enqueue(*args)
  queue = args.shift unless args.first.is_a? Hash
  queue ||= queue_name
  opts = args.shift || {}
  fail ArgumentError, "unexpected arguments #{args}" unless args.empty?
  fail ArgumentError, 'opts is not a hash' unless opts.is_a?(Hash)

  activity = Krikri::Activity.create do |a|
    a.agent = agent_name
    a.opts = JSON.generate(opts)
  end

  log :info, "created activity #{activity.id}"
  Resque.enqueue_to(queue, Krikri::Job, activity.id)
  log :info, "enqueued to #{queue}"
  true
end

#log(priority, msg) ⇒ Object

Log a message, tagged in a way suitable for software agents.

See Also:

  • method_missing

39
40
41
42
43
44
45
# File 'lib/krikri/software_agent.rb', line 39

def log(priority, msg)
  Krikri::SoftwareAgent::Logger.tagged(
    Time.now.to_s, Process.pid, to_s
  ) do
    Krikri::SoftwareAgent::Logger.send(priority, msg)
  end
end

#queue_nameObject

Returns the name of the default queue for jobs invoking this SoftwareAgent

Returns:

  • the name of the default queue for jobs invoking this SoftwareAgent


56
57
58
# File 'lib/krikri/software_agent.rb', line 56

def queue_name
  agent_name.downcase
end