fixes: min threads for async queue; format; spelling

master
Oliver 3 months ago
parent 0e2ab20a21
commit c3e29a2c40
Signed by: okaestne
GPG Key ID: 06A81B143EA9588F
  1. 15
      src/main/java/de/mrbesen/telegram/AsyncHandler.java
  2. 4
      src/main/java/de/mrbesen/telegram/TelegramAPI.java
  3. 12
      src/main/java/de/mrbesen/telegram/commands/FeedbackCommand.java

@ -9,7 +9,7 @@ public class AsyncHandler {
private List<Task> fasttasks = new LinkedList<>();
private List<Task> slowtasks = new LinkedList<>();
private AsyncHandlerThread[] asynchandlerthread;
private static final String THREADPREFIX = "AsyncTgHandler-";
@ -18,19 +18,22 @@ public class AsyncHandler {
this(api, 2);
}
//allow as many as threadCount threads to handle Async Tasks
// allow as many as threadCount threads to handle Async Tasks
public AsyncHandler(TelegramAPI api, int threadCount) {
if (threadCount < 2) {
threadCount = 2;
}
asynchandlerthread = new AsyncHandlerThread[threadCount];
asynchandlerthread[0] = new AsyncHandlerThread(fasttasks, THREADPREFIX + "Fast-0", api);
for(int i = 1; i < threadCount; ++i) {
for (int i = 1; i < threadCount; ++i) {
asynchandlerthread[i] = new AsyncHandlerThread(slowtasks, THREADPREFIX + "Slow-" + i, api);
}
}
public void stop() {
for(AsyncHandlerThread t : asynchandlerthread) {
for (AsyncHandlerThread t : asynchandlerthread) {
t.stop();
}
}
@ -42,7 +45,7 @@ public class AsyncHandler {
public void enque(Task t, boolean isSlowMethod, boolean priority) {
List<Task> tasks = isSlowMethod ? slowtasks : fasttasks;
if(priority) {
if (priority) {
synchronized (tasks) {
tasks.add(0, t);
tasks.notifyAll();
@ -85,7 +88,9 @@ public class AsyncHandler {
public static abstract class Callback<T extends Object, K extends Object> {
public Callback<K, ?> next = null;
public abstract K call(T j) throws Throwable;
@SuppressWarnings("unchecked")
public K callObj(Object j) throws Throwable {
return call((T) j);

@ -190,10 +190,10 @@ public class TelegramAPI implements Runnable {
int idx = errdesc.lastIndexOf(" ");
try {
timeout = Integer.parseInt(errdesc.substring(idx))+1;
System.out.println("timeout read: " + timeout);
log.log("timeout read: " + timeout);
} catch(NumberFormatException | StringIndexOutOfBoundsException e ) {}
try {
System.out.println("Got 429 -> sleep for " + timeout + " seconds");
log.log("Got 429 -> sleep for " + timeout + " seconds");
Thread.sleep(timeout * 1000);
} catch(InterruptedException e) {}

@ -20,7 +20,7 @@ public class FeedbackCommand implements JSONCommandHandler {
protected final TInlineKeyboardMarkup cancelMarkup = TInlineKeyboardMarkup.makeSingleButton("❌ cancel", CANCELFEEDBACK);
protected final Map<Long, TMessage> awaitingFeedback = new TreeMap<>(); //maps users chat id -> message that said "please send feedback" TODO: make persistent?
protected final Map<Long, Long[]> awnser = new TreeMap<>(); // chatid -> [chatid, messageid] to awnser to; next message from key will be fwd to value[0] and reply to value[1]
protected final Map<Long, Long[]> answer = new TreeMap<>(); // chatid -> [chatid, messageid] to answer to; next message from key will be fwd to value[0] and reply to value[1]
protected final TelegramAPI api;
protected final List<Long> admins;
protected final static String CANCELFEEDBACK = "cancelfeedback", REPLYFEEDBACK = "replyFeedback ";
@ -41,7 +41,7 @@ public class FeedbackCommand implements JSONCommandHandler {
removeMsg(sender.getID(), null);
awaitingFeedback.put(sender.getID(), null);
MessageBuilder mb = new MessageBuilder();
mb.setAsync().setReciver(sender).setMarkup(cancelMarkup).setText("Your next Message will be forwarded to the admins.")
mb.setAsync().setReciver(sender).setMarkup(cancelMarkup).setText("Your next message will be forwarded to the admins.")
.setCallback(new AsyncHandler.Callback<TMessage, Object>() {
@Override
public Object call(TMessage msg) {
@ -71,10 +71,10 @@ public class FeedbackCommand implements JSONCommandHandler {
}
return true;
} else { //reply to a message
Long[] rplymsg = awnser.remove(user.getID());
Long[] rplymsg = answer.remove(user.getID());
if(rplymsg != null) {
if(admins.contains(user.getID())) {
System.out.println("admin awnsered feedback!");
System.out.println("admin answered feedback!");
//admin to user -> copy text send as new msg from bot to disguise admin
TInlineKeyboardMarkup mu = new TInlineKeyboardMarkup(1);
mu.addCallbackButton("↩ reply", buildRplyCallback(user.getID(), msg.getMessageID()), 1);
@ -83,7 +83,7 @@ public class FeedbackCommand implements JSONCommandHandler {
.setReplyTo(Math.toIntExact(rplymsg[1])).setText(msg.getText()).setMarkup(mu);
api.sendMessage(mb.build());
} else {
System.out.println("user awnsered feedback!");
System.out.println("user answered feedback!");
//user to admin -> fwd message
TMessage.forwardAsync(api, rplymsg[0], msg);
@ -142,7 +142,7 @@ public class FeedbackCommand implements JSONCommandHandler {
ex.printStackTrace();
}
if(msgid > -1) {
awnser.put(e.getUser().getID(), new Long[]{chatid, msgid});
answer.put(e.getUser().getID(), new Long[]{chatid, msgid});
api.answerCallbackQuery(e.getID(), "Write a Message", true);
}
return true;

Loading…
Cancel
Save