Skip to content

Commit

Permalink
Merge pull request #49 from exacaster/fix/no_infinite_retry
Browse files Browse the repository at this point in the history
Do not retry failed session connection forever
  • Loading branch information
pdambrauskas authored Jun 15, 2022
2 parents 2412b9d + 97c072f commit e37ba42
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
42 changes: 28 additions & 14 deletions server/src/main/resources/shell_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ def setup_output():
sys.stdout = io.StringIO()
sys.stderr = io.StringIO()

def _do_with_retry(attempts, action):
attempts_left = attempts
last_exception = None
while attempts_left:
try:
return action()
except Exception as e:
last_exception = e
attempts_left -= 1
raise last_exception


class Controller:
def __init__(self, session_id):
Expand All @@ -33,16 +44,18 @@ def write(self, _id, _result):

class TestController(Controller):
def read(self):
try:
def do_read():
str_line = sys_stdin.readline()
line = json.loads(str_line)
return [line]
except:
return []
return _do_with_retry(2, do_read)


def write(self, id, result):
print(json.dumps(result), file=sys_stdout)
sys_stdout.flush()
def do_write():
print(json.dumps(result), file=sys_stdout)
sys_stdout.flush()
_do_with_retry(2, do_write)


class GatewayController(Controller):
Expand All @@ -56,17 +69,18 @@ def __init__(self, session_id):
self.endpoint = self.gateway.entry_point

def read(self):
try:
return [{"id": stmt.getId(), "code": stmt.getCode()} for stmt in self.endpoint.statementsToProcess(self.session_id)]
except Exception as e:
log.exception(e)
return []
return _do_with_retry(
3,
lambda: [
{"id": stmt.getId(), "code": stmt.getCode()} for stmt in self.endpoint.statementsToProcess(self.session_id)
]
)

def write(self, id, result):
try:
self.endpoint.handleResponse(self.session_id, id, result)
except Exception as e:
log.exception(e)
_do_with_retry(
3,
lambda: self.endpoint.handleResponse(self.session_id, id, result)
)


class CommandHandler:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.exacaster.lighter.application.sessions.processors


import spock.lang.Ignore
import spock.lang.Specification

Expand All @@ -12,12 +11,13 @@ class PythonProcessorTest extends Specification {
def processor

def setup() {
def builder = new ProcessBuilder("python3", "/home/paulius/projects/lighter/server/src/main/resources/shell_wrapper.py")
def builder = new ProcessBuilder("python3", "./src/main/resources/shell_wrapper.py")
builder.redirectErrorStream(true)
def env = builder.environment()
env.put("PYTHONINSPECT", "true")
env.put("PYTHONUNBUFFERED", "true")
env.put("LIGHTER_TEST", "true")
env.put("LIGHTER_SESSION_ID", "session1")
process = builder.start()
processor = new PythonProcessor(process)
}
Expand Down

0 comments on commit e37ba42

Please sign in to comment.