fixes: min threads for async queue; format; spelling
This commit is contained in:
parent
0e2ab20a21
commit
c3e29a2c40
|
@ -18,19 +18,22 @@ public class AsyncHandler {
|
||||||
this(api, 2);
|
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) {
|
public AsyncHandler(TelegramAPI api, int threadCount) {
|
||||||
|
if (threadCount < 2) {
|
||||||
|
threadCount = 2;
|
||||||
|
}
|
||||||
asynchandlerthread = new AsyncHandlerThread[threadCount];
|
asynchandlerthread = new AsyncHandlerThread[threadCount];
|
||||||
|
|
||||||
asynchandlerthread[0] = new AsyncHandlerThread(fasttasks, THREADPREFIX + "Fast-0", api);
|
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);
|
asynchandlerthread[i] = new AsyncHandlerThread(slowtasks, THREADPREFIX + "Slow-" + i, api);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
for(AsyncHandlerThread t : asynchandlerthread) {
|
for (AsyncHandlerThread t : asynchandlerthread) {
|
||||||
t.stop();
|
t.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,7 +45,7 @@ public class AsyncHandler {
|
||||||
public void enque(Task t, boolean isSlowMethod, boolean priority) {
|
public void enque(Task t, boolean isSlowMethod, boolean priority) {
|
||||||
List<Task> tasks = isSlowMethod ? slowtasks : fasttasks;
|
List<Task> tasks = isSlowMethod ? slowtasks : fasttasks;
|
||||||
|
|
||||||
if(priority) {
|
if (priority) {
|
||||||
synchronized (tasks) {
|
synchronized (tasks) {
|
||||||
tasks.add(0, t);
|
tasks.add(0, t);
|
||||||
tasks.notifyAll();
|
tasks.notifyAll();
|
||||||
|
@ -85,7 +88,9 @@ public class AsyncHandler {
|
||||||
|
|
||||||
public static abstract class Callback<T extends Object, K extends Object> {
|
public static abstract class Callback<T extends Object, K extends Object> {
|
||||||
public Callback<K, ?> next = null;
|
public Callback<K, ?> next = null;
|
||||||
|
|
||||||
public abstract K call(T j) throws Throwable;
|
public abstract K call(T j) throws Throwable;
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public K callObj(Object j) throws Throwable {
|
public K callObj(Object j) throws Throwable {
|
||||||
return call((T) j);
|
return call((T) j);
|
||||||
|
|
|
@ -190,10 +190,10 @@ public class TelegramAPI implements Runnable {
|
||||||
int idx = errdesc.lastIndexOf(" ");
|
int idx = errdesc.lastIndexOf(" ");
|
||||||
try {
|
try {
|
||||||
timeout = Integer.parseInt(errdesc.substring(idx))+1;
|
timeout = Integer.parseInt(errdesc.substring(idx))+1;
|
||||||
System.out.println("timeout read: " + timeout);
|
log.log("timeout read: " + timeout);
|
||||||
} catch(NumberFormatException | StringIndexOutOfBoundsException e ) {}
|
} catch(NumberFormatException | StringIndexOutOfBoundsException e ) {}
|
||||||
try {
|
try {
|
||||||
System.out.println("Got 429 -> sleep for " + timeout + " seconds");
|
log.log("Got 429 -> sleep for " + timeout + " seconds");
|
||||||
Thread.sleep(timeout * 1000);
|
Thread.sleep(timeout * 1000);
|
||||||
} catch(InterruptedException e) {}
|
} catch(InterruptedException e) {}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ public class FeedbackCommand implements JSONCommandHandler {
|
||||||
|
|
||||||
protected final TInlineKeyboardMarkup cancelMarkup = TInlineKeyboardMarkup.makeSingleButton("❌ cancel", CANCELFEEDBACK);
|
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, 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 TelegramAPI api;
|
||||||
protected final List<Long> admins;
|
protected final List<Long> admins;
|
||||||
protected final static String CANCELFEEDBACK = "cancelfeedback", REPLYFEEDBACK = "replyFeedback ";
|
protected final static String CANCELFEEDBACK = "cancelfeedback", REPLYFEEDBACK = "replyFeedback ";
|
||||||
|
@ -41,7 +41,7 @@ public class FeedbackCommand implements JSONCommandHandler {
|
||||||
removeMsg(sender.getID(), null);
|
removeMsg(sender.getID(), null);
|
||||||
awaitingFeedback.put(sender.getID(), null);
|
awaitingFeedback.put(sender.getID(), null);
|
||||||
MessageBuilder mb = new MessageBuilder();
|
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>() {
|
.setCallback(new AsyncHandler.Callback<TMessage, Object>() {
|
||||||
@Override
|
@Override
|
||||||
public Object call(TMessage msg) {
|
public Object call(TMessage msg) {
|
||||||
|
@ -71,10 +71,10 @@ public class FeedbackCommand implements JSONCommandHandler {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} else { //reply to a message
|
} else { //reply to a message
|
||||||
Long[] rplymsg = awnser.remove(user.getID());
|
Long[] rplymsg = answer.remove(user.getID());
|
||||||
if(rplymsg != null) {
|
if(rplymsg != null) {
|
||||||
if(admins.contains(user.getID())) {
|
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
|
//admin to user -> copy text send as new msg from bot to disguise admin
|
||||||
TInlineKeyboardMarkup mu = new TInlineKeyboardMarkup(1);
|
TInlineKeyboardMarkup mu = new TInlineKeyboardMarkup(1);
|
||||||
mu.addCallbackButton("↩ reply", buildRplyCallback(user.getID(), msg.getMessageID()), 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);
|
.setReplyTo(Math.toIntExact(rplymsg[1])).setText(msg.getText()).setMarkup(mu);
|
||||||
api.sendMessage(mb.build());
|
api.sendMessage(mb.build());
|
||||||
} else {
|
} else {
|
||||||
System.out.println("user awnsered feedback!");
|
System.out.println("user answered feedback!");
|
||||||
//user to admin -> fwd message
|
//user to admin -> fwd message
|
||||||
TMessage.forwardAsync(api, rplymsg[0], msg);
|
TMessage.forwardAsync(api, rplymsg[0], msg);
|
||||||
|
|
||||||
|
@ -142,7 +142,7 @@ public class FeedbackCommand implements JSONCommandHandler {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
if(msgid > -1) {
|
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);
|
api.answerCallbackQuery(e.getID(), "Write a Message", true);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Reference in New Issue