Skip to content

feat: add processing statement state #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ public class Statement {
private final Output output;
private final String state;
private final LocalDateTime createdAt;
private final LocalDateTime finishedAt;

public Statement(@Nullable String id, String code, @Nullable Output output, @Nullable String state, LocalDateTime createdAt) {
public Statement(@Nullable String id, String code, @Nullable Output output, @Nullable String state, LocalDateTime createdAt, @Nullable LocalDateTime finishedAt) {
this.id = id;
this.code = code;
this.output = output;
this.state = state;
this.createdAt = createdAt;
this.finishedAt = finishedAt;
}

public String getId() {
Expand All @@ -45,6 +47,10 @@ public LocalDateTime getCreatedAt() {
return createdAt;
}

public LocalDateTime getFinishedAt() {
return finishedAt;
}

@Override
public String toString() {
return new StringJoiner(", ", Statement.class.getSimpleName() + "[", "]")
Expand All @@ -53,6 +59,7 @@ public String toString() {
.add("output=" + output)
.add("state='" + state + "'")
.add("createdAt='" + createdAt + "'")
.add("finishedAt='" + finishedAt + "'")
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.micronaut.scheduling.annotation.Async;
import jakarta.inject.Singleton;
import java.net.InetSocketAddress;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -37,6 +38,8 @@ public List<Statement> statementsToProcess(String id) {
var statementQueue = statementStorage.findByState(id, "waiting");
if (!statementQueue.isEmpty()) {
LOG.info("Waiting: {}", statementQueue);
statementQueue = statementQueue.subList(0, 1);
statementQueue.forEach(st -> statementStorage.updateState(id, st.getId(), "processing"));
}
return statementQueue;
}
Expand All @@ -54,7 +57,8 @@ public void handleResponse(String sessionId, String statementId, Map<String, Obj
string(result.get("traceback"))
);
var status = error != null ? "error" : "available";
statementStorage.update(sessionId, statementId, status, output);
var finishedDate = LocalDateTime.parse(string(result.get("finished_at")));
statementStorage.update(sessionId, statementId, status, output, finishedDate);
}

private String string(Object obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.exacaster.lighter.application.sessions.Statement;
import com.exacaster.lighter.application.sessions.processors.Output;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

Expand All @@ -15,7 +17,7 @@ public interface StatementStorage {

Optional<Statement> findLatest(String sessionId);

void update(String sessionId, String statementId, String state, Output output);
void update(String sessionId, String statementId, String state, Output output, LocalDateTime finishedDate);

Statement create(String sessionId, Statement statement);
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ public Optional<Statement> updateState(String sessionId, String statementId, Str

@Override
@Transactional
public void update(String sessionId, String statementId, String state, Output output) {
jdbi.withHandle(handle -> handle.createUpdate("UPDATE application_statement SET state=:state, output=:output WHERE application_id=:application_id AND id=:id")
public void update(String sessionId, String statementId, String state, Output output, LocalDateTime finishedAt) {
jdbi.withHandle(handle -> handle.createUpdate("UPDATE application_statement SET state=:state, output=:output, finished_at=:finished_at WHERE application_id=:application_id AND id=:id")
.bind("application_id", sessionId)
.bind("id", statementId)
.bind("state", state)
.bind("output", outputToString(output))
.bind("finished_at", finishedAt != null ? Timestamp.valueOf(finishedAt) : null)
.execute());
}

Expand Down Expand Up @@ -133,7 +134,8 @@ public Statement map(ResultSet rs, StatementContext ctx) throws SQLException {
rs.getString("code"),
output,
rs.getString("state"),
rs.getTimestamp("created_at").toLocalDateTime()
rs.getTimestamp("created_at").toLocalDateTime(),
rs.getTimestamp("finished_at") != null ? rs.getTimestamp("finished_at").toLocalDateTime() : null
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table application_statement add column finished_at timestamp;
2 changes: 2 additions & 0 deletions server/src/main/resources/shell_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import zipfile
import requests
import shutil
from datetime import datetime
from typing import Callable, Any, List, Dict
from pathlib import Path
from time import sleep
Expand Down Expand Up @@ -238,6 +239,7 @@ def main() -> int:
setup_output()
log.debug(f"Processing command {command}")
result = handler.exec(command)
result["finished_at"] = datetime.now().isoformat()
controller.write(command["id"], result)
log.debug("Response sent")
sleep(0.25)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Factories {
}

static statement() {
new Statement("id", "code", new Output("error", 1, [:], "evalue", "traceback"), "ok", null)
new Statement("id", "code", new Output("error", 1, [:], "evalue", "traceback"), "ok", null, null)
}

static kubernetesProperties() {
Expand All @@ -99,6 +99,6 @@ class Factories {
}

static newStatement() {
new Statement(null, "code", null, null, LocalDateTime.now())
new Statement(null, "code", null, null, LocalDateTime.now(), null)
}
}