v0.4 more drafts

This commit is contained in:
Dmitry Isaenko 2019-01-18 05:34:08 +03:00
parent 434e7e3715
commit 7ed9d8615e
5 changed files with 58 additions and 38 deletions

View file

@ -32,7 +32,7 @@ Used libraries:
- [x] CI/CD Jenkins
- [ ] Suppress messages from server or handle them separately from selected worker
- [ ] Logs backend workers as threads (SQLite and co. are too slow)
- [ ] Logs backend worker for mongodb
- [x] Logs backend worker for mongodb
- [ ] Logs backend worker for redis/redis node
- [ ] Re-implement connection routine
- [ ] Availability to run scripts @ 'ChanelCommander'

View file

@ -28,7 +28,11 @@ public class BotDriver {
case "SQLite":
return new BotSQLiteWorker(serverName, serverDriver.get(serverName)[1], chanelName);
case "MongoDB":
return new BotMongoWorker(serverName, serverDriver.get(serverName)[1], chanelName);
BotMongoWorker botMongoWorker = new BotMongoWorker(serverName, serverDriver.get(serverName)[1], chanelName);
if (botMongoWorker.isConsistent())
return botMongoWorker;
else
System.out.println("BotDriver: Unable to use MongoWorker. Using ZeroWorker instead."); // else, fall down and use BotZeroWorker.
case "Zero":
return new BotZeroWorker();
default:

View file

@ -1,6 +1,5 @@
package InnaIrcBot.LogDriver;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoTimeoutException;
@ -24,7 +23,7 @@ public class BotMongoWorker implements Worker {
private String ircServer;
private MongoCollection<Document> collection;
private boolean consistent = true; // TODO: clarify possible issues???
private boolean consistent = false; // TODO: clarify possible issues???
public BotMongoWorker(String ircServer, String[] driverParameters, String channel){
if (channel.equals("system")) // Set ircServer variable only if it's 'system' log thread.
@ -34,44 +33,54 @@ public class BotMongoWorker implements Worker {
if (!serversMap.containsKey(ircServer)){
MongoClientSettings MCS = MongoClientSettings.builder().addCommandListener(new CommandListener() {
CommandListener mongoCommandListener = new CommandListener() {
@Override
public void commandStarted(CommandStartedEvent commandStartedEvent) {
System.out.println("commandStarted");
System.out.println("C: commandStarted");
}
@Override
public void commandSucceeded(CommandSucceededEvent commandSucceededEvent) {
System.out.println("commandSucceeded");
System.out.println("C: commandSucceeded");
}
@Override
public void commandFailed(CommandFailedEvent commandFailedEvent) {
System.out.println("commandFailed");
System.out.println("C: commandFailed");
consistent = false;
close(ircServer); // ircServer recieved by constructor, not this.ircServer
}
})
.applyConnectionString(new ConnectionString("mongodb://asasa:27017"))
.applyToServerSettings(builder -> builder.addServerListener(new ServerListener() {
};
ServerListener mongoServerListener = new ServerListener() {
@Override
public void serverOpening(ServerOpeningEvent serverOpeningEvent) {
System.out.println("Server Listener: 1 SRV ID: "+serverOpeningEvent.getServerId());
System.out.println("BotMongoWorker: ServerListener: Server opened successfully: "+serverOpeningEvent.getServerId());
}
@Override
public void serverClosed(ServerClosedEvent serverClosedEvent) {
System.out.println("Server Listener: Server has been closed");
System.out.println("BotMongoWorker: ServerListener: Server has been closed");
}
@Override
public void serverDescriptionChanged(ServerDescriptionChangedEvent serverDescriptionChangedEvent) {
System.out.println("Server Listener: Desc Changed: "+serverDescriptionChangedEvent.getNewDescription().getException());
close();
if (!serverDescriptionChangedEvent.getNewDescription().isOk()) {
consistent = false;
close(ircServer); // ircServer recieved by constructor, not this.ircServer
System.out.println("BotMongoWorker: ServerListener: Server description changed (exception occurs): "
+ serverDescriptionChangedEvent.getNewDescription().getException());
}
}))
}
};
MongoClientSettings MCS = MongoClientSettings.builder()
.addCommandListener(mongoCommandListener)
.applyConnectionString(new ConnectionString("mongodb://192.168.1.186:27017")) // TODO: replace with driverParameters[0] - address
.applyToServerSettings(builder -> builder.addServerListener(mongoServerListener))
.build();
MongoClient mongoClient = MongoClients.create(MCS);
//MongoClient mongoClient = MongoClients.create("mongodb://asasa:27017"); // TODO: replace with driverParameters[0] - address
serversMap.put(ircServer, mongoClient);
}
@ -81,14 +90,15 @@ public class BotMongoWorker implements Worker {
Document ping = new Document("ping", "1");
try {
collection.insertOne(ping);
consistent = true; // if no exceptions, then true
} catch (MongoTimeoutException e) {
System.out.println("Timeout exception");
System.out.println("BotMongoWorker: Timeout exception");
consistent = false;
close(ircServer); // ircServer recieved by constructor, not this.ircServer
} catch (IllegalStateException ise){
System.out.println("Illegal state exception: MongoDB server already closed.");
System.out.println("BotMongoWorker: Illegal state exception: MongoDB server already closed (not an issue).");
consistent = false;
}
}
@Override
@ -142,6 +152,12 @@ public class BotMongoWorker implements Worker {
serversMap.remove(ircServer);
System.out.println("BotMongoWorker->close(): " + ircServer);
}
}
public void close(String server) {
if (serversMap.containsKey(server)) {
serversMap.get(server).close();
serversMap.remove(server);
System.out.println("BotMongoWorker->close(): " + server + " (forced by listeners)");
}
}
}

View file

@ -197,7 +197,7 @@ public class SystemConsumer implements Runnable{
}
}
else
System.out.println("Some internal shit happens that shouldn't happens never ever. Take your cat, call scientists and wait for singularity. Panic allowed.");
System.out.println("Some internal shit happens that shouldn't happens never ever. Take your cat, call scientists and wait for singularity. Panic allowed. Log: \nEvent:|"+eventNum+"| sender:|"+sender+"| message|"+message+"|");
break;
case "NICK":
if (sender.startsWith(nick+"!")) {

View file

@ -14,13 +14,13 @@ public class DriverTest {
}
Worker fw1 = BotDriver.getWorker("irc.tomsk.net","system");
//Worker fw2 = BotDriver.getWorker("irc.tomsk.net","#main");
//Worker fw3 = BotDriver.getWorker("irc.tomsk.net","#lpr");
Worker fw2 = BotDriver.getWorker("irc.tomsk.net","#main");
Worker fw3 = BotDriver.getWorker("irc.tomsk.net","#lpr");
//if ((fw1 !=null) && (fw2 !=null) && (fw3 !=null)){
System.out.println("LogFile1: "+fw1.isConsistent());
//System.out.println("LogFile2: "+fw2.isConsistent());
//System.out.println("LogFile3: "+fw3.isConsistent());
System.out.println("LogFile2: "+fw2.isConsistent());
System.out.println("LogFile3: "+fw3.isConsistent());
/*
fw1.logAdd("JOIN", "de_su!loper@desktop.lan", "message1");
fw1.logAdd("PRIVMSG", "de_su!loper@desktop.lan", ": some text here");
@ -44,8 +44,8 @@ public class DriverTest {
fw3.logAdd("PART", "de_su!loper@desktop.lan", "#chan3");
*/
fw1.close();
//fw2.close();
//fw3.close();
fw2.close();
fw3.close();
//}
}
}