Hosting & Domaining Forum

Hosting Discussion => Managed Hosting and Services => Topic started by: dtsystems on Feb 10, 2023, 08:31 AM

Title: Limitation of script execution time on webhosting
Post by: dtsystems on Feb 10, 2023, 08:31 AM
I require a script to run for an exact three-minute duration, but the script has elements of Sleep and System that would prevent it from running for the full three minutes if limits were set on the script. Though there are options to set limits on the script via max_execution_time and set_time_limit(), including Sleep, System, and HTTP requests in the restriction would mean that the script would not run for three minutes. Altering the ngnix settings on the hosting is not an option. One solution I am considering is running the script through ajax with a set timeout, but once the timeout is released, the script continues to run. In this case, I plan to run the task via Cron.

while(true){

"script body"

sleep(10);
}


Title: Re: Limitation of script execution time on hosting
Post by: maxi007 on Feb 10, 2023, 11:14 AM
When operating in console mode, the user has access to various features, one of which is unlimited execution time by default. During each iteration of the program, the time should be checked against the maximum allowed execution time (max_execution_time()), subtracting 10 to ensure that the program does not exceed the limit by running until the very last second.
Title: Re: Limitation of script execution time on hosting
Post by: Abhinavjain on Feb 10, 2023, 12:09 PM
Using a timer in this way will not call ajax every three minutes exactly, as the timer will begin to experience delays over time and may even stop functioning if the browser tab is left inactive.
If precision is crucial, it is necessary to write the timer differently and disable the browser's "falling asleep" feature.
Title: Re: Limitation of script execution time on hosting
Post by: Nidhibng on May 04, 2023, 02:42 AM
When trying to use a timer to execute Ajax every three minutes, it should be kept in mind that the timer may not work precisely. There may be delays over time or the timer may stop entirely if the browser tab is left inactive. If precision is of critical importance, the timer code should be written differently and any browser features that could interfere with its accuracy must be disabled.
Title: Re: Limitation of script execution time on webhosting
Post by: danban on May 23, 2024, 09:46 AM
The use of sleep and system calls within the script introduces potential setbacks in achieving the desired three-minute runtime. These operations can contribute to the unpredictability of script execution, making it challenging to ensure strict timing control. Additionally, synchronous operations, such as HTTP requests, can further impact the script's ability to run for the full three minutes without interruption.

One approach to tackle this challenge is to break down the script into smaller, manageable units of work that can be executed within shorter time frames. By modularizing the script, you can create distinct segments of the task and schedule them to run at specific intervals using a cron job. This approach effectively simulates the three-minute duration by distributing the workload across multiple script invocations.

Furthermore, you can implement a mechanism to track the progress and continuity of the task by persisting relevant information in a data store, such as a database or a file. This ensures that the execution of the script is seamless and uninterrupted, even when it spans multiple iterations.

To address the limitations posed by sleep and system calls, it's essential to optimize the script itself by minimizing potentially blocking operations and leveraging asynchronous patterns where applicable. For example, you may consider using non-blocking I/O for handling HTTP requests and employing event-driven programming paradigms to enhance the efficiency of the script.

In addition to modularization and optimization, you can explore the use of server-side technologies, such as messaging queues, to orchestrate the execution of the script segments and coordinate their timing in a distributed manner. This approach can introduce scalability and fault tolerance, allowing the script to be run for the exact duration while overcoming potential disruptions caused by sleep and system calls.

By combining modularization, cron scheduling, script optimization, and server-side coordination, you can address the challenge of running a script for an exact three-minute duration in a robust and comprehensive manner. This multi-faceted approach aligns with best practices in software engineering and backend development, providing a strategic and scalable solution to the complexities posed by the limitations of sleep and system calls.