Add a Cron or Scheduled task that executes at your lowest common interval (5 minutes? 1 minute?).
The scheduled task should query a database table that holds info on the frequency of the user's task. I would express frequency in seconds, then use a UNIX timestamp to determine eligibility for execution. The computation is then somewhat easy:
- Find the current time, rounded down to nearest common interval (assuming a 60-second interval): NOW = TIMESTAMP - (TIMESTAMP % 60)
- Find users whose interval is an even divisor of the current time period
The table could look like this:
CREATE TABLE `scheduled_tasks` (
`id` int(7) unsigned NOT NULL auto_increment,
`user_id` int(7) unsigned NOT NULL,
`task` varchar(32) NOT NULL,
`interval` int(10) NOT NULL,
`date_start` date NOT NULL default '0000-00-00',
`date_end` date NOT NULL default '2030-12-31', -- Hopefully you won't be still maintaining this app
PRIMARY KEY (`id`)
) ;
INSERT INTO `scheduled_tasks` (user_id, task, interval, date_start, date_end )
VALUES
(1, 'standard_job', 5*60, '2009-06-01', '2009-08-01'),
(2, 'standard_job', 10*60, CURDATE(), CURDATE() ),
(3, 'standard_job', 24*60*60, '2009-06-01', '2009-08-01')
To find jobs to run, this query could work:
SELECT `user_id`, `task`
FROM `scheduled_tasks`
WHERE (UNIX_TIMESTAMP() - (UNIX_TIMESTAMP() % 60 ) ) % `interval` = 0
AND CURDATE() BETWEEN `date_start` AND `date_end`
That comes back with a list of users/tasks to run. You can add multiple entries per user if necessary.