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 - [x] CI/CD Jenkins
- [ ] Suppress messages from server or handle them separately from selected worker - [ ] Suppress messages from server or handle them separately from selected worker
- [ ] Logs backend workers as threads (SQLite and co. are too slow) - [ ] 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 - [ ] Logs backend worker for redis/redis node
- [ ] Re-implement connection routine - [ ] Re-implement connection routine
- [ ] Availability to run scripts @ 'ChanelCommander' - [ ] Availability to run scripts @ 'ChanelCommander'

View file

@ -28,7 +28,11 @@ public class BotDriver {
case "SQLite": case "SQLite":
return new BotSQLiteWorker(serverName, serverDriver.get(serverName)[1], chanelName); return new BotSQLiteWorker(serverName, serverDriver.get(serverName)[1], chanelName);
case "MongoDB": 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": case "Zero":
return new BotZeroWorker(); return new BotZeroWorker();
default: default:

View file

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

View file

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