• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codeigniter CLI does not log to database

#1
Hey Guys,

I tried asking this question on StackOverflow, but I think people do not have the correct knowledge to help me out. At least the one answer I got was not what I was looking for... So here goes.

I have the following function which updates the status of my vacancies (based on dates) and logs the amount of vacancies that got a change, it looks like this:


PHP Code:
    //Function used to check the statusses of every vacancy and update it accordingly
        function checkVacanciesStatusses()
        {
            // this function can only be called from the command line
            if (!$this->input->is_cli_request()) {
                echo "Access is only allowed from the command line";
                return;
            }
    
            $this
->load->model('vacancy/vacancies_model');
            $this->load->library('managers/LogManager');
    
            $affected0 
$this->vacancies_model->updateToStatus0();
            $this->logmanager->createCronLog("updateToStatus0"$affected0);
            $affected1 $this->vacancies_model->updateToStatus1();
            $this->logmanager->createCronLog("updateToStatus1"$affected1);
            $affected2 $this->vacancies_model->updateToStatus2();
            $this->logmanager->createCronLog("updateToStatus2"$affected2);
        


This is put in a cronjob to be executed every 30minutes, like so:

Code:
    */30 * * * * /usr/bin/php /var/www/index.php cli/cron checkVacanciesStatusses >/dev/null 2>&1


The code for the createCronLog in my logmanager looks like this:

   
PHP Code:
class LogManager {
    
        
protected $CI;
    
        
// We'll use a constructor, as you can't directly call a function
        // from a property definition.
        public function __construct()
        {
            // Assign the CodeIgniter super-object
            $this->CI =& get_instance();
            $this->CI->load->helper('url');
            $this->CI->load->model('logging/logCron_model');
        }
    
        
/**
         * Creates an entry in the log_cron table
         *
         * @param string $method            the name of the method executed
         * @param array  $amount_updated    the amount of rows updated during the method the cron logged for
         *
         * @return int                      row id
         */
        public function createCronLog($method$amount_updated)
        {
            //Create a LOG entry
            $entity = new stdClass();
            $entity->created_at date('Y-m-d H:i:s');
            $entity->hostname gethostname();
            $entity->pid getmypid();
            $entity->user get_current_user();
            $entity->uri $_SERVER['REQUEST_URI'];
            $entity->method $method;
            $entity->amount_updated $amount_updated;
    
            
return $this->CI->logCron_model->add($entity);
        }
    

The thing which I cannot wrap my head around is that the vacancy updates (so the model calls) work perfectly. But the logging does not work.

When I comment the if clause which checks if it is a cli_request, and I manually browse to the function, the vacancies gets updated AND the logging works!


What is happening? 
It is not giving any errors or whatsoever, it just works when executed manually.
Reply

#2
Some shared hosting setups don't allow cli via cron. Maybe your problem?
Reply

#3
Of course there are no errors, because you are suppressing them:


Code:
>/dev/null 2>&1


Remove that and try again.
Reply

#4
(01-18-2018, 01:14 PM)skunkbad Wrote: Of course there are no errors, because you are suppressing them:


Code:
>/dev/null 2>&1


Remove that and try again.


Hello Skunkbad! 

I tried removing that, there are no errors, the only thing it returns is warnings about the time usage, that's all. If i execute thiswithout the IF cli check, the logging does happen. So I know the code is working as intended.
Reply

#5
(01-18-2018, 01:02 PM)dave friend Wrote: Some shared hosting setups don't allow cli via cron. Maybe your problem?


Hey Dave!

Wouldn't this then also bug the status updates? so basically half my function works , the logging half does not...
Reply

#6
(01-19-2018, 05:19 AM)creawbe Wrote: Hey Dave!

Wouldn't this then also bug the status updates? so basically half my function works , the logging half does not...

I didn't understand that it was happening that way. But yes, I would think so.

What happens if you comment the cli check and have cron execute? Same behavior?
Reply

#7
i have a similar issue in one of my crons job, so remove the if statement.

Another method is to call the controller method via curl since you want to secure the execution of that page.
Reply

#8
(01-19-2018, 07:32 AM)dave friend Wrote:
(01-19-2018, 05:19 AM)creawbe Wrote: Hey Dave!

Wouldn't this then also bug the status updates? so basically half my function works , the logging half does not...

I didn't understand that it was happening that way. But yes, I would think so.

What happens if you comment the cli check and have cron execute? Same behavior?

Yep, exact same behavior. Statusses get updated but the logging does not occur...
Reply

#9
(01-19-2018, 07:47 AM)Folad Wrote: i have a similar issue in one of my crons job, so remove the if statement.

Another method is to call the controller method via curl since you want to secure the execution of that page.

How do you mean? Should I call the library via curl?
Reply

#10
(01-18-2018, 07:45 AM)creawbe Wrote:
Code:
    */30 * * * * /usr/bin/php /var/www/index.php cli/cron checkVacanciesStatusses >/dev/null 2>&1

The syntax after index.php should be the same as if you enter it in a URL. I don't know what 'cli/cron' is in there, maybe dir/controller, but the fact that the checkVacanciesStatusses is separated from them doesn't look right.

(01-19-2018, 02:35 AM)creawbe Wrote:
(01-18-2018, 01:14 PM)skunkbad Wrote: Of course there are no errors, because you are suppressing them:


Code:
>/dev/null 2>&1


Remove that and try again.


Hello Skunkbad! 

I tried removing that, there are no errors, the only thing it returns is warnings about the time usage, that's all. If i execute thiswithout the IF cli check, the logging does happen. So I know the code is working as intended.

This can't be true if only the logging doesn't work - either the entire thing doesn't work under CLI, or you have more errors. You've used $_SERVER['REQUEST_URI'] in there, and that variable doesn't exist under a CLI environment, so there must be a warning about that.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.