use of com.redhat.jenkins.plugins.ci.messaging.data.FedmsgMessage in project jms-messaging-plugin by jenkinsci.
the class FedMsgMessagingWorker method sendMessage.
@Override
public SendResult sendMessage(Run<?, ?> build, TaskListener listener, ProviderData pdata) {
FedMsgPublisherProviderData pd = (FedMsgPublisherProviderData) pdata;
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket sock = context.socket(ZMQ.PUB);
sock.setLinger(0);
log.fine("pub address: " + provider.getPubAddr());
sock.connect(provider.getPubAddr());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String body = "";
String msgId = "";
try {
EnvVars env = new EnvVars();
env.putAll(build.getEnvironment(listener));
env.put("CI_NAME", build.getParent().getName());
if (!build.isBuilding()) {
env.put("CI_STATUS", (build.getResult() == Result.SUCCESS ? "passed" : "failed"));
env.put("BUILD_STATUS", Objects.requireNonNull(build.getResult()).toString());
}
FedmsgMessage fm = new FedmsgMessage(PluginUtils.getSubstitutedValue(getTopic(provider), build.getEnvironment(listener)), PluginUtils.getSubstitutedValue(pd.getMessageContent(), env));
fm.setTimestamp(System.currentTimeMillis());
// Use toString() instead of getBodyJson so that message ID is included and sent.
body = fm.toJson();
msgId = fm.getMsgId();
if (!sock.sendMore(fm.getTopic()) && pd.isFailOnError()) {
log.severe("Unhandled exception in perform: Failed to send message (topic)!");
return new SendResult(false, msgId, body);
}
if (!sock.send(body) && pd.isFailOnError()) {
log.severe("Unhandled exception in perform: Failed to send message (body)!");
return new SendResult(false, msgId, body);
}
log.fine("JSON message body:\n" + body);
listener.getLogger().println("JSON message body:\n" + body);
} catch (Exception e) {
if (pd.isFailOnError()) {
log.severe("Unhandled exception in perform: ");
log.severe(ExceptionUtils.getStackTrace(e));
listener.fatalError("Unhandled exception in perform: ");
listener.fatalError(ExceptionUtils.getStackTrace(e));
return new SendResult(false, msgId, body);
} else {
log.warning("Unhandled exception in perform: ");
log.warning(ExceptionUtils.getStackTrace(e));
listener.error("Unhandled exception in perform: ");
listener.error(ExceptionUtils.getStackTrace(e));
return new SendResult(true, msgId, body);
}
} finally {
sock.close();
context.term();
}
return new SendResult(true, msgId, body);
}
use of com.redhat.jenkins.plugins.ci.messaging.data.FedmsgMessage in project jms-messaging-plugin by jenkinsci.
the class FedMsgMessagingWorker method receive.
@Override
public void receive(String jobname, ProviderData pdata) {
FedMsgSubscriberProviderData pd = (FedMsgSubscriberProviderData) pdata;
int timeoutInMs = (pd.getTimeout() != null ? pd.getTimeout() : FedMsgSubscriberProviderData.DEFAULT_TIMEOUT_IN_MINUTES) * 60 * 1000;
if (interrupt) {
log.info("we have been interrupted at start of receive");
return;
}
while (!subscribe(jobname)) {
if (!Thread.currentThread().isInterrupted()) {
try {
int WAIT_SECONDS = 2;
Thread.sleep(WAIT_SECONDS * 1000);
} catch (InterruptedException e) {
// We were interrupted while waiting to retry. We will
// jump ship on the next iteration.
// NB: The interrupt flag was cleared when
// InterruptedException was thrown. We have to
// re-install it to make sure we eventually leave this
// thread.
Thread.currentThread().interrupt();
}
}
}
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
long lastSeenMessage = new Date().getTime();
try {
while ((new Date().getTime() - lastSeenMessage) < timeoutInMs) {
if (poller.poll(1000) > 0) {
pollerClosed = false;
if (poller.pollin(0)) {
ZMsg z = ZMsg.recvMsg(poller.getSocket(0));
// Reset timer
lastSeenMessage = new Date().getTime();
//
String json = z.getLast().toString();
FedmsgMessage data = mapper.readValue(json, FedmsgMessage.class);
if (provider.verify(data.getBodyJson(), pd.getChecks(), jobname)) {
Map<String, String> params = new HashMap<>();
params.put("CI_MESSAGE", data.getBodyJson());
trigger(jobname, provider.formatMessage(data), params);
}
}
} else {
if (interrupt) {
log.info("We have been interrupted...");
pollerClosed = true;
break;
}
}
}
if (!interrupt) {
log.info("No message received for the past " + timeoutInMs + " ms, re-subscribing for job '" + jobname + "'.");
unsubscribe(jobname);
}
} catch (Exception e) {
if (!Thread.currentThread().isInterrupted()) {
// Something other than an interrupt causes this.
// Unsubscribe, but stay in our loop and try to reconnect..
log.log(Level.WARNING, "JMS exception raised, going to re-subscribe for job '" + jobname + "'.", e);
// Try again next time.
unsubscribe(jobname);
}
}
}
use of com.redhat.jenkins.plugins.ci.messaging.data.FedmsgMessage in project jms-messaging-plugin by jenkinsci.
the class FedMsgMessageWatcher method watch.
@Override
public String watch() {
fedMsgMessagingProvider = (FedMsgMessagingProvider) provider;
log.info("Waiting for message with selector: " + selector);
for (MsgCheck msgCheck : checks) {
log.info(" with check: " + msgCheck.toString());
}
taskListener.getLogger().println("Waiting for message with selector: " + selector);
for (MsgCheck msgCheck : checks) {
taskListener.getLogger().println(" with check: " + msgCheck.toString());
}
log.info(" with timeout: " + timeout);
taskListener.getLogger().println(" with timeout: " + timeout);
lcontext = ZMQ.context(1);
lpoller = lcontext.poller(1);
lsocket = lcontext.socket(ZMQ.SUB);
topic = PluginUtils.getSubstitutedValue(getTopic(overrides, fedMsgMessagingProvider.getTopic(), DEFAULT_TOPIC), environment);
lsocket.subscribe(topic.getBytes(StandardCharsets.UTF_8));
lsocket.setLinger(0);
lsocket.connect(fedMsgMessagingProvider.getHubAddr());
lpoller.register(lsocket, ZMQ.Poller.POLLIN);
ObjectMapper mapper = new ObjectMapper();
long startTime = new Date().getTime();
int timeoutInMs = timeout * 60 * 1000;
interrupted = false;
try {
while ((new Date().getTime() - startTime) < timeoutInMs) {
if (lpoller.poll(1000) > 0) {
if (lpoller.pollin(0)) {
ZMsg z = ZMsg.recvMsg(lpoller.getSocket(0));
String json = z.getLast().toString();
FedmsgMessage data = mapper.readValue(json, FedmsgMessage.class);
if (!provider.verify(data.getBodyJson(), checks, jobname)) {
continue;
}
return data.getBodyJson();
}
}
}
if (interrupted) {
return null;
}
log.severe("Timed out waiting for message!");
taskListener.getLogger().println("Timed out waiting for message!");
} catch (Exception e) {
log.log(Level.SEVERE, "Unhandled exception waiting for message.", e);
} finally {
try {
if (lpoller != null) {
ZMQ.Socket s = lpoller.getSocket(0);
lpoller.unregister(s);
s.disconnect(fedMsgMessagingProvider.getHubAddr());
lsocket.unsubscribe(this.topic.getBytes(StandardCharsets.UTF_8));
lsocket.close();
}
if (lcontext != null) {
lcontext.term();
}
} catch (Exception e) {
log.fine(e.getMessage());
}
lpoller = null;
lsocket = null;
}
return null;
}
use of com.redhat.jenkins.plugins.ci.messaging.data.FedmsgMessage in project jms-messaging-plugin by jenkinsci.
the class FedMsgMessagingWorker method waitForMessage.
@Override
public String waitForMessage(Run<?, ?> build, TaskListener listener, ProviderData pdata) {
FedMsgSubscriberProviderData pd = (FedMsgSubscriberProviderData) pdata;
log.info("Waiting for message.");
listener.getLogger().println("Waiting for message.");
for (MsgCheck msgCheck : pd.getChecks()) {
log.info(" with check: " + msgCheck.toString());
listener.getLogger().println(" with check: " + msgCheck);
}
Integer timeout = (pd.getTimeout() != null ? pd.getTimeout() : FedMsgSubscriberProviderData.DEFAULT_TIMEOUT_IN_MINUTES);
log.info(" with timeout: " + timeout);
listener.getLogger().println(" with timeout: " + timeout);
ZMQ.Context lcontext = ZMQ.context(1);
ZMQ.Poller lpoller = lcontext.poller(1);
ZMQ.Socket lsocket = lcontext.socket(ZMQ.SUB);
String ltopic = getTopic(provider);
try {
ltopic = PluginUtils.getSubstitutedValue(getTopic(provider), build.getEnvironment(listener));
} catch (IOException | InterruptedException e) {
log.warning(e.getMessage());
}
lsocket.subscribe(ltopic.getBytes(StandardCharsets.UTF_8));
lsocket.setLinger(0);
lsocket.connect(provider.getHubAddr());
lpoller.register(lsocket, ZMQ.Poller.POLLIN);
ObjectMapper mapper = new ObjectMapper();
long startTime = new Date().getTime();
int timeoutInMs = timeout * 60 * 1000;
try {
while ((new Date().getTime() - startTime) < timeoutInMs) {
if (lpoller.poll(1000) > 0) {
if (lpoller.pollin(0)) {
ZMsg z = ZMsg.recvMsg(lpoller.getSocket(0));
listener.getLogger().println("Received a message");
String json = z.getLast().toString();
FedmsgMessage data = mapper.readValue(json, FedmsgMessage.class);
String body = data.getBodyJson();
if (!provider.verify(body, pd.getChecks(), jobname)) {
continue;
}
if (StringUtils.isNotEmpty(pd.getVariable())) {
EnvVars vars = new EnvVars();
vars.put(pd.getVariable(), body);
build.addAction(new CIEnvironmentContributingAction(vars));
}
return body;
}
}
}
log.severe("Timed out waiting for message!");
listener.getLogger().println("Timed out waiting for message!");
} catch (Exception e) {
log.log(Level.SEVERE, "Unhandled exception waiting for message.", e);
} finally {
try {
ZMQ.Socket s = lpoller.getSocket(0);
lpoller.unregister(s);
s.disconnect(provider.getHubAddr());
lsocket.unsubscribe(ltopic.getBytes(StandardCharsets.UTF_8));
lsocket.close();
lcontext.term();
} catch (Exception e) {
listener.getLogger().println("exception in finally");
}
}
return null;
}
Aggregations