<commit_before>package backtype.storm.spout;

import backtype.storm.generated.ShellComponent;
import backtype.storm.multilang.ShellMsg;
import backtype.storm.multilang.SpoutMsg;
import backtype.storm.task.TopologyContext;
import backtype.storm.utils.ShellProcess;
import java.util.Map;
import java.util.List;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ShellSpout implements ISpout {
    public static Logger LOG = LoggerFactory.getLogger(ShellSpout.class);

    private SpoutOutputCollector _collector;
    private String[] _command;
    private ShellProcess _process;
    private SpoutMsg spoutMsg;

    public ShellSpout(ShellComponent component) {
        this(component.get_execution_command(), component.get_script());
    }

    public ShellSpout(String... command) {
        _command = command;
    }

    public void open(Map stormConf, TopologyContext context,
                     SpoutOutputCollector collector) {
        _collector = collector;
        _process = new ShellProcess(_command);

        Number subpid = _process.launch(stormConf, context);
        LOG.info("Launched subprocess with pid " + subpid);
    }

    public void close() {
        _process.destroy();
    }

    public void nextTuple() {
        if (spoutMsg == null) {
            spoutMsg = new SpoutMsg();
        }
        spoutMsg.setCommand("next");
        spoutMsg.setId("");
        querySubprocess();
    }

    public void ack(Object msgId) {
    	if (spoutMsg == null) {
            spoutMsg = new SpoutMsg();
        }
        spoutMsg.setCommand("ack");
        spoutMsg.setId(msgId.toString());
        querySubprocess();
    }

    public void fail(Object msgId) {
    	if (spoutMsg == null) {
            spoutMsg = new SpoutMsg();
        }
        spoutMsg.setCommand("fail");
        spoutMsg.setId(msgId.toString());
        querySubprocess();
    }

    private void querySubprocess() {
        try {
            _process.writeSpoutMsg(spoutMsg);

            while (true) {
                ShellMsg shellMsg = _process.readShellMsg();
                String command = shellMsg.getCommand();
                if (command.equals("sync")) {
                    return;
                } else if (command.equals("log")) {
                    String msg = shellMsg.getMsg();
                    LOG.info("Shell msg: " + msg);
                } else if (command.equals("emit")) {
                    String stream = shellMsg.getStream();
                    Long task = shellMsg.getTask();
                    List<Object> tuple = shellMsg.getTuple();
                    Object messageId = shellMsg.getId();
                    if (task == 0) {
                        List<Integer> outtasks = _collector.emit(stream, tuple, messageId);
                        if (shellMsg.areTaskIdsNeeded()) {
                            _process.writeTaskIds(outtasks);
                        }
                    } else {
                        _collector.emitDirect((int) task.longValue(), stream,
                                tuple, messageId);
                    }
                }<commit_msg>Added debugging info to ShellSpout and pulled in latest upstream master.<commit_after>package backtype.storm.spout;

import backtype.storm.generated.ShellComponent;
import backtype.storm.multilang.ShellMsg;
import backtype.storm.multilang.SpoutMsg;
import backtype.storm.task.TopologyContext;
import backtype.storm.utils.ShellProcess;
import java.util.Map;
import java.util.List;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ShellSpout implements ISpout {
    public static Logger LOG = LoggerFactory.getLogger(ShellSpout.class);

    private SpoutOutputCollector _collector;
    private String[] _command;
    private ShellProcess _process;
    private SpoutMsg spoutMsg;

    public ShellSpout(ShellComponent component) {
        this(component.get_execution_command(), component.get_script());
    }

    public ShellSpout(String... command) {
        _command = command;
    }

    public void open(Map stormConf, TopologyContext context,
                     SpoutOutputCollector collector) {
        _collector = collector;
        _process = new ShellProcess(_command);

        Number subpid = _process.launch(stormConf, context);
        LOG.info("Launched subprocess with pid " + subpid);
    }

    public void close() {
        _process.destroy();
    }

    public void nextTuple() {
        if (spoutMsg == null) {
            spoutMsg = new SpoutMsg();
        }
        spoutMsg.setCommand("next");
        spoutMsg.setId("");
        querySubprocess();
    }

    public void ack(Object msgId) {
    	if (spoutMsg == null) {
            spoutMsg = new SpoutMsg();
        }
        spoutMsg.setCommand("ack");
        spoutMsg.setId(msgId.toString());
        querySubprocess();
    }

    public void fail(Object msgId) {
    	if (spoutMsg == null) {
            spoutMsg = new SpoutMsg();
        }
        spoutMsg.setCommand("fail");
        spoutMsg.setId(msgId.toString());
        querySubprocess();
    }

    private void querySubprocess() {
        try {
            _process.writeSpoutMsg(spoutMsg);

            while (true) {
                ShellMsg shellMsg = _process.readShellMsg();
                String command = shellMsg.getCommand();
                if (command.equals("sync")) {
                    return;
                } else if (command.equals("log")) {
                    String msg = shellMsg.getMsg();
                    LOG.info("Shell msg: " + msg);
                } else if (command.equals("emit")) {
                    String stream = shellMsg.getStream();
                    Long task = shellMsg.getTask();
                    List<Object> tuple = shellMsg.getTuple();
                    Object messageId = shellMsg.getId();
                    if (task == 0) {
                        List<Integer> outtasks = _collector.emit(stream, tuple, messageId);
                        if (shellMsg.areTaskIdsNeeded()) {
                            _process.writeTaskIds(outtasks);
                        }
                    } else {
                        _collector.emitDirect((int) task.longValue(), stream,
                                tuple, messageId);
                    }
                } else {
                    throw new RuntimeException("Unknown command received: " + command);
                }
{'fraction_non_alphanumeric': 0.1012970969734404, 'fraction_numerical': 0.0009264978381717109, 'mean_word_length': 1.8646616541353382, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6885490', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1687, 'n_words': 367}