mysql - PHP Laravel Task Scheduling Best Practice -
i building simple application schedule ad set enabled / disabled based on time user inputs.
for example, user may want have ads run 6 - 9 , 5 pm - 10 pm , off @ other times.
what best way schedule this? store of data in mysql, should have cron/task checks every minute rows in table match time , function enable/disable?
to continue example, table might contain columns, time, function.
6 am, enable 9 am, disable 5 pm, enable 10 pm, disable
my question if have 10,000 users or so, web server or there more efficient way this?
in below approach, doing is...
- there 2 tables -
ads
,ad_timings
saves different start_time , end_time each ad... - where saving start_time 0600 , end_time 0900. so, you'll check if current time (say,
2016-23-12 06:50:11
)... convert0650
. - now find out ads start_time less 650 , end_time more find out active ads , reverse ads stop.
- run every 10 mins... give each user minimum input time interval of 10 mins... way run cron every 10 mins , save memory in background....
your tables
ad id | name | current_status | .... 1 | ... | 0 | ..... ad_timings id | ad_id | start_time | end_time 1 | 1 | 600 | 900 1 | 1 | 1700 | 2200
your models
class ad extends model { public function timings() { return $this->hasmany('app\models\adtimings'); } } class adtimings extends model { protected $table = 'ad_timings'; public function ad() { return $this->belongsto('app\models\ad') } }
in scheduler
use carbon\carbon; use app\models\ad; class adscheduler { public function handle() { $now = carbon::now(); // convert current timestamp // timestamp: 2016-12-23 23:36:11 // // now: 2336 // calculating time on basis of hundreds.. // people say... 1300 hours... me? $now = $now->hour . $now->minute; // these ads run // can change current_status field 1 update $adstorun = ad::wherehas('timings', function($query) use ($now) { return $query->where('start_time', '<=', $now) ->where('end_time', '>=', $now) })->get(); // ads stop // can change current_status field 0 update $adstostop = ad::wherehas('timings', function($query) use ($now) { return $query->where('start_time', '>=', $now) ->where('end_time', '<=', $now) })->get(); } }
Comments
Post a Comment