Easy to Use EasyCron

I created my first cron job using EasyCron and it was Easy.

As part of the second phase of my To The Moon! Coin Calculator, I needed to populate my database with information on a scheduled basis. Since I am using a hosting service that doesn’t have a schedule jobs feature, I looked to a third party app to get it done.

EasyCron allows for anyone to create simple cron jobs for free or as a paid service, if you want more functionality.

For my needs, the free service works well. I only plan on running my cron job 3 times a day. I don’t need any email notifications, hooks or things provided in the paid subscription.

Since the To The Moon! status shouldn’t change very often, I really only need to get it once a day. But, since I might use the data provided for other information, I decided that I would schedule it for one might after midnight, 8 in the morning and 4 in the evening. This would give me the start of the day and the “trading open and close” values.

To make a cron job, you need a web page to point to in order to execute something. Since I am using php, I just simply create a page that will run the actions to get the JSON data object, parse it and then insert it into my database. If going to the page URL works to do these activities, then it will work with the cron job.

Of course, you don’t want to have the job run if someone else goes to your URL, so there is a very simple solution for this. EasyCron gives you the IP addresses of the servers that will call the URL. Once you have those, you can simply use php to check for those IP addresses and only run the job if the person accessing the URL has the correct IP address.

// create ip array
$ipArray = array( $easycronIP4, $myIp, $easycronIP1, $easycronIP2, $easycronIP3);
		
// find user's ip address
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
	$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_REAL_IP'])) {
	$ip = $_SERVER['HTTP_X_REAL_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
	$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
	$ip = $_SERVER['REMOTE_ADDR'];
}	
// check to see if ip is in array
if(in_array($ip, $ipArra, true) == true) {
	print_r(array_values($ipArray));
	print('<div>Move Along. ' .  $ip . '</div>');
} else {
	print('<div>Hello: ' .  $ip . '</div>');
	getMarketHistory();
}

It took a few attempts and a little bit of searching to figure out the right code would check the array of IPs to see if the current IP was correct. The function in_array did not work as expected as I had to add the “== true” portion. Also, EasyCron’s example of using $_SERVER[‘HTTP_X_REAL_IP’] didn’t work as well, but I added it to the list of ways to check for the IP.

Once you have both your php file and the check for IP address working, it is a good time to test it. After you create the cron job, you have the option to manually run it.

After the job runs, you can view the data output for the job. The data output is basically the URL output, so it is a smart thing to add some output content for your job so that you know it is running correctly. For example, for my page gives the output that looks like this.

However, the output displayed on EasyCron is actually the source code of the page.

Of course, you can not have the HTML tags and just get plain text, but that would make it boring to check when you run the job yourself.

The free subscription only requires that you reactivate it every month, which isn’t a big deal at all. You only get 200 executions per day, which is a lot. Again, I am only running 3. If I wanted to get more data on the coins, I could run it every hour and that still wouldn’t be very much. But, since I am getting 500 records every time I run it, I don’t think I want to run it more often than that.

After a half of day running, I can see the results.

As you can see, the last update from where I get the data isn’t done every minute. This isn’t a big deal, but by using the datetime stamp for when the row was added, it will be easier to get similar records without worry that they’ll be off by more than a minute.

This article was also posted on my Steemit.com blog

About DeanLogic
Dean has been playing around with programming ever since his family got an IBM PC back in the early 80's. Things have changed since BASICA and Dean has dabbled in HTML, JavaScript, Action Script, Flex, Flash, PHP, C#, C++, J2ME and SQL. On this site Dean likes to share his adventures in coding. And since programming isn't enough of a time killer, Dean has also picked up the hobby of short film creation.

About DeanLogic

Dean has been playing around with programming ever since his family got an IBM PC back in the early 80's. Things have changed since BASICA and Dean has dabbled in HTML, JavaScript, Action Script, Flex, Flash, PHP, C#, C++, J2ME and SQL. On this site Dean likes to share his adventures in coding. And since programming isn't enough of a time killer, Dean has also picked up the hobby of short film creation.