<commit_before><?php
class Kwf_Controller_Action_Cli_Web_NewsletterController extends Kwf_Controller_Action_Cli_Abstract
{
    public static function getHelp()
    {
        return "Call by cronjob to send waiting newsletters. If called manually, Ctrl+C stops newsletter, ist has to be started again!";
    }

    public static function getHelpOptions()
    {
        return array(
            array(
                'param'=> 'debug',
                'value'=> false,
                'valueOptional' => true,
            )
        );
    }
    /*
    possible parameters:
    --maxProcesses
    --debug
    --benchmark
    */

    public function indexAction()
    {
        throw new Kwf_Exception_Client("Don't call Newsletter controller directly. Use process-control to start 'php bootstrap.php newsletter start' instead.");
    }

    public function startAction()
    {
        $this->_helper->viewRenderer->setNoRender(true);

        $model = Kwf_Model_Abstract::getInstance('Kwc_Newsletter_Model');

        $procs = array();

        while (true) {
            $select = $model->select()
                ->where(new Kwf_Model_Select_Expr_Or(array(
                    new Kwf_Model_Select_Expr_Equal('status', 'start'),
                    new Kwf_Model_Select_Expr_And(array(
                        new Kwf_Model_Select_Expr_Equal('status', 'startLater'),
                        new Kwf_Model_Select_Expr_LowerEqual('start_date', new Kwf_DateTime(time())),
                    )),
                    new Kwf_Model_Select_Expr_Equal('status', 'sending')
                )));
            $rows = $model->getRows($select);
            $activeCountRows = count($rows);
            foreach ($rows as $newsletterRow) {

                if ($newsletterRow->status != 'sending') {
                    $newsletterRow->resume_date = date('Y-m-d H:i:s');
                    $newsletterRow->status = 'sending';
                    $newsletterRow->save();
                }

                if (!isset($procs[$newsletterRow->id])) {
                    $procs[$newsletterRow->id] = array();
                }

                //remove stopped processes (might stop because of memory limit or simply crash for some reason)
                foreach ($procs[$newsletterRow->id] as $k=>$p) {
                    if (!$p->isRunning()) {
                        echo "process ".$p->getPid()." stopped...\n";
                        unset($procs[$newsletterRow->id][$k]);
                    }
                }

                $s = new Kwf_Model_Select();
                $s->whereEquals('newsletter_id', $newsletterRow->id);
                $s->whereNull('send_process_pid');<commit_msg>output only in debug mode<commit_after><?php
class Kwf_Controller_Action_Cli_Web_NewsletterController extends Kwf_Controller_Action_Cli_Abstract
{
    public static function getHelp()
    {
        return "Call by cronjob to send waiting newsletters. If called manually, Ctrl+C stops newsletter, ist has to be started again!";
    }

    public static function getHelpOptions()
    {
        return array(
            array(
                'param'=> 'debug',
                'value'=> false,
                'valueOptional' => true,
            )
        );
    }
    /*
    possible parameters:
    --maxProcesses
    --debug
    --benchmark
    */

    public function indexAction()
    {
        throw new Kwf_Exception_Client("Don't call Newsletter controller directly. Use process-control to start 'php bootstrap.php newsletter start' instead.");
    }

    public function startAction()
    {
        $this->_helper->viewRenderer->setNoRender(true);

        $model = Kwf_Model_Abstract::getInstance('Kwc_Newsletter_Model');

        $procs = array();

        while (true) {
            $select = $model->select()
                ->where(new Kwf_Model_Select_Expr_Or(array(
                    new Kwf_Model_Select_Expr_Equal('status', 'start'),
                    new Kwf_Model_Select_Expr_And(array(
                        new Kwf_Model_Select_Expr_Equal('status', 'startLater'),
                        new Kwf_Model_Select_Expr_LowerEqual('start_date', new Kwf_DateTime(time())),
                    )),
                    new Kwf_Model_Select_Expr_Equal('status', 'sending')
                )));
            $rows = $model->getRows($select);
            $activeCountRows = count($rows);
            foreach ($rows as $newsletterRow) {

                if ($newsletterRow->status != 'sending') {
                    $newsletterRow->resume_date = date('Y-m-d H:i:s');
                    $newsletterRow->status = 'sending';
                    $newsletterRow->save();
                }

                if (!isset($procs[$newsletterRow->id])) {
                    $procs[$newsletterRow->id] = array();
                }

                //remove stopped processes (might stop because of memory limit or simply crash for some reason)
                foreach ($procs[$newsletterRow->id] as $k=>$p) {
                    if (!$p->isRunning()) {
                        if ($this->_getParam('debug')) echo "process ".$p->getPid()." stopped...\n";
                        unset($procs[$newsletterRow->id][$k]);
                    }
                }

                $s = new Kwf_Model_Select();
                $s->whereEquals('newsletter_id', $newsletterRow->id);
                $s->whereNull('send_process_pid');
{'fraction_non_alphanumeric': 0.11846239142487526, 'fraction_numerical': 0.0, 'mean_word_length': 1.7668711656441718, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 50, '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': '7146211', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1389, 'n_words': 301}