#1
  1. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221

    Is this what a cronjob is for?


    Hello

    Is it a good idea to use a cronjob in order to take some pressure off the API endpoint?

    In other words, would it be a good idea to replace script A with script B below and every minute run a cron job to clean the database and apply that logic?

    Script A

    PHP Code:
    <?php
    defined
    ('BASEPATH') OR exit('No direct script access allowed');

    class 
    Data_receiver extends CI_Controller {

        public function 
    __construct()
            {
                
    parent::__construct();
            }

        public function 
    index() 
            {
                
    //$this->output->enable_profiler(TRUE);
                
    $this->db->trans_start();
                
    $sql "SELECT * FROM campaign_owners INNER JOIN campaign_campaignes 
                ON campaign_campaignes.user_id = campaign_owners.id 
                    AND campaign_campaignes.id = ?
                WHERE campaign_owners.id = ?"
    ;
                
    $query $this->db->query($sql, array($_POST['campaign_id'], $_POST['user_id']));
                if(
    $query->num_rows()!=1)
                    {
                        echo 
    "Invalid Request";exit();
                    }
                else
                    {
                        
    $results $query->result_array()[0];    
                        if(
    $results['status']!='active' || $results['secret_key']!=$_POST['secret_key'])
                            {
                                echo 
    "Inactive Account Or Invalid Secret";exit();
                            }
                        else 
                            {
                                
    //Get campaign timezone:
                                
    $timezone $this->custom_functions->get_timezone_by_campaign_id($_POST['campaign_id']);
                                
    $this->custom_functions->set_timezone($timezone);
                                
    //Validate Launch
                                
    $sql "SELECT * FROM campaign_campaignes WHERE id = ? AND campaign_type = ?";
                                
    $query $this->db->query($sql, array($_POST['campaign_id'], 'evergreen'));
                                if(
    $query->num_rows()==1)
                                    {
                                        
    //Does Prospect exist under this user?
                                        
    $sql "SELECT * FROM campaign_prospects WHERE email = ? AND owner_id = ?";
                                        
    $query $this->db->query($sql, array($_POST['prospect_email'], $_POST['user_id']));
                                        if(
    $query->num_rows()==1)
                                            {
                                                
    $prospect_id $query->result_array()[0]['id'];
                                            }
                                        else 
                                            {
                                                
    $data = array(
                                                    
    'email' => $_POST['prospect_email'],
                                                    
    'owner_id' => $_POST['user_id'],
                                                );
                                                
    $this->db->insert('campaign_prospects'$data);
                                                
    $prospect_id $this->db->insert_id();
                                            }
                                        
    //Delete prospect from this campaign if already exist in the seqeunce
                                        
    $this->db->delete('campaign_campaign_prospect', array('campaign_id' => $_POST['campaign_id'], 'prospect_id'=>$prospect_id)); 
                                        
    //Add Prospect To Launch
                                        
    $data = array(
                                                    
    'campaign_id' => $_POST['campaign_id'],
                                                    
    'prospect_id' => $prospect_id,
                                                    
    'time_added' => date('H:i'),
                                                    
    'date_added' => date('Y-m-d'),
                                                    
    'source' => (isset($_POST['source']) ? $_POST['source'] : '')
                                            );
                                        
    $this->db->insert('campaign_campaign_prospect'$data);
                                        
    $last $this->db->insert_id();
                                        
    //Insert the same into stats table
                                        
    $data = array(
                                                    
    'campaign_id' => $_POST['campaign_id'],
                                                    
    'prospect_id' => $prospect_id,
                                                    
    'time_added' => date('H:i'),
                                                    
    'date_added' => date('Y-m-d'),
                                                    
    'source' => (isset($_POST['source']) ? $_POST['source'] : '')
                                            );
                                        
    $this->db->insert('campaign_campaign_prospect_history'$data);
                                        
                                        echo 
    "Added Successfully";
                                    }
                                else 
                                    {
                                        echo 
    "Invalid campaign";exit();
                                    }    
                            }    
                    }    
            
    $this->output->enable_profiler(TRUE);
            
    $this->db->trans_complete();        

            }
    }

    Script B
    PHP Code:


    //Just check if total number of records is not over 100k then

    $data = array(
                
    'campaign_id' => $_POST['campaign_id'],
                
    'prospect_id' => $prospect_id,
                
    'time_added' => date('H:i'),
                
    'date_added' => date('Y-m-d'),
                
    'source' => (isset($_POST['source']) ? $_POST['source'] : '')
        );
    $this->db->insert('campaign_campaign_prospect'$data); 
  2. #2
  3. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,904
    Rep Power
    9646
    The API should not allow requests if you don't want to allow them, so it should check the database to make sure everything is okay.

    cronjobs are about background tasks. Something that is not directly related to user input or actions. A cronjob to "clean the database" is (probably) fine in theory because it's not driven by the user, but I would be worried about the "cleaning" you need to do.
  4. #3
  5. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221
    Hi, thanks.

    That's the easy part. Removing duplicates etc.

    By the way, you might find this interesting.

    Server dudes came back and said the code is good enough IF we use the beast ==> https://www.liquidweb.com/products/a...oad-balancers/ <===

    I am trying to avoid load balancers since it's over 1k a month. So before that, we're gonna try separating database server from the web server, optimizing the API code and maybe taking advantage of cronjobs. I was gonna instal laravle workers but cron seems easier to set up.

    Will have this ready soon.

    I should start charging Devshed for SEO. I could use the cash to pay for the load balancer.

IMN logo majestic logo threadwatch logo seochat tools logo