How To Create A Background Process?

Hi All,

I need to create a process that needs to be run every day, I guess that for that, I will need to create a CronJob in my hosting.

My idea is to have two tables:

  • queue_in_table: where I have the records to be processed.

  • queue_out_table: where I have the records already processed.

Well, the process will read from queue_in_table will do some treatment like updating some fields in other tables and after that, if there is no errors will copy the record from queur_in_table to queue_out_put.

Now… how to do it? I thought that I could do it by creating a simple action in some controller, but reding around forum and guides, I found that the best way is by creating a console application, is that right? so the action would be in protected/commands/CronCommand.php




class CronCommand extends CConsoleCommand

{

    public function actionIndex($type) {

       //do things

    }

}



Also it will be needed to make some changes in config/console.php

What is the script file that will launch this action? or how to run it?

what else I’m forgetting? is ok my approach or what other things shoudl be aware of?

Any suggestions are welcome!

Thanks!

There is a good wiki article on running cronjobs - read that first.

An alternate way to automate jobs instead of creating a console app is to run wget/lynx from the commandline on a linux server (cronjob) with the URL of your action and a secret $_GET key parameter.

The cronjob wiki is pretty easy to follow. That’s where I started and I ended up with a very good implementation for my website.

I’m currently working on a hobby website. It’s beta launched already so guys can check it out. I have a similar requirement to run a daily jobs which basically award badges to the users. These are the badges http://www.dizkover.com/help/badges

My cron job might be a little confusing. Because it involves, run_log table and transaction tables.

For example, in http://www.dizkover.com, I created several transaction tables that stores measures. One transaction table is item_tran table. This table stores vote count and comment count in a daily level. So basically the daily table has a YYYYMMDD integer index column.

For the cronjob, I have a run_log table that stores when the job was last executed. I just basically stored the YYYYMMDD integer as the run_id and a job_id.

I have 2 things:

  1. Job shell script file to be called by cron:

For example:


YIIC=/www/protected/yiic

#Arguments job_id, badge_id, like_count

$YIIC job runUpdateItemTranSummary 	100		#update item_tran_summary

#start from 200 arguments job_id, badge_id, like_count

$YIIC job runUpdateUserBadge 		200 10 10 	#good post

  1. JobCommand.php

For example:


<?php 

class JobCommand extends CConsoleCommand

{

	public function run($args) 

	{ 

		echo "\n\n";

		$this->printLog("Running $args[0] $args[1] $args[2] $args[3] $args[4]...");

  		switch ($args[0])

  		{

  			case "runUpdateItemTranSummary":

  				$this->actionItemTranSummary($args[1]);

  				break;

  			case "runUpdateUserBadge":

  				$this->actionUpdateUserBadge($args[1],$args[2],$args[3],$args[4]);

  				break;

  			default:

  				$this->printLog("Unknown job $args[0].");

  				break;

  		}

	}

....

?>

Now this is what the job does:

  1. Update the summary table measures base on the last run looking up the run_id. So I don’t have to look up the whole daily table.

  2. Look for award measures in summary table. For example if the item has 10+ likes, I assign the user an award of "Good Post" which is stored in user_badge table.

Thanks to both. I always read as much documentation as I can, and I already read that guide and other posts, I just wanted to be sure that I wasn’t missing something and that the best way is by creating a command.

I wouldn’t create two tables and copy records from one table to the other table. That is bad design.

Create one table with the queue. Create a property ‘processed’ which you set to true after the item is processed by the cronjob.

Make sure you have a look at transactions and PHP script timeout.

Thanks i.amniels!

Why do you think having two tables is bad design? the queue_in could have a lot of records and not sure if filtering by processed property could affect to performance. Maybe I have to test doing this with two tables and only with one.