Methods for scheduling scheduled tasks in IP-PBX, IP-PBX and storage media
By using a red-black tree to manage the scheduled task queue and event notification mechanism in the IP-PBX, the problems of excessive thread usage and insufficient precision are solved, achieving efficient scheduled task management and monitoring.
Patent Information
- Application Number
- CN202310796360.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-30
- Publication Date
- 2026-11-17
- Estimated Expiration
- 2043-06-30
AI Technical Summary
The current method of handling scheduled tasks by third-party modules in IP-PBX results in excessive thread usage, wasted resources, high time complexity, inability to guarantee the accuracy of scheduled tasks, and a lack of unified management and monitoring.
Scheduled tasks are written to a scheduled task queue and managed using a timer container implemented with a red-black tree. The timers are sorted by their expiration time, and tasks are monitored and executed through an event notification mechanism to prevent tasks from monopolizing threads. The execution status of tasks is recorded for unified monitoring.
It reduces the thread usage of scheduled tasks, ensures the execution accuracy of tasks, avoids the accumulation of task exceptions, realizes unified management and monitoring of scheduled tasks, and improves system performance.
Smart Images

Figure CN116954854B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of communication technology, and in particular to a method for scheduling timed tasks in an IP-PBX, the IP-PBX, and a storage medium. Background Technology
[0002] IP-PBX is an enterprise telephone system based on Internet protocols. It can integrate multiple communication methods and supports new work styles such as remote and mobile work. IP-PBX can interface with various third-party services, such as Microsoft services, ZOHO, PMS, and WeChat. Interface with third-party services can be achieved by configuring a third-party integration module within the IP-PBX. The thread scheduling module within this module is specifically designed to schedule various tasks. It maintains a thread pool, prioritizes tasks according to their priority, and requests resources from the thread pool to schedule and execute the tasks.
[0003] Currently, the scheduled task processing of the IP-PBX third-party integration module is carried out by using dedicated threads. Each scheduled task of the third-party service is processed by a separate thread. If there are a large number of scheduled tasks in the third-party module, such as scheduled token refresh, scheduled synchronization of Teams status, heartbeat detection, etc., each scheduled task will occupy a dedicated thread and be blocked waiting to be executed, which will lead to an excessive number of threads and waste of performance and resources.
[0004] Furthermore, in third-party modules, it is necessary to detect when a scheduled task is triggered. This usually requires actively polling and then traversing the linked list to obtain the time of the scheduled task and compare it with the current time before deciding whether to execute the task. The worst-case time complexity can reach O(n) or higher. As the number of scheduled tasks increases, the impact of high time complexity on accuracy will become more and more significant, thus making it impossible to guarantee the accuracy of scheduled tasks.
[0005] Furthermore, the current third-party modules do not provide unified management and monitoring of the current scheduled tasks. Since the performance of different IP-PBX models varies, abnormal situations may occur. For example, a scheduled task may be executed every 10 seconds, but it may take 11 seconds to complete. This means that tasks are piling up, leading to anomalies, but the current scheduling scheme lacks control measures for this. Summary of the Invention
[0006] To address at least one of the technical problems of the prior art, embodiments of the present invention provide a method, apparatus, and storage medium for scheduling timed tasks in an IP-PBX.
[0007] To achieve the above objectives, a method for scheduling scheduled tasks in an IP-PBX is provided, characterized by comprising: writing all scheduled tasks into a scheduled task queue; adding all scheduled tasks in the scheduled task queue to a timer container, the timer container being implemented using a red-black tree, wherein each scheduled task corresponds to a node in the red-black tree according to its expiration time, and the expiration time of the scheduled tasks corresponding to the nodes in the red-black tree increases from left to right, wherein the earliest expiring scheduled task corresponds to the leftmost node in the red-black tree, and the latest expiring scheduled task corresponds to the rightmost node in the red-black tree; listening for blocking and waiting events, and when the scheduled task corresponding to the leftmost node in the red-black tree expires, the blocking is released; cyclically checking all nodes in the red-black tree from left to right to determine whether the scheduled task corresponding to the node has expired, and performing thread scheduling processing on the expired scheduled tasks.
[0008] Preferably, in the method, adding all timed tasks in the timed task queue to the timer container means adding all timed tasks sequentially to the timer container, wherein:
[0009] When adding a new timed task to the timer container, it is determined whether the expiration time of the new timed task is greater than the expiration time of the latest timed task in the timer container; if so, the new timed task is directly inserted into the rightmost node of the red-black tree; otherwise, the new timed task is inserted into the red-black tree according to the order of its expiration time.
[0010] Preferably, the method further includes:
[0011] When adding all timed tasks to the timer container in sequence, task IDs are assigned to each timed task according to the order of addition. When adding a new timed task to the timer container, if the expiration time of the new timed task is equal to the expiration time of an existing timed task in the timer container, the execution order of the two timed tasks is determined according to the task ID.
[0012] Preferably, in the method, writing all scheduled tasks into the scheduled task queue includes:
[0013] Write the task function and time interval of each timed task into the timed task queue.
[0014] Preferably, the method further includes, when the timed task expires: obtaining the execution time used in the last execution of the expired timed task; determining whether the execution time used in the last execution is greater than the time interval corresponding to the expired timed task; if so, determining that the timed task execution is abnormal, not executing the timed task this time, saving the abnormal information of the task, and adding the task back to the timer container according to the time interval, waiting for the next execution; if not, performing thread scheduling processing on the expired timed task.
[0015] Preferably, the method further includes, after determining that the scheduled task is abnormal, updating the execution time used in the previous execution by subtracting the current execution time for the scheduled task from the execution time used in the previous execution.
[0016] Preferably, the method further includes, for expired scheduled tasks:
[0017] The time interval of the timed task is used to determine whether the timed task is executed cyclically. If the time interval is not zero, the timed task is executed cyclically. If so, the timed task is added to the timer container according to the time interval and waits for the next execution.
[0018] Preferably, in the method, an event notification mechanism is used to listen for the blocking wait event.
[0019] On the other hand, an IP-PBX is provided, including a memory and a processor, the memory storing at least one program that is executed by the processor to implement any of the methods described above.
[0020] In another aspect, a computer-readable storage medium is provided, wherein the storage medium stores at least one program that is executed by a processor to implement any of the methods described above.
[0021] The above technical solution has the following technical effects:
[0022] The technical solution of this invention adds timed tasks to a timer container implemented by a red-black tree according to their expiration time. The earliest expiration time is set in the leftmost node of the red-black tree, and the latest expiration time is set in the rightmost node of the red-black tree. The expiration time of the leftmost node of the red-black tree is used to trigger a blocking release event. This can organize timed tasks according to time requirements, that is, according to the time priority of the tasks. The more urgent the time requirement, the higher the priority will be executed, ensuring that the tasks are completed before the deadline and guaranteeing the accuracy of the timed tasks.
[0023] Furthermore, in this embodiment of the invention, all scheduled tasks are added to a red-black tree, and the scheduled tasks in the red-black tree are detected by event triggering. Tasks that are due to be executed are handed over to the corresponding thread scheduling module for thread scheduling processing. Each scheduled task no longer occupies a thread exclusively, which greatly reduces the thread occupation of scheduled tasks.
[0024] Furthermore, embodiments of the present invention compare the previous execution time of the scheduled task with the time interval, and determine the scheduled task whose execution time exceeds the time interval as an abnormal task, and delay its execution by one time interval. This can prevent the abnormal accumulation of scheduled tasks, handle abnormal tasks in a timely manner, and avoid the occurrence of larger problems. Attached Figure Description
[0025] Figure 1 This is a flowchart illustrating a method for scheduling timed tasks in an IP-PBX according to an embodiment of the present invention.
[0026] Figure 2 This is a flowchart illustrating a method for scheduling timed tasks in an IP-PBX according to another embodiment of the present invention. Detailed Implementation
[0027] To further illustrate the various embodiments, the present invention provides accompanying drawings. These drawings are part of the disclosure of the present invention, primarily used to illustrate the embodiments and to explain the operating principles of the embodiments in conjunction with the relevant descriptions in the specification. With reference to these drawings, those skilled in the art should be able to understand other possible implementations and the advantages of the present invention. Components in the drawings are not drawn to scale, and similar component symbols are generally used to represent similar components.
[0028] The present invention will now be further described in conjunction with the accompanying drawings and specific embodiments.
[0029] Example 1:
[0030] Figure 1 This is a flowchart illustrating a method for scheduling timed tasks in an IP-PBX according to an embodiment of the present invention.
[0031] like Figure 1 The method for scheduling timed tasks in the IP-PBX of this embodiment includes the following steps:
[0032] Step S1: Write all scheduled tasks into the scheduled task queue;
[0033] Preferably, all the aforementioned scheduled tasks refer to all scheduled tasks or all potential scheduled tasks of third-party services set up in the IP-PBX; for example, the aforementioned third-party services include: all scheduled tasks or all potential scheduled tasks required by third-party services such as WeChat provided by companies such as Tencent, Google, ZOHO, PMS, and / or Microsoft. The scheduled tasks are written to the scheduled task queue to complete the initialization work. The aforementioned scheduled tasks include, for example, heartbeat detection, Teams status synchronization, CRM and / or HELPDESK token refreshers, etc. Preferably, writing the scheduled tasks to the scheduled task queue includes: writing the task function and timing interval corresponding to the scheduled task to the scheduled task queue.
[0034] Step S2: Add all timed tasks in the timed task queue to the timer container. The timer container is implemented using a red-black tree. Each timed task corresponds to a node in the red-black tree according to its expiration time. The expiration times of the timed tasks corresponding to the nodes in the red-black tree increase from left to right. The timed task that expires earliest corresponds to the leftmost node in the red-black tree, and the timed task that expires latest corresponds to the rightmost node in the red-black tree.
[0035] A red-black tree is a self-balancing binary search tree that uses an extra bit at each node to represent the node's color, which can be either red or black. It has the following properties: (1) Every node is either black or red; (2) The root node must be black; (3) Every leaf node is black; (4) If a node is red, then its children must be black; (5) Simple paths from any node to each of its leaves contain the same number of black nodes.
[0036] Step S3: Listen for blocking and waiting events. When the timed task corresponding to the leftmost node of the red-black tree expires, the blocking is released. Check all nodes of the red-black tree from left to right to determine whether the timed task corresponding to the node has expired. Perform thread scheduling processing on the expired timed task.
[0037] Preferably, the above-mentioned monitoring of blocking and waiting events is implemented through an event notification mechanism. In one specific implementation, after the scheduled tasks are added to the nodes of the red-black tree according to their timeout periods, the system's I / O model, such as the epoll model used in Linux systems, determines the timeout period for the event notification mechanism to wait for the event to trigger based on the timeout period of the leftmost node in the red-black tree. Before the timeout period of the leftmost node in the red-black tree arrives, the thread will block and release the CPU. When the timeout period of the leftmost node arrives, the blocking is released, and all tasks that need to be executed are checked and executed in the red-black tree nodes in a left-to-right order. By using this event notification mechanism to start scheduled tasks, the time complexity of adding, deleting, and searching in the red-black tree can all be O(logn). Therefore, when the number of scheduled tasks increases significantly, the overall system performance of the IP-PBX can still be maintained well.
[0038] In one specific implementation, the method for scheduling scheduled tasks according to embodiments of the present invention is applied to multiple scheduled tasks required by third-party services configured in an IP-PBX. IP-PBXs typically interface with third-party services configured in the IP-PBX through a third-party interface module implemented by a program. The method for scheduling scheduled tasks according to embodiments of the present invention can be implemented through this third-party interface module.
[0039] In a specific implementation, preferably, adding all timed tasks from the timed task queue to the timer container involves adding all timed tasks sequentially to the timer container, wherein:
[0040] In one specific implementation, when adding a new timed task to the timer container, it is determined whether the expiration time of the new timed task is greater than the expiration time of the latest timed task in the timer container; if so, the new timed task is directly inserted into the rightmost node of the red-black tree; otherwise, the new timed task is inserted into the red-black tree according to the order of its expiration time.
[0041] By comparing the expiration time of the new timer task to be added to the timer container with the expiration time of the latest red-black tree node in the timer container, and if the expiration time of the new timer task is greater than the expiration time of the latest red-black tree node (i.e., the rightmost node in the red-black tree), the new timer task is directly inserted into the end of the container to become the rightmost node in the red-black tree. This optimizes the time complexity of the insertion operation, allowing the insertion operation to be completed in constant time without the need for searching and rotating the red-black tree.
[0042] If the timer container is empty or the expiration time of the new timer task is less than the expiration time of the last timer in the current container, then the new timer task is inserted into the timer container implemented by the red-black tree according to the expiration time. The timer tasks in the timer container are maintained in ascending order of expiration time.
[0043] Example 2:
[0044] Figure 2 This is a flowchart illustrating a method for scheduling timed tasks in an IP-PBX according to another embodiment of the present invention. Figure 2 The method in this embodiment includes the following steps:
[0045] (1) The process corresponding to the third-party module starts and initializes the timed tasks required by the third-party module; the third-party module is the program module used to implement third-party services in the IP-PBX.
[0046] (2) During initialization, the timed tasks required for the third-party module to run are written into the timed task queue. The timed task queue is managed by the program module used to implement timed task management. The information written into the task queue includes the task function of the timed task to be executed and the time interval. Preferably, the information written into the task queue also includes the priority of the task. This priority information can be used by the corresponding program module used to implement thread scheduling management when performing thread scheduling.
[0047] (3) After initialization, the timed tasks in the task queue are added to the timer container implemented by the red-black tree in turn, and during the addition process, it is determined whether the expiration time of the newly added timed task is greater than the expiration time of the last timed task in the current container.
[0048] (4) If the expiration time of the new timed task is greater than the expiration time of the last timed task in the current container, the new timed task is directly inserted into the end of the timer container, that is, inserted into the rightmost node of the red-black tree; this method of insertion makes the time complexity of the method in this embodiment reach O(1).
[0049] If the expiration time of the new scheduled task is not greater than the expiration time of the last scheduled task in the current container, it is inserted into the appropriate position in the timer container according to its expiration time, that is, the expiration time of the scheduled tasks in the timer container increases from left to right according to the red-black tree nodes. If the expiration time of the new scheduled task is equal to the expiration time of an existing scheduled task in the timer container, the execution order of the scheduled tasks is determined by the size of the task ID. Specifically, when adding all scheduled tasks to the timer container sequentially, task IDs are assigned to each scheduled task according to the order of addition. In one implementation, task IDs can be assigned to each scheduled task in an incremental manner, with the earliest added scheduled task having the smallest ID. In another implementation, for example, the order of execution of scheduled tasks based on their IDs is determined as follows: the smaller the ID, the earlier it is executed.
[0050] (5) After all the timed tasks are inserted, the earliest timed task that expires is located in the leftmost node of the timer container, i.e., the red-black tree.
[0051] (6) Obtain the expiration time of the leftmost node and calculate the timeout waiting time t_wait for the event trigger, where t_wait = the expiration time of the leftmost node - the current time.
[0052] (7) Utilize the event notification mechanism provided by the system, such as the epoll mechanism in the Linux system, to enable event blocking and waiting, and use t_wait as the timeout parameter for blocking and waiting.
[0053] (8) When the timeout period is reached, the blockage is released.
[0054] (9) Loop through the nodes of the timer container, i.e. the red-black tree, from left to right, and determine whether the timed task corresponding to the node has reached its execution time, i.e. whether the current time is greater than or equal to the task's expiration time; if so, determine that the corresponding timed task has reached its execution time.
[0055] (10) If a scheduled task has reached its execution time, retrieve the last execution time of the scheduled task from the scheduled task queue, i.e., the execution time spent when the scheduled task was executed last time. Determine whether the last execution time is greater than the time interval. If the last execution time is greater than the time interval, it is determined that the scheduled task is abnormal. The scheduled task will not be executed this time. Save the abnormal information of the task and add the task back to the timer container according to the time interval, waiting for the next execution.
[0056] Furthermore, when the previous execution time is greater than the time interval, the process also includes: updating the previous task execution time to the previous task execution time minus the current execution time for the task, i.e., the time the system has spent on the task this time. In other words, the variable value used to record the previous task execution time is updated to the above value; where, if the updated time value is less than 0, it is set to 0.
[0057] After storing the information related to the scheduled task, such as writing it into the database, return to step (9) to determine the next scheduled task.
[0058] In this step, if the execution time of the previous scheduled task is less than the time interval, the specific task callback function and priority of the scheduled task are handed over to the thread scheduler module used to implement thread scheduling. The thread scheduler module allocates threads and executes the scheduled task. After the scheduled task is completed, the task execution time is updated to the scheduled task queue. Preferably, it is further determined whether the scheduled task is to be executed repeatedly; if so, the task is added back to the timer container according to the time interval; finally, the scheduled task currently handed over to the thread scheduler module is removed from the timer container; then, return to step (9) and continue to determine the next node. In one implementation, it is determined whether the scheduled task needs to be executed repeatedly by judging whether the time interval of the scheduled task is zero; if it is zero, it is not to be executed repeatedly, and it is not necessary to add the scheduled task back to the timer container.
[0059] (11) If there are no tasks that have reached their execution time, return to step (6) and retrieve the earliest expired task, i.e., the leftmost node of the red-black tree, based on all the timed tasks that have been inserted and completed in the current timer container.
[0060] Using the method of this invention, the execution status of all scheduled tasks can be recorded, including the resources consumed by all scheduled tasks, thereby enabling unified monitoring and management of all scheduled tasks of all scheduled services, such as third-party services.
[0061] Preferably, the scheduled task in this embodiment of the invention is a pre-determined scheduled task; during the implementation of the method in this embodiment of the invention, information related to the execution of the scheduled task is recorded in the database, including the number of executions, the duration of each execution, and any abnormal situations; this information can be synchronously analyzed and viewed by the front end, so that the running status of these scheduled tasks executed in the IP-PBX, including running abnormalities and resource consumption, can be handled in a timely manner and maintained regularly, which facilitates the monitoring and management of the operation of third-party programs in the IP-PBX.
[0062] Example 3:
[0063] The present invention also provides an IP-PBX, including a memory and a processor, wherein the memory stores at least one program, which is executed by the processor to perform the steps as described in any of the above method embodiments.
[0064] As an executable solution, the processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor can be a microprocessor or any conventional processor. The processor is the control center of the computer unit, connecting various parts of the entire computer unit via various interfaces and lines.
[0065] The memory can be used to store the computer programs and / or modules. The processor implements various functions of the computer unit by running or executing the computer programs and / or modules stored in the memory and by calling data stored in the memory. The memory may mainly include a program storage area and a data storage area. The program storage area may store the operating system and at least one application program required for a function; the data storage area may store data created based on the use of the mobile phone, etc. In addition, the memory may include high-speed random access memory and may also include non-volatile memory, such as hard disk, RAM, plug-in hard disk, smart media card (SMC), secure digital card (SD), flash card, at least one disk storage device, flash memory device, or other volatile solid-state storage device.
[0066] Example 4:
[0067] The present invention also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of the method described in the embodiments of the present invention.
[0068] If the modules / units integrated in the computer unit are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, all or part of the processes in the methods of the above embodiments can also be implemented by a computer program instructing related hardware. The computer program can be stored in a computer-readable storage medium, and when executed by a processor, it can implement the steps of the various method embodiments described above. The computer program includes computer program code, which can be in the form of source code, object code, executable files, or certain intermediate forms. The computer-readable medium can include: any entity or device capable of carrying the computer program code, recording media, USB flash drives, portable hard drives, magnetic disks, optical disks, computer memory, read-only memory (ROM), random access memory (RAM), and software distribution media, etc. It should be noted that the content contained in the computer-readable medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction.
[0069] Although the invention has been specifically shown and described in conjunction with preferred embodiments, those skilled in the art should understand that various changes in form and detail may be made to the invention without departing from the spirit and scope of the invention as defined in the appended claims, all of which shall be within the scope of protection of the invention.
Claims
1. A method for scheduling a timing task in an IP-PBX, characterized in that, include: Write all scheduled tasks to the scheduled task queue; All timed tasks in the timed task queue are added to a timer container, which is implemented using a red-black tree. Each timed task corresponds to a node in the red-black tree according to its expiration time. The expiration times of the timed tasks corresponding to the nodes in the red-black tree increase from left to right. The timed task that expires earliest corresponds to the leftmost node in the red-black tree, and the timed task that expires latest corresponds to the rightmost node in the red-black tree. Listen for blocking and waiting events. When the timed task corresponding to the leftmost node of the red-black tree expires, the blocking is released. Detect all nodes of the red-black tree from left to right, determine whether the timed task corresponding to the node has expired, and perform thread scheduling processing on the expired timed task.
2. The method according to claim 1, characterized in that, Add all scheduled tasks to the timer container in sequence, where: When adding a new timed task to the timer container, it is determined whether the expiration time of the new timed task is greater than the expiration time of the latest timed task in the timer container; if it is greater, the new timed task is directly inserted into the rightmost node of the red-black tree; if it is less, the new timed task is inserted into the red-black tree according to the order of its expiration time.
3. The method according to claim 2, characterized in that, Also includes: When adding all timed tasks to the timer container in sequence, task IDs are assigned to each timed task in ascending order of addition, with the earliest added timed task having the smallest ID. When adding a new timed task to the timer container, if the expiration time of the new timed task is equal to the expiration time of an existing timed task in the timer container, the execution order of the two timed tasks is determined according to the size of their task IDs, with the smaller ID being executed first.
4. The method according to claim 1, characterized in that, Writing all scheduled tasks to the scheduled task queue includes: Write the task function and time interval of each timed task into the timed task queue.
5. The method according to claim 1, characterized in that, When the scheduled task expires, it also includes: Get the execution time used when the expired scheduled task was last executed; Determine whether the execution time used in the previous execution is greater than the time interval corresponding to the expired timed task; if so, determine that the timed task execution is abnormal, do not execute the timed task this time, save the abnormal information of the task, and add the task back to the timer container according to the time interval, waiting for the next execution; if not, perform thread scheduling processing on the expired timed task.
6. The method according to claim 1, characterized in that, Overdue scheduled tasks also include: The time interval of the timed task is used to determine whether the timed task is executed cyclically. If the time interval is not zero, the timed task is executed cyclically. If so, the timed task is added to the timer container according to the time interval and waits for the next execution.
7. The method according to claim 1, characterized in that, Use an event notification mechanism to listen for the blocking wait event.
8. An IP-PBX, characterized in that, It includes a memory and a processor, the memory storing at least one program, the at least one program being executed by the processor to implement the method as claimed in any one of claims 1 to 7.
9. A computer-readable storage medium, characterized in that, The storage medium stores at least one program segment, which is executed by a processor to implement the method as described in any one of claims 1 to 7.
Citation Information
Patent Citations
Timer task processing method, software timer and storage medium
CN115525402A
Multi-terminal call switching method, device and system and storage medium
CN116170419A
