rvkvino's avatar

Need to create a service to execute my method every seconds

I have created a function in my class, I need to execute this method for every second. This service need to be run as background service to update something to users. My scenario, I have an application like stock market. I'm receiving the base rates from one external service. I have parsed this base rate and stored into redis. When ever base rates get change this will update in redis. So redis holding the base rates. Users will set the rate alert like if the stock rate hit 3030 need an notification to their mobile. So I nee to check in this service If rates hit client requested rate I need to send one process to queue, queue will send sms and notification to client. Now I have make ready my method now I need to run this method as background service in lumen api.

0 likes
5 replies
Yorki's avatar

And why don't you queue job after receiving base rates from external service to check them?

rvkvino's avatar

@Yorki For every second base rate receives from external service, I have to update this rates into redis and I have to give response as soon as updated into redis. If I post to queue job means I need to create queue for every second and it get increase the queues. Could you please explain some more how this will be possible in this. I think you are saying whenever the external service hitting my method with base rate means I need to store into redis and need to call my logic method in queue right. It won't cause any issue like time our or time delay.

Yorki's avatar

I don't think that using PHP in that case is a good idea. You should go for persistent server application like nodejs or java. I mean you can go with PHP if you really want, but sooner or later you will realize that was wrong decision. However you could do cron job which spawn process each second to process your method.

rvkvino's avatar

@Yorki Could you please give me any sample code for run for every second in cron because I could create only for every min, couldn't create for ever sec.

Yorki's avatar
for ($i = 0; $i < 59; $i++) {
    exec('php path-to-your-app/artisan process:stock > /dev/null 2>&1 &');
    sleep(1); //or time_sleep_until
}

Please or to participate in this conversation.