@Override
protected Void doExecute(PartakeConnection con, IPartakeDAOs daos) throws DAOException, PartakeException {
DataIterator<MessageEnvelope> it = daos.getMessageEnvelopeAccess().getIterator(con);
try {
while (it.hasNext()) {
MessageEnvelope envelope = it.next();
if (envelope == null) {
it.remove();
continue;
}
// InvalidAfter 後であれば、message を update して envelope を消去
// TODO: Refine this code!
if (envelope.getInvalidAfter() != null && envelope.getInvalidAfter().isBefore(TimeUtil.getCurrentDateTime())) {
Logger.warn("run : envelope id " + envelope.getId() + " could not be sent : Time out.");
if (envelope.getUserMessageId() != null) {
UserReceivedMessage userMessage = daos.getUserReceivedMessageAccess().find(con, UUID.fromString(envelope.getUserMessageId()));
if (userMessage != null) {
UserReceivedMessage message = new UserReceivedMessage(userMessage);
message.setDelivery(MessageDelivery.FAIL);
message.setModifiedAt(TimeUtil.getCurrentDateTime());
daos.getUserReceivedMessageAccess().put(con, message);
}
}
if (envelope.getTwitterMessageId() != null) {
TwitterMessage twitterMessage = daos.getTwitterMessageAccess().find(con, envelope.getTwitterMessageId());
if (twitterMessage != null) {
TwitterMessage message = new TwitterMessage(twitterMessage);
message.setDelivery(MessageDelivery.FAIL);
message.setModifiedAt(TimeUtil.getCurrentDateTime());
daos.getTwitterMessageAccess().put(con, message);
}
}
if (envelope.getUserNotificationId() != null) {
UserNotification notification = daos.getUserNotificationAccess().find(con, envelope.getUserNotificationId());
if (notification != null) {
UserNotification message = new UserNotification(notification);
message.setDelivery(MessageDelivery.FAIL);
message.setModifiedAt(TimeUtil.getCurrentDateTime());
daos.getUserNotificationAccess().put(con, message);
}
}
it.remove();
continue;
}
// tryAfter 前であれば送らない。
if (envelope.getTryAfter() != null && !envelope.getTryAfter().isBefore(TimeUtil.getCurrentDateTime())) {
Logger.debug("run : envelope id " + envelope.getId() + " should be sent after " + envelope.getTryAfter());
continue;
}
if (envelope.getTwitterMessageId() != null)
sendTwitterMessage(con, daos, it, envelope);
else if (envelope.getUserMessageId() != null)
sendUserMessage(con, daos, it, envelope);
else if (envelope.getUserNotificationId() != null)
sendUserNotification(con, daos, it, envelope);
else {
// Hmm... shouldn't happen.
Logger.error("Shouldn't happen");
assert false;