I'm very new to creating cron jobs. Just started dabbling yesterday.
I design a website for a local realtor and I have a field in my database for 'Status' (For Sale, Pending, Sold), 'SoldDate' (NULL if not sold), and 'Visible' (Boolean). I am trying to create a cron job that checks once a day for any sold properties that 'SoldDate' is greater than 30 days in the past and if TRUE change the 'Visible' field to 0 (False).
Right now I just have a test script that adds a value to a dummy database called 'CRON' and a timestamp.
I'm using Codeigniter to build my website. Below is my code:
Cron Controller
class Cron extends MY_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('cron_m');
}
public function index()
{
if(!$this->input->is_cli_request())
{
echo "This script can only be accessed via the command line" . PHP_EOL;
return;
}
$data = array('value' => 'test');
$this->cron_m->save($data);
echo 'saved!';
}
}
Frontend_Controller
class Frontend_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->library('form_validation');
}
public function lock()
{
$this->load->library('session');
$this->load->model('user_m');
// Login check
$exception_uris = array(
'admin/user/login',
'admin/user/logout'
);
if (in_array(uri_string(), $exception_uris) == FALSE)
{
if ($this->user_m->loggedin() == FALSE)
{
$uri = $this->uri->uri_string();
$this->session->set_tempdata('uri', $uri);
redirect('admin/user/login');
}
}
}
}
MY_Controller
class MY_Controller extends CI_Controller {
public $data = array();
function __construct() {
parent::__construct();
$this->data['errors'] = array();
}
}
Cron_m Model
class Cron_m extends MY_Model {
public $_table_name = 'CRON';
protected $_order_by = 'id';
public function __construct()
{
parent::__construct();
}
}
MY_Model
class MY_Model extends CI_Model {
public $_table_name = '';
protected $_primary_key = 'id';
protected $_primary_filter = 'intval';
protected $_order_by = 'order';
function __construct()
{
parent::__construct();
}
public function save($data, $id = NULL)
{
//INSERT
if ($id === NULL)
{
!isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
$this->db->set($data);
$this->db->insert($this->_table_name);
$id = $this->db->insert_id();
}
//UPDATE
else
{
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->set($data);
$this->db->where($this->_primary_key, $id);
$this->db->update($this->_table_name);
}
return $id;
}
}
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|assets|uploads|podcast\.xml)
RewriteRule ^(.*)$ /index.php/$1 [L]
Everything works fine on my local machine, but when I run the code in the terminal on my web server (php /home4/server_name/public_html/site_name/index.php cron) It doesn't run and gives me an error about a Database Error Occurred for the session database. "ip_address cannot be null"
I'm not sure why it's trying to create session data. I haven't created a session in my Fronted_Controller.
Below is the error i get:
Status: 500 Internal Server Error
Set-Cookie: ci_session=e9e9c8120a392b9009cd80196ce2fa2cabda516e; expires=Fri, 27-Nov-2015 01:42:33 GMT; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-type: text/html; charset=UTF-8
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Database Error</title>
<style type="text/css">
::selection { background-color: #E13300; color: white; }
::-moz-selection { background-color: #E13300; color: white; }
body {
background-color: #fff;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}
a {
color: #003399;
background-color: transparent;
font-weight: normal;
}
h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 19px;
font-weight: normal;
margin: 0 0 14px 0;
padding: 14px 15px 10px 15px;
}
code {
font-family: Consolas, Monaco, Courier New, Courier, monospace;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
#container {
margin: 10px;
border: 1px solid #D0D0D0;
box-shadow: 0 0 8px #D0D0D0;
}
p {
margin: 12px 15px 12px 15px;
}
</style>
</head>
<body>
<div id="container">
<h1>A Database Error Occurred</h1>
<p>Error Number: 1048</p><p>Column 'ip_address' cannot be null</p>
<p>INSERT INTO `CI_SESSIONS` (`id`, `ip_address`, `timestamp`, `data`) VALUES ('e9e9c8120a392b9009cd80196ce2fa2cabda516e', NULL, 1448581353, '__ci_last_regenerate|i:1448581353;')</p>
<p>Filename: libraries/Session/drivers/Session_database_driver.php</p><p>Line Number: 220</p>
</div>
</body>
I'm not sure where I'm going wrong. If you need any additional information let me know. I would love any help or suggestions.
Thank you.