Class: Gemlings::Sandbox
- Inherits:
-
Object
- Object
- Gemlings::Sandbox
- Defined in:
- lib/gemlings/sandbox.rb
Defined Under Namespace
Classes: BoxExecutor, Executor, ForkExecutor, ThreadExecutor
Constant Summary collapse
- DEFAULT_TIMEOUT =
seconds
30- EXECUTORS =
Executor strategy — user-selectable, with sensible per-platform defaults.
{}
Instance Attribute Summary collapse
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Class Method Summary collapse
Instance Method Summary collapse
- #execute(code) ⇒ Object
-
#initialize(tools:, timeout: DEFAULT_TIMEOUT, executor: nil) ⇒ Sandbox
constructor
A new instance of Sandbox.
Constructor Details
#initialize(tools:, timeout: DEFAULT_TIMEOUT, executor: nil) ⇒ Sandbox
Returns a new instance of Sandbox.
135 136 137 138 139 140 |
# File 'lib/gemlings/sandbox.rb', line 135 def initialize(tools:, timeout: DEFAULT_TIMEOUT, executor: nil) @tools = tools @timeout = timeout @executor = executor || self.class.resolve_executor(self.class.default_executor) @tool_map = tools.each_with_object({}) { |t, h| h[t.class.tool_name] = t } end |
Instance Attribute Details
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
133 134 135 |
# File 'lib/gemlings/sandbox.rb', line 133 def timeout @timeout end |
Class Method Details
.default_executor ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/gemlings/sandbox.rb', line 114 def self.default_executor if BoxExecutor.available? :box elsif ForkExecutor.available? :fork else :thread end end |
.resolve_executor(name) ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/gemlings/sandbox.rb', line 124 def self.resolve_executor(name) klass = EXECUTORS[name] raise ArgumentError, "Unknown executor: #{name.inspect}. Available: #{EXECUTORS.keys.join(", ")}" unless klass unless klass.available? raise Error, "Executor #{name.inspect} is not available on this platform (#{RUBY_ENGINE} #{RUBY_VERSION})" end klass.new end |
Instance Method Details
#execute(code) ⇒ Object
142 143 144 |
# File 'lib/gemlings/sandbox.rb', line 142 def execute(code) @executor.call(@timeout) { run_in_child(code) } end |