Class: Proxy::OpenBolt::Executor

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/smart_proxy_openbolt/executor.rb

Constant Summary collapse

SHUTDOWN_TIMEOUT =
30
MAX_CACHED_JOBS =
1000

Instance Method Summary collapse

Constructor Details

#initializeExecutor

Returns a new instance of Executor.



15
16
17
18
# File 'lib/smart_proxy_openbolt/executor.rb', line 15

def initialize
  @pool = Concurrent::FixedThreadPool.new(Plugin.settings.workers.to_i)
  @jobs = LruCache.new(MAX_CACHED_JOBS)
end

Instance Method Details

#add_job(job) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
# File 'lib/smart_proxy_openbolt/executor.rb', line 20

def add_job(job)
  raise ArgumentError, "Only Job instances can be added" unless job.is_a?(Job)
  id = SecureRandom.uuid
  job.id = id
  @jobs.put(id, job)
  @pool.post { job.process }
  id
end

#jobs_completedObject

Total number of jobs completed since proxy start



56
57
58
# File 'lib/smart_proxy_openbolt/executor.rb', line 56

def jobs_completed
  @pool.completed_task_count
end

#num_runningObject

How many workers are currently busy



46
47
48
# File 'lib/smart_proxy_openbolt/executor.rb', line 46

def num_running
  @pool.length
end

#queue_lengthObject

How many jobs are waiting in the queue



51
52
53
# File 'lib/smart_proxy_openbolt/executor.rb', line 51

def queue_length
  @pool.queue_length
end

#remove_job(id) ⇒ Object



41
42
43
# File 'lib/smart_proxy_openbolt/executor.rb', line 41

def remove_job(id)
  @jobs.delete(id)
end

#result(id) ⇒ Object



35
36
37
38
39
# File 'lib/smart_proxy_openbolt/executor.rb', line 35

def result(id)
  job = get_job(id)
  return :invalid unless job
  job.result
end

#running?Boolean

Still accepting and running jobs, or shutting down?

Returns:

  • (Boolean)


61
62
63
# File 'lib/smart_proxy_openbolt/executor.rb', line 61

def running?
  @pool.running?
end

#shutdown(timeout = SHUTDOWN_TIMEOUT) ⇒ Object

Stop accepting tasks and wait for in-flight jobs to finish. If timeout is nil, wait forever.



67
68
69
70
# File 'lib/smart_proxy_openbolt/executor.rb', line 67

def shutdown(timeout = SHUTDOWN_TIMEOUT)
  @pool.shutdown
  @pool.wait_for_termination(timeout)
end

#status(id) ⇒ Object



29
30
31
32
33
# File 'lib/smart_proxy_openbolt/executor.rb', line 29

def status(id)
  job = get_job(id)
  return :invalid unless job
  job.status
end