Continous Loop or Daemon from external application ? Can you please suggest

I have a requirement as follows
Every day say @ 6 AM Local time I have to run a php function (within scriptcase as a php method) . This function retrieves and calculates new shift times for a manufacturing company, This company has shift times as 1st Shift 6 AM to 2 PM 2nd Shift 2 PM to 10 PM and 3rd Shift 10PM to 6 AM (next Day).
Within each shift I have to perform various operations. And this has to happen every hour . For example under 1 st ,Shift @ 6 AM, then 7 AM , then 8 AM and so on.
There are no external triggers for evens or the hourly actions or shift change actions @ every 8th hour completion.
Example :
6AM, 7 AM, 8 AM ??. (Every hour) Do some calculations and push to client
2 PM (Shift change) Refresh client with new shift details. Reset calculations and start new calculations.
6 AM Next Day : Calculate hew shift cycle start (which happens after every 3rd shift)
The timings can be changed. There is a shift master table . So these should be configurable.
I have read all over the net that continuous loops can create lots of issues in a continuous running application on PHP. I have been a C# developer all along. New to PHP. BTB, Please note that the Production System will be a Windows System with Apache, PHP and not Linux. Can somebody tell me if there is any way to handle this under scriptcase. My version is SC 9.03. Thanks

I’ve done done a similar, but simpler scheduled tasks in scriptcase. It is done in linux via Chrontab, but is the same procedure with the schedule task in windows (server or professional)

Steps:

  1. In scriptcase, make a blank application, with a on_load event, that in turn calls your php function. This function must do all the calculation, selects, inserts, updates an so on. The parameters of the function must be inside scriptcase itself, or by a special table in the DDBB. The application must not have security (scriptcase user and password). If you have any concern of misuse of this application, you can put it in a hidden URL, or configure apache to be called only by localhost.
  2. Compile and test the blank application. It must run without user inputs or intervention.
  3. In the task scheduler in windows add a new entry, setting the time to run, and using the full URL of the blank applications as a target. At the set time of the day, windows will call the blank app.

Don’t do a infinite loop!

jomscl: Thanks for the reply. I honestly an thankful for your effort in replying back.
I am not so good with PHP , still learning. This is going to be in windows environment. Your solution seems good but for one catch. The hourly updates and end of third shift are not fixed. Let me explain a bit more. For example as of now the shift starts at 6 AM today and ends at 6 AM today , 3 shifts totally of eight hours each. Since it starts at 6 AM so hourly updates will be 7, 8, 9 and so on. After a few months the shift timings are changed. That is from 6:30 AM onwards . the last shift ends at 6:30 next day. Also the hourly updates should occur at 7:30, 8:30 and 9:30. We can not ask the end user to make changes in the scheduler . This should be done from within PHP (scriptcase) as this the application for CRUD of the shift master. This is where I am getting stuck.
Anyway, thank you very much. Probably I took up a project more than I can chew. This project involves MQTT also. PHP is good for publish but not suitable for subscribe part of MQTT. I am looking at one option which is to develop a deamon in C# which will run as windows service. This will contact Scriptcase Blank Application using REST API. So SC will send the shift timing to the C# program. C# will monitor and send the new day start and 3rd shift end to SC. Also, C# will handle the MQTT subscribe part an use the SC blank REST API to insert the MQTT topics in MySQL. honestly SC is good in many many ways. It has made the development a breeze though I am still learning. But there are a few issues like the sc_exec_sql and attributes under Programming(above internal libraries and PHP methods ) option. I am trying to understand these two. sc_exec_sql does not offer any way to trap errors . And Attributes is still eluding me. Sorry for the long post. thanks once again.

What you’re looking for is timed trigger that’ll fire every [specified period]; that’s a cronjob. Windows undoubtedly has a myriad of applications that’ll handle that, it comes built in with every *NIX OS and pretty much every hosting service has got it somewhere in their plesk/directadmin/hosting environment setup. Cronjobs are a bit like regular expressions; absolutely logical, unforgiving and a total $^#%$ to wrap you head around. Yet; they do exactly what you want. Just do “something” (i.e. executing a certain script) at very specific intervals.

I’m on MacOS, so I can’t be of any assistance as far as Windows goes - but with a bit of browsing you should be able to find an app that does the job. It is however entirely possible and absolutely no strange idea / request. Yet; it’s well beyond the scope of Scriptcase itself - as it needs user input in order to do something.

Setting up one cron to run every minute (or 15 mins etc) is the best way - and not too hard. Lots of online tutorials for it. But once you have then you want a blank app to start something like this

$now = date('Y-m-d H:i:s ', time()); // Current time when the app is triggered
$sql = “SELECT id, task, triggered FROM schedule” . " WHERE Date < ‘" . $now . "’" . " AND triggered = 0 "; //find all untriggered tasks that are now in the past. Often this will be none.
sc_lookup(rs, $sql);

$i = 0; // loop through all the tasks (there many be several set to the same time)
while (isset({rs[$i][0]}))
{
$id = {rs[$i][0]};
$task = {rs[$i][1]};
$triggered = {rs[$i][2]};

// This allows you to fill a database with all sort of tasks (rather than set a complex series of cron jobs up) at all sorts of time intervals. Fill the db several years ahead if you need to maybe importing an excel spreadsheet into mysql.

// The triggered = 0 double ensures that it only gets triggered once.

// So you set triggered to 1 when it has been executed.

$sql3 = “UPDATE schedule SET triggered = 1 WHERE id = '” . $id . “’”;
sc_exec_sql($sql3);
sc_commit_trans(); // Probably not required but best to have it when you are looping v quickly.

// Then for each of your results in the SELECT write a function to take care of what happens.

function task($task){do something here};//define task
task($task); //execute task

$i++;
} // finish the loop for each task at that time.

Basically that is a simply blank app that will respond to a cron job. The function you define can be as complex as you like. And there can be lots of them using various “ifs”. But at least the “when” part is taken care of.

@charlesfairbairn Follow up question to this… great info here!

Anyone have experience on the best way to call the Blank application that handles all this? Using curl or wget works, but calling PHP and the script directly does not work. a la “php /home/public_html/MyScript/MyScript.php”… if I run the command from terminal I see a screen full of CSS… but it never hits the database and runs any of the jobs.

The reason I would like to get away from wget or curl from the command line is that I have a lot of jobs running and some jobs take longer than others that I need to run more often. The slow ones have to complete before it can move on to the faster jobs. I need a way to run multiple jobs at the same time without them waiting on each other.

Any tips from anyone in this situation?