Thread: Using cronjobs
View Single Post
  #1  
Old 06-30-2017, 02:58 PM
Dinocanid's Avatar
Dinocanid Dinocanid is offline
Member
 
Join Date: Aug 2016
Location: Maryland, USA
Posts: 516
Gender: Unknown/Other
Credits: 65,172
Dinocanid is on a distinguished road
Default Using cronjobs

I thought this might be helpful for people who want some automatic functions on their site. Special thanks to Kyttias and IntoRain!
Disclaimer: It is not recommended that you have several cronjobs running at once unless you have a really good server, because it can potentially slow things down; especially if the runtime is frequent (IE: running several times a day)

-Introduction-
Create a new file in public_html (or wherever you have mysidia installed) called cron.php. Inside, paste this:
PHP Code:
<?php

class CronController extends AppController{

    public function 
__construct(){

    }
    
    public function 
index(){

    }   

}
?>
-Adjusting the database-
More often then not, you're going to want to do some math. Whether it's giving pets realtime needs or some sort of daily rewards system, the database needs to know how to math. Go to class_database.php and add this: Pastebin Link

Here are the examples IntoRain gave on how these can now be used in scripts:
PHP Code:
//decrease mood of every owned adoptable by 2
$mysidia->db->update_decrease("owned_adoptables", array("mood"), 2);

//decrease mood and totalclicks by 1, when an adoptable has totalclicks = 1
$mysidia->db->update_decrease("owned_adoptables", array("mood""totalclicks"), 1"totalclicks = 1"); 
-Creating your first cronjob function-
From here, it's just like coding for any other part of the script. Go to cron.php and create a new function like this (do not add anything to the index function or construct function!):
PHP Code:
public function YourFunction(){
    
$mysidia Registry::get("mysidia");
    
//coding goes here

    

Here's an example of how I rotate seasons for my site (feel free to use if you'd like):
PHP Code:
public function changeSeason(){
    
//Allows shops, explore, and other site features to rotate automatically. Runs once a month.    
    
$mysidia Registry::get("mysidia");
    
    
$month date('F');
    
    if(
$month 'March' OR 'April' OR 'May'){
        
$mysidia->db->update("seasons", array("season" => 'Spring'));
    }

    if(
$month 'June' OR 'July' OR 'August'){
        
$mysidia->db->update("seasons", array("season" => 'Summer'));
    }

    if(
$month 'September' OR 'October' OR 'November'){
        
$mysidia->db->update("seasons", array("season" => 'Fall'));
    }
    
    if(
$month 'December' OR 'January' OR 'February'){
        
$mysidia->db->update("seasons", array("season" => 'Winter'));
    }
    
    } 
Now in my other scripts, I can easily add seasonal things in a simple switch statement without getting too lengthy.
Adding little notes right inside the functions is something I recommend so you can easily tell when it should run at a glance. They're also helpful if you have multiple coders, so notes are good!

-Making it go-
Now that you created your function, you need to run it. You might have noticed that cron.php has no coding on actually running the functions, and that's intentional! Many server hosts (including mysidia host) have CPanel available. Access it and navigate to cronjobs under the "advanced" section.

(cpanel)

(retro cpanel)

(mysidiahost client area)

Luckily for us, the page is full of presets. Clicking on the dropdowns allows you to specify exactly when you want the cronjob to run (even down to an exact minute once a year!) Customize it however you want. Afterwards use this for the command section:
Code:
curl http://YOURSITE.com/cron/FUNCTION
Replace "function" with the name of the function you created in cron.php. This is the command for my seasons:
Code:
curl http://adopttest.mysidiahost.com/cron/changeSeason
For every new function you make, you have to create a new cronjob for it. (excluding index and _construct)
__________________
Reply With Quote