@Override public String getFunctionValue(final Node node, final Parameters parameters) { if (log.isDebugEnabled()) { log.debug("node #" + node.getNumber()); log.debug("params: " + parameters); } String status = getDownloadStatus(node); int timeout = 5; if (parameters.get(TIMEOUT) != null) { timeout = parameters.get(TIMEOUT); } if (status == null) { Action action = ActionRepository.getInstance().get("streams", "download_media"); if (action == null) { throw new IllegalStateException("Action could not be found"); } if (node.getCloud().may(action, null)) { synchronized (runningJobs) { Future<?> future = runningJobs.get(node.getNumber()); if (future == null) { setDownloadStatus(node, "busy: " + System.currentTimeMillis()); future = submit(node, parameters); ThreadPools.identify( future, DownloadFunction.class.getName() + " downloading... for #" + node.getNumber() + " - status: " + getDownloadStatus(node)); String fname = ThreadPools.getString(future); log.info("Future name: " + fname); try { status = (String) future.get(timeout, TimeUnit.SECONDS); log.info("status: " + status); } catch (TimeoutException te) { status = ThreadPools.getString(future); log.info("TimeoutException: " + status); } catch (Exception e) { log.error(e); } } else { status = ThreadPools.getString(future); } } log.info("status: " + status); return status; } else { throw new org.mmbase.security.SecurityException("Not allowed"); } } return status; }
public <T> void parallelRecursive( final Executor exec, List<Node<T>> nodes, final Collection<T> results) { for (final Node<T> n : nodes) { exec.execute( new Runnable() { public void run() { results.add(n.compute()); } }); parallelRecursive(exec, n.getChildren(), results); } }
public void start() { nodes.addAll(readNodes); nodes.addAll(taskNodes); nodes.addAll(writerNodes); for (Node node : nodes) { node.init(); node.pre(); } for (ReadNode readNode : readNodes) { readNode.start(); } }
protected String getProperty(Node node, String key) { NodeManager properties = node.getCloud().getNodeManager("properties"); Function get = properties.getFunction("get"); Parameters params = get.createParameters(); params.set("node", node); params.set("key", key); return (String) get.getFunctionValue(params); }
protected void setProperty(Node node, String key, String value) { NodeManager properties = node.getCloud().getNodeManager("properties"); Function set = properties.getFunction("set"); Parameters params = set.createParameters(); params.set("node", node); params.set("key", key); if (value.length() > 255) { value = value.substring(0, 255); } params.set("value", value); set.getFunctionValue(params); }
public void join() { boolean allFinalized; synchronized (syncObject) { do { allFinalized = true; for (Node node : nodes) { if (!node.isFinished()) { System.out.println( "Node " + node.name + " is not finished pending:" + node.pendingJobs + " lastBatch:" + node.lastBatch); allFinalized = false; break; } /*else { System.out.println("Node " + node.name + " is finished"); }*/ } if (!allFinalized) { try { System.out.println("WAIT"); syncObject.wait(); System.out.println("NOTIFY"); } catch (InterruptedException e) { e.printStackTrace(); } } } while (!allFinalized); } for (Node node : nodes) { node.post(); } executorService.shutdown(); }
public <T> void sequentialRecursive(List<Node<T>> nodes, Collection<T> results) { for (Node<T> n : nodes) { results.add(n.compute()); sequentialRecursive(n.getChildren(), results); } }
private Boolean sendMail(HttpServletRequest req, Node node, String email) { boolean send = false; Cloud cloud = node.getCloud(); String emailbuilder = "email"; try { Module sendmail = cloud.getCloudContext().getModule("sendmail"); emailbuilder = sendmail.getProperty("emailbuilder"); } catch (NotFoundException nfe) { log.warn("No email module " + nfe); } if (cloud.hasNodeManager(emailbuilder)) { NodeManager nm = cloud.getNodeManager(emailbuilder); Node message = nm.createNode(); String host = req.getHeader("host"); if (host == null || "".equals(host)) { try { host = java.net.InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException uhe) { log.warn("No host: " + uhe); } } String from = "downloader@" + host; // do a quick check if we've got something more or less valid Pattern p = Pattern.compile(".+@.+\\.[a-z]+"); Matcher m = p.matcher(from); if (!m.matches()) { from = "*****@*****.**"; } String mediaTitle = node.getStringValue("title"); String mediaUrl = getProperty(node, URL_KEY); StringBuilder body = new StringBuilder(); body.append("*This is an automated message / Dit is een geautomatiseerd bericht*"); body.append("\n\n*English*"); body.append("\n\nDear,"); body.append("\n\nWe have received your file belonging to media item titled '") .append(mediaTitle) .append("'. "); body.append("In about 1 hour, you can find your submission at: "); body.append("http://").append(host).append("/media/").append(node.getNumber()); body.append("\n\nKind regards,"); body.append("\n\n").append(host); body.append("\n\n\n*Nederlands*"); body.append("\n\nBeste,"); body.append("\n\nWe hebben je bestand voor het media item met de titel '") .append(mediaTitle) .append("' ontvangen. "); body.append("Je kunt je bijdrage hier over circa een uur terugvinden: "); body.append("http://").append(host).append("/media/").append(node.getNumber()); body.append("\n\nMet vriendelijke groet,"); body.append("\n\n").append(host); message.setValue("from", from); message.setValue("to", email); message.setValue("subject", "Download complete / Download voltooid"); message.setValue("body", body.toString()); message.commit(); Function mail = message.getFunction("mail"); Parameters mail_params = mail.createParameters(); mail_params.set("type", "oneshot"); mail.getFunctionValue(mail_params); if (log.isDebugEnabled()) { log.debug("Message download ready send to: " + email); } send = true; } else { log.warn("Can not send message - no emailbuilder installed."); } return send; }
protected void setDownloadStatus(Node node, String status) { log.info("Setting status of " + node.getNumber() + " to " + status); setProperty(node, STATUS_KEY, status); }
/*package*/ List<O> doJob(List<I> batch) { List<O> generatedBatch; assert lastBatchSent == false; if (batch == POISON_PILL) { lastBatch = true; synchronized (name) { pendingJobs--; } // System.out.println(name + " - lastBatch"); generatedBatch = Collections.emptyList(); } else { EXECUTOR task = taskQueue.poll(); boolean nextNodesAvailable = true; for (Node<O, ?, ?> node : nodes) { nextNodesAvailable &= node.isAvailable(); } if (task == null) { // No available task resubmit(batch); generatedBatch = null; } else if (!nextNodesAvailable) { // Next nodes have to many batches. try { taskQueue.put(task); } catch (InterruptedException e) { e.printStackTrace(); } resubmit(batch); generatedBatch = null; } else { // Execute generatedBatch = execute(task, batch); // System.out.println(name + " - end job - " + generatedBatch.size()); for (Node<O, ?, ?> node : nodes) { node.submit(generatedBatch); } try { taskQueue.put(task); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (name) { pendingJobs--; } // System.out.println(name + " - pendingJobs " + pendingJobs); } } if (isFinished()) { if (!lastBatchSent) { for (Node<O, ?, ?> node : nodes) { node.submit(POISON_PILL); } lastBatchSent = true; } System.out.println("Node '" + name + "' is finished"); synchronized (syncObject) { syncObject.notify(); } } else { System.out.println("Node '" + name + "' pendingJobs " + pendingJobs); } return generatedBatch; }