Task processing method and device, electronic equipment and computer readable storage medium
By dividing the task queue into a first queue and a second queue, and moving and processing tasks according to their execution time, the problem of low task processing efficiency in existing technologies is solved, and efficient task processing is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-07-22
- Publication Date
- 2026-03-24
AI Technical Summary
The existing task queue design suffers from a sharp decrease in data record access and update performance when handling a large number of delayed, time-triggered tasks due to the polling mechanism, resulting in low efficiency in business data processing.
The task queue is divided into a first queue and a second queue. The first queue stores tasks whose execution time is greater than the current time by a set time. The second queue stores tasks whose time difference is less than or equal to the set time. Tasks in the first queue whose time difference is less than or equal to the set time are moved to the second queue by monitoring them. Tasks in the second queue are then processed according to their execution time.
It reduced the amount of scanned data, solved the problem of untimely triggering of scheduled tasks, and improved task processing efficiency.
Smart Images

Figure CN114625494B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and more specifically, to a task processing method, apparatus, electronic device, and computer-readable storage medium. Background Technology
[0002] Existing communication services employ numerous task queue designs. A common task queue design involves sending instant messages to a queue, where a backend task handler retrieves and processes them. These task queues are designed in various ways, including distributed processing and storage methods.
[0003] Currently, for tasks requiring delayed, time-delayed triggering, the common design is to write the events to be delayed into a task queue or database table. The backend task handler then continuously polls and scans the delayed data before executing it at the designated time. However, this approach suffers from a significant performance degradation when dealing with very large datasets due to the simplistic polling mechanism and the resulting slowdown in data access and updates, leading to low data processing efficiency for the business. Summary of the Invention
[0004] This application provides a task processing method, apparatus, electronic device, and computer-readable storage medium to solve the problem of low efficiency in timed task processing.
[0005] In a first aspect, embodiments of this application provide a task processing method, the method comprising:
[0006] Scan the first pending task in the first queue. If there is a pending task in the first queue whose execution time is less than or equal to the current time, then move the pending task from the first queue to the second queue.
[0007] The first queue is used to store the first task to be processed that meets the first condition, and the second queue is used to store the second task to be processed that meets the second condition. Meeting the first condition means that the task to be processed has a first time difference greater than a set time, and meeting the second condition means that the task to be processed has a first time difference less than or equal to a set time. The first time difference is the time difference between the task execution time and the task acquisition time.
[0008] Monitor the second pending tasks in the second queue and process them according to their respective execution times.
[0009] Secondly, embodiments of this application provide a task processing apparatus, which includes:
[0010] The task monitoring module is used to scan the first pending task in the first queue. If there is a pending task in the first queue whose execution time is less than or equal to the current time, the pending task will be moved from the first queue to the second queue.
[0011] The first queue is used to store the first task to be processed that meets the first condition, and the second queue is used to store the second task to be processed that meets the second condition. Meeting the first condition means that the task to be processed has a first time difference greater than a set time, and meeting the second condition means that the task to be processed has a first time difference less than or equal to a set time. The first time difference is the time difference between the task execution time and the task acquisition time.
[0012] The task processing module is used to monitor the second pending tasks in the second queue and process the second pending tasks in the second queue according to their respective task execution times.
[0013] Thirdly, embodiments of this application provide an electronic device, which includes:
[0014] Processor; and
[0015] The memory is configured to store machine-readable instructions that, when executed by a processor, cause the processor to perform the task processing method described in the first aspect above.
[0016] Fourthly, embodiments of this application provide a computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the task processing method described in the first aspect above.
[0017] The beneficial effects of the technical solutions provided in this application are:
[0018] Tasks with an execution time difference greater than a set duration are stored in a first queue, while those with a difference less than or equal to the set duration are stored in a second queue. The first queue is monitored, and tasks with this difference are moved to the second queue. The second queue is also monitored, and tasks are processed according to their execution times. In other words, tasks are added to different queues based on their execution times. The system scans the first queue to determine if tasks need to be moved to the second queue. During polling, only tasks in the second queue are scanned, reducing the amount of data scanned. This solves the problems of untimely task triggering and low data processing efficiency, ultimately improving task processing performance and efficiency. Attached Figure Description
[0019] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments of this application will be briefly introduced below.
[0020] Figure 1 A flowchart illustrating a task processing method provided in an embodiment of this application;
[0021] Figure 2 A flowchart illustrating another task processing method provided in an embodiment of this application;
[0022] Figure 3 A schematic diagram of a second queue including multiple sub-queues provided for embodiments of this application;
[0023] Figure 4 A flowchart illustrating another task processing method provided in an embodiment of this application;
[0024] Figure 5 A flowchart illustrating another task processing method provided in an embodiment of this application;
[0025] Figure 6 A flowchart illustrating yet another task processing method provided in an embodiment of this application;
[0026] Figure 7 A flowchart illustrating yet another task processing method provided in an embodiment of this application;
[0027] Figure 8 A schematic diagram illustrating an application scenario of the task processing method provided in the embodiments of this application;
[0028] Figure 9This is a schematic diagram of the structure of a task processing device provided in an embodiment of this application;
[0029] Figure 10 This is a schematic diagram of another task processing device provided in an embodiment of this application;
[0030] Figure 11 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0031] The embodiments of this application are described in detail below. Examples of these embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain this application, and should not be construed as limiting the invention.
[0032] Those skilled in the art will understand that, unless specifically stated otherwise, the singular forms “a,” “an,” “the,” and “the” used herein may also include the plural forms. It should be further understood that the term “comprising” as used in this application means the presence of the stated features, integers, steps, operations, elements, and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and / or groups thereof. It should be understood that when we say an element is “connected” or “coupled” to another element, it can be directly connected or coupled to the other element, or there may be intermediate elements. Furthermore, “connected” or “coupled” as used herein can include wireless connections or wireless coupling. The term “and / or” as used herein includes all or any units and all combinations of one or more associated listed items.
[0033] The technical solution of this application and how the technical solution of this application solves the above-mentioned technical problems are described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments. The embodiments of this application will now be described with reference to the accompanying drawings.
[0034] Figure 1 This is a flowchart illustrating a task processing method provided in an embodiment of this application. Figure 1 As shown, the method can be executed by any electronic device, and optionally by a server. The method includes the following steps S101 and S102.
[0035] S101: Scan the first pending task in the first queue. If there is a pending task in the first queue whose execution time is less than or equal to the current time, move the pending task from the first queue to the second queue.
[0036] The first queue (also known as the limit queue) stores the first pending task that meets the first condition, and the second queue (also known as the normal queue) stores the second pending task that meets the second condition. Meeting the first condition means that the first time difference is greater than a set duration, and meeting the second condition means that the first time difference is less than or equal to the set duration. The first time difference refers to the time difference between the task execution time and the task acquisition time.
[0037] In this process, the queue for storing tasks to be processed can be pre-divided into a first queue and a second queue. It is understood that the tasks to be processed can be different in different application scenarios. This embodiment does not limit the specific nature of the tasks to be processed; for example, a task to be processed could be a timed reminder task.
[0038] In this embodiment of the application, the tasks to be processed stored in the first queue and the second queue can also be called delayed-triggered tasks, that is, the tasks to be processed may not be tasks that need to be processed in real time.
[0039] Optionally, for a task to be processed, the task execution time refers to the time when the task will be triggered, that is, the time when the task will be executed. For ease of description, the task execution time can be represented as T. deal Task acquisition time refers to the time it takes to acquire the task, that is, the time it takes for the electronic device to acquire (e.g., receive) the task, which can be expressed as T. obtain In this context, if the task to be processed is received from another device, the task acquisition time can refer to the time when the task sender sends the task, or the time when the task receiver receives the task, such as the system time of the electronic device when it receives the task. The current time refers to the current system time of the electronic device, which can be represented as T. now It is understandable that the current time is a changing time; it is different at different moments.
[0040] Optionally, assuming the above-mentioned duration is Δt, where Δt is a positive number and the time can be in seconds (s), the first queue stores the task execution time T. deal With task acquisition time T obtain The first queue contains tasks with a time difference greater than Δt. The second queue stores tasks with execution times T. deal With task acquisition time T obtain The tasks to be processed whose first time difference is less than or equal to Δt are designated as pending tasks. For ease of description, in this embodiment, the pending tasks stored in the first queue are referred to as first pending tasks, and the pending tasks stored in the second queue are referred to as second pending tasks.
[0041] In this embodiment, after each task to be processed is acquired, it is determined whether to store the task in the first queue or the second queue based on the acquisition time (i.e., the current time when the task was acquired) and the task execution time. Furthermore, it is determined whether to move any first tasks to be processed in the first queue to the second queue by scanning each first task to be processed in the first queue. Specifically, if there are tasks with execution time T in the first queue... deal With current time T now If a task has a first time difference less than or equal to a set duration Δt, then the task is moved from the first queue to the second queue and removed from the first queue.
[0042] Optionally, scanning the first pending task in the first queue can include: real-time scanning or scanning the first pending task in the first queue at a set period. The set period can be the system default or set by the user as needed.
[0043] Optionally, in practical applications, the process of moving tasks from the first queue to the second queue can be implemented by configuring the corresponding queue management interface.
[0044] S102: Monitor the second pending tasks in the second queue, and process the second pending tasks in the second queue according to the task execution time of each second pending task.
[0045] Optionally, the second pending tasks in the second queue are monitored, and the second pending tasks in the second queue are processed accordingly according to the task execution time of each second pending task in the second queue. This includes removing the second pending task whose task processing time has arrived from the second queue (also known as dequeuing) and processing the pending task according to the type and content of the pending task.
[0046] For example, if the pending task is a reminder, a reminder message will be sent to the client. If the pending task is an execution task, the corresponding operation will be performed based on the content of the pending task.
[0047] Tasks with an execution time difference greater than a set duration are stored in a first queue, while those with a difference less than or equal to the set duration are stored in a second queue. The first queue is monitored, and tasks with this difference are moved to the second queue. The second queue is also monitored, and tasks are processed according to their execution times. In other words, tasks are added to different queues based on their execution times. The system scans the first queue to determine if tasks need to be moved to the second queue. During polling, only tasks in the second queue are scanned, reducing the amount of data scanned. This solves the problems of untimely task triggering and low data processing efficiency, ultimately improving task processing performance and efficiency.
[0048] Figure 2 This is a flowchart illustrating another task processing method provided in an embodiment of this application. Figure 2 As shown, the task processing method may include the following steps S201, S202, and S203:
[0049] S201: Obtain a task processing request, which includes at least one third task to be processed and the task execution time of each third task to be processed.
[0050] This embodiment describes a scheme for adding new tasks to a queue when they are acquired.
[0051] Optionally, embodiments of this application may determine the system's current time T when a task processing request is received. now The task processing request may include at least one third pending task and the task execution time for each third pending task. The task execution time refers to the time when the task will be triggered, and can be represented as T. deal .
[0052] S202: For each third pending task, determine the second time difference between the task execution time of the third pending task and the current time.
[0053] S203: For each third pending task, if the second time difference is less than or equal to the set duration, the third pending task is added to the second queue; if the second time difference is greater than the set duration, the third pending task is added to the first queue.
[0054] Optionally, for each third pending task, the task execution time T of that third pending task is determined.deal With current time T now The second time difference. If the second time difference is less than or equal to the set duration (e.g., Δt), the third task to be processed is added to the second queue; if the second time difference is greater than the set duration, the third task to be processed is added to the first queue.
[0055] Optionally, assuming the set duration Δt is 7 days, if the task execution time T of the task to be processed is... deal With current time T now If the time difference is less than or equal to 7 days, the task to be processed will be added to the second queue and wait for its execution time to expire before being processed. If the task execution time T of the task to be processed is less than or equal to 7 days, the task will be added to the second queue and wait for its execution time to expire before being processed. deal With current time T now If the time difference is greater than 7 days, the task to be processed will be added to the first queue, awaiting subsequent polling scans. As time changes, at task execution time T... deal With current time T now If the time difference is less than or equal to 7 days, the task to be processed will be moved from the first queue to the second queue.
[0056] The storage method after the tasks to be processed are enqueued (that is, stored in the first or second queue) is not limited in this embodiment. Optionally, when inserting a task to be processed into the corresponding queue, the task can be serialized and a record can be written to the corresponding queue using the following fields as shown in Table 1: task number, serialized task, execution time, execution status, creation time, and queue entry module type.
[0057] Among them, the task number can represent the sequential number when the task to be processed is added to the queue; the serialized task means that the task to be processed is serialized into bytes such as binary encoding; the execution time means the execution time of the task to be processed, that is, the time when the task is triggered to be executed; the creation time means the time when the task to be processed is retrieved; and the queue entry module type means the type of the task to be processed, such as request type, reminder type, etc.
[0058] Alternatively, the storage structure of the queue can be as shown in Table 1 below:
[0059] Table 1
[0060]
[0061]
[0062] According to embodiments of this application, tasks to be processed in the queue can be accessed and read using serialization and deserialization mechanisms to facilitate task saving and reconstruction.
[0063] The queue in this application guarantees data atomicity, meaning that the process of writing a task to the queue is an indivisible process. If the task is successfully written, it means that the entire record corresponding to the task has been successfully written; if the task fails to write, it means that the entire record corresponding to the task has failed to be written.
[0064] Once a task is successfully enqueued, the enqueued data will be persisted, meaning the task can be stored in the database.
[0065] Embodiments of this application can be implemented via an interface. For example, an enqueue interface can be provided to save tasks to be processed (e.g., delayed-triggered tasks) to a queue. After receiving a task processing request including the tasks to be processed, the enqueue interface can be called, and the enqueue operation can be performed based on parameters such as the task execution time of the tasks to be processed, the queue entry module type, the serialized tasks to be processed, and extended information.
[0066] By designing a reasonable queue layout and adding tasks to different queues based on their execution time, queue data can be managed more efficiently.
[0067] According to an embodiment of this application, the second queue may include multiple sub-queues, and each sub-queue may correspond to a task time period.
[0068] Figure 3 This is a schematic diagram of a second queue including multiple sub-queues, provided as an embodiment of this application. For example... Figure 3 As shown, the second queue comprises seven sub-queues. The second sub-queue may also include other numbers of sub-queues, which is not limited in this application.
[0069] A subqueue is used to store the second unprocessed task whose execution time belongs to the task time period corresponding to the subqueue.
[0070] This application does not limit the way the task time period is divided. For example, the task time period can be a time period in hours or a time period in days.
[0071] Optionally, such as Figure 3 As shown, the seven sub-queues included in the second queue correspond to the task time periods: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday, and can be represented as AI_QUEUE_MON, AI_QUEUE_TUES, AI_QUEUE_WED, AI_QUEUE_THUR, AI_QUEUE_FRI, AI_QUEUE_SAT, and AI_QUEUE_SUN, respectively. The structures of the multiple sub-queues can be the same or different.
[0072] Each sub-queue is used to store pending tasks whose execution time is within that time period. For example, the Monday sub-queue is used to store pending tasks whose execution time is Monday.
[0073] Optionally, the second pending tasks in the second queue are monitored, and the second pending tasks in the second queue are processed according to their respective task execution times, including:
[0074] Determine the task time period to which the current time belongs, monitor the second pending task in the sub-queue corresponding to the task time period, and process the second pending task if the task execution time of the second pending task arrives.
[0075] According to an embodiment of this application, during processing, the current system time is first determined, and the task time period to which the current time belongs is determined. Then, the tasks to be processed in the sub-queue corresponding to the task time period are monitored in real time. During real-time monitoring, when the task execution time of a task to be processed arrives, the task to be processed is processed.
[0076] Optionally, the arrival of task execution time as described herein includes situations where the task execution time is equal to the current time, or where the task execution time is less than the current time and the time difference between the current time and the task execution time is less than a predetermined interval. For example, the predetermined interval can be configured by the system or set by the user as needed, and can be represented as Δb, where Δb is a positive number and the time is in seconds (s).
[0077] Optionally, when the current time T is determined now If the task falls within the task time period of Monday, then the subtasks corresponding to Monday will be monitored in real time. During real-time monitoring, when the task execution time T of a certain pending task... deal Equal to the current time T now Or task execution time T deal Less than the current time T now And the current time T now With task execution time T deal If the time difference is less than the predetermined interval Δb, then the task to be processed will be processed.
[0078] By further subdividing the second queue into multiple sub-queues, only the tasks to be processed in one sub-queue can be monitored at a time, which greatly reduces the amount of data to be processed and reduces the traversal of invalid data.
[0079] Optionally, the second tasks to be processed in the second queue can be stored in order of their execution time from earliest to latest.
[0080] Optionally, the second pending tasks in the second queue are monitored, and the second pending tasks in the second queue are processed according to their respective task execution times, including:
[0081] According to the storage order of each second pending task, perform the following operations on each second pending task in sequence until the task execution time of the current pending task has not arrived. Then process each second pending task that is stored before the current pending task: determine whether the task execution time of the current pending task has arrived based on the current time and the task execution time of the current pending task. If the task execution time of the current pending task has arrived, then take the next second pending task as the new current pending task.
[0082] Figure 4 This is a flowchart illustrating another task processing method provided in an embodiment of this application. Figure 4 As shown, when the second tasks to be processed in the second queue are stored in order of their execution times from first to last, the method for processing the tasks to be processed may include the following steps.
[0083] S401: Based on the current time and the current pending task's execution time, determine whether the current pending task's execution time has arrived. The arrival of the task execution time, as described herein, includes the task execution time being equal to the current time, or the task execution time being less than the current time and the time difference between the current time and the task execution time being less than a predetermined interval. For example, the predetermined interval can be configured by the system or set by the user as needed, and can be represented as Δb, where Δb is a positive number and the time is in seconds (s).
[0084] If the execution time of the current pending task is reached, proceed to step S402: take the next second pending task as the new current pending task, and return to step S401.
[0085] If the execution time of the current pending task has not arrived, then proceed to step S403: process each of the second pending tasks that are stored before the current pending task.
[0086] Optionally, assume there are 5 pending tasks in the second queue, and these 5 pending tasks are stored in chronological order as Task 1, Task 2, Task 3, Task 4, and Task 5. During task processing, first determine whether the execution time of Task 1 has arrived based on the current time and the execution time of Task 1 (the current pending task at this time is Task 1).
[0087] If the execution time of Task 1 expires, Task 2 is added as a new pending task, and its execution time is checked. If the execution time of Task 2 has not expired, both Task 1 and Task 2 are processed. If the execution time of Task 2 expires, Task 3 is added as the current pending task and processed.
[0088] If the execution time of Task 1 has not been reached, wait for the scheduled time or make the next judgment in real time.
[0089] When the finite number of tasks to be processed in the second queue are sorted from first to last according to time, the status of the tasks to be processed after that can be determined by judging the execution time of the tasks that are earlier in the queue, without having to traverse invalid data, thus further improving data processing efficiency.
[0090] Figure 5 This is a flowchart illustrating another task processing method provided in an embodiment of this application.
[0091] According to an embodiment of this application, the task processing method may further include the following steps S501, S502 and S503.
[0092] S501: If the execution time of the third task to be processed is less than the current time, and the time difference between the current time and the execution time of the third task to be processed is greater than a predetermined interval, then the third task to be processed is added to the third queue.
[0093] According to embodiments of this application, on one hand, the current time T of the system at the time of obtaining the task processing request can be determined. now The task processing request may include at least one third pending task and the task execution time for each third pending task. The task execution time refers to the time when the task will be triggered, and can be represented as T. deal .
[0094] If the task execution time T of the third pending task is obtained deal Less than the current time T now The predetermined interval Δb (Δb is a positive number, and the time is in seconds (s)), i.e., the current time T. now With task execution time T deal The time difference is greater than the predetermined interval Δb, (i.e., T) now -T deal If the value is greater than Δb), then the third task to be processed will be added to the third queue.
[0095] S502: If there is a first task to be processed in the first queue whose execution time is less than the current time and whose time interval between the current time and the execution time is greater than a predetermined interval, then the first task to be processed is moved from the first queue to the third queue.
[0096] On the other hand, the first queue is used to store the first pending tasks whose execution time and acquisition time differ from the task acquisition time by a set duration. The first pending task in the first queue is scanned; if a task with execution time T exists in the first queue... deal Less than the current time T now And the current time T now With task execution time T deal The first task to be processed whose time interval is greater than the predetermined interval, i.e., T now -T deal If the value is greater than Δb, then the first task to be processed will be added from the first queue to the third queue.
[0097] S503: When the pre-configured abnormal task handling conditions are met, process the pending tasks in the third queue.
[0098] The third queue in the embodiments of this application can also be called an exception queue (e.g., CA_QUEUE_EXCEPTION). Exception handling conditions can be pre-configured. When the pre-configured exception handling conditions are met, the tasks pending in the exception queue are processed.
[0099] Optionally, the pre-configured abnormal task handling conditions can be: completing a predetermined number (1000) of pending tasks; elapsed a predetermined time; receiving a specific processing instruction; etc.
[0100] By adding overdue tasks (i.e., tasks whose execution time is less than the current time and the time interval between the current time and the task execution time is greater than a predetermined interval) to an exception queue, and processing exception tasks when conditions are met, it is possible to handle timed-out tasks and efficiently manage queue data.
[0101] Figure 6 This is a flowchart illustrating another task processing method provided in an embodiment of this application.
[0102] The embodiments of this application can consider the task execution time T of the task to be processed when acquiring the task to be processed. deal With current time T now Based on the different relationships, the tasks to be processed are added to the first queue, the second queue, or the third queue, such as... Figure 6 As shown.
[0103] S601: Obtain a task processing request. The task processing request includes at least one task to be processed and the task execution time for each task to be processed.
[0104] The embodiments of this application can determine the current time T of the system when a task processing request is received. nowThe task processing request may include at least one pending task and the task execution time for each pending task. The task execution time refers to the time at which the task will be triggered, and can be represented as T. deal .
[0105] S602: Determine the task execution time T for the pending task. deal With current time T now Time difference T deal -T now .
[0106] S603: Determine whether the time difference is greater than the set duration Δt (Δt is a positive number, and the time is in seconds (s)), that is, T deal -T now >Δt. If so, add the task to be processed to the first queue.
[0107] If the time difference is less than or equal to the set duration Δt, i.e., T deal -T now If ≤Δt, proceed to S604: determine the task execution time T of the task to be processed. deal Whether the timeout has occurred within the predetermined interval Δb (Δb is a positive number, and the time is in seconds) is determined, i.e., the task execution time T is determined. deal Is it less than the current time, and is the time difference between the current time and the task execution time greater than the predetermined interval Δb, i.e., T? now -T deal If yes, add the task to be processed to the third queue; otherwise, add the task to be processed to the second queue.
[0108] After adding the tasks to be processed to the corresponding queues, they are processed accordingly based on the descriptions of each queue in the above embodiments.
[0109] Figure 7 A flowchart illustrating another task processing method provided in this application embodiment includes the following steps. Before processing a task, this application embodiment can remove the task to be processed from a queue, hereinafter referred to as dequeueing. The dequeueing process is as follows: Figure 7 As shown, it includes steps S701-S711.
[0110] S701: Configure dequeue parameters and obtain parameters of the database where the tasks to be processed are located.
[0111] Data to be processed can be stored in a corresponding queue in the database. When it is necessary to retrieve the data from the queue, dequeue parameters can be configured and parameters of the database where the task resides can be obtained. These dequeue parameters may include: database scan frequency, output parameters of the task, etc. Information about the database where the task resides may include, for example, username, password, and IP port.
[0112] S702: Get queue name, that is, get the queue name of the database used to store tasks to be processed, such as first queue, second queue, first sub-queue, second sub-queue, etc.
[0113] S703: Query pending tasks that meet the criteria. For example, a query stream (e.g., otl_stream) can be constructed to perform the query. The query stream is used to traverse and query the pending tasks in the second queue and determine whether there are any pending tasks that meet the criteria in the second queue, i.e., whether the execution time of each pending task has arrived. The query stream ends (eof) when all pending tasks in the second queue have been traversed. If the traversal ends and there are no pending tasks that meet the criteria, the query stream is reconstructed after a predetermined time (e.g., 5 seconds). The predetermined time can be the system default or configured by the user. If the query is not completed, the second queue is queried again, and the pending tasks that meet the criteria are placed in the cache until all pending tasks that meet the criteria have been retrieved.
[0114] S704: Determine if this is the first time the dequeue process has been initiated.
[0115] S705: If this is the first time the dequeue process is initiated, the eligible pending tasks in the second queue need to be moved from each sub-queue to the sub-queue corresponding to the task time period of the current time.
[0116] S706: If there is an exception, such as the move failing, then execute S707: roll back operation.
[0117] The rollback operation (S707) undoes the operation in step S705. In other words, it cancels the move operation on the eligible pending tasks in the second queue.
[0118] S708: Construct a query, delete, and insert stream (or otl_stream) to obtain the eligible pending tasks in the sub-queue corresponding to the current time in the second queue, i.e., tasks whose execution time has arrived. After obtaining the eligible pending tasks, sort them according to their execution time (T). deal Sequential sorting, for example, sorting tasks by execution time from earliest to latest.
[0119] Set a counter to monitor the number of times the query, delete, and insert streams are executed. Additionally, set the counter parameter `commit_count`, which determines that eligible pending tasks will be dequeued after the number of query, delete, and insert stream executions reaches `commit_count` times.
[0120] S709: Determine if the counter count has reached the set commit_count. If the counter count reaches the set commit_count (i>commit_count) (e.g., default is 1000), then commit the transaction, that is, synchronize the cache to the database. Simultaneously check if the transaction commit is abnormal. If there is no abnormality, dequeue the pending tasks sorted by task execution time. If there is an abnormality, perform a rollback operation.
[0121] If the counter count has not reached the set commit_count (i≤commit_count), then proceed to step S710: determine if there are any tasks pending in the queue. This can be determined by checking if the query result set cursor points to EOF.
[0122] For example, if the database queues contain 100 tasks to be processed, in step S710, it is determined whether all tasks to be processed in all queues have been dequeued and completed.
[0123] If there are still tasks to be processed in the queue, execute S711; otherwise, commit the transaction.
[0124] S711: Move the pending tasks that meet the above conditions and are sorted by task execution time to the history queue. This history queue is used to store the pending tasks that have been dequeued. At this time, the counter is incremented by 1 (counter I = I + 1), and the counter is returned to determine whether the set commit_count has been reached.
[0125] Alternatively, the above steps can be implemented through the dequeue interface.
[0126] Figure 8 This is a schematic diagram illustrating an application scenario of the task processing method provided in the embodiments of this application. Figure 8 This is a specific example of an embodiment of this application being executed by a task processing server.
[0127] The task processing method provided in this application can be applied in the field of communications. Optionally, the task processing method provided in this application can be applied to the credit control reminder service for billing systems in the field of communications.
[0128] In the credit control reminder service for the billing system, application server 1 can issue pending tasks. For example, after a user's call detail record (CDR) is generated, application server 1 compares the cost of the call with the user's corresponding account balance. If the user's account balance is insufficient, it generates pending task 1 (e.g., sending a reminder SMS) and pending task 2 (e.g., requesting service suspension), and sends both pending tasks 1 and 2 to the task processing server. For example, the execution time of pending task 1 is the current time T.now +1 minute, the execution time of pending task 2 is the current time T. now +1 day.
[0129] After receiving the two tasks mentioned above, the task processing server can determine the execution time of task 1 and task 2 respectively, relative to the current time T. now The time difference.
[0130] For task 1 to be processed, the task execution time is compared with the current time T. now The time difference is 1 minute.
[0131] For task 2 to be processed, the task execution time is compared with the current time T. now The time difference is 2 days.
[0132] Since both of the above time differences are less than the set duration of 7 days, both Task 1 and Task 2 can be added to the second queue.
[0133] When the execution time of pending task 1 is detected, pending task 1 is dequeued from the second queue, and the application server is called to send a reminder to the client based on the type and content of pending task 1, such as sending an SMS reminder for insufficient balance.
[0134] When the execution time of pending task 2 is detected, pending task 2 is dequeued from the second queue, and application server 2 is called to perform an operation based on the type and content of pending task 2, such as suspending the user account.
[0135] Application server 1 and application server 2 can be the same or different servers, and with authorization, the operations performed by the application server can also be performed by the task processing server.
[0136] According to embodiments of this application, the task processing method of this application further includes exception handling for abnormal situations.
[0137] During the scanning and execution of pending tasks, insertion errors may occur due to reasons such as tables or queues not being created in the database. These abnormal pending tasks will be directly stored in the third queue (also known as the exception queue, CA_QUEUE_EXCEPTION). If a network error occurs during insertion, this application can repeat the processing of the pending task until it is inserted into the database. This exception queue also stores overdue processing tasks (overdue because the task execution time is less than the current time and the time difference between the current time and the task execution time is greater than a predetermined interval) or overdue processing tasks from the first queue.
[0138] Optionally, the structure of the exception queue can be as shown in Table 2 below.
[0139] Table 2
[0140]
[0141] The descriptions of error codes (ERR_CODE) are shown in Table 3 below.
[0142] Table 3
[0143] Error code describe 45500 Processing time expired 45501 The current data type does not support 45502 Database connection failed 45503 No table partitioning rules 45504 Database error 45505 Unknown error
[0144] Based on the same principle as the task processing method provided in this application, embodiments of this application also provide a task processing apparatus. Figure 9 This is a schematic diagram of a task processing device provided in an embodiment of this application. Figure 9 As shown, the task processing device 900 in this embodiment may include a task monitoring module 901 and a task processing module 902.
[0145] The task monitoring module 901 is used to scan the first pending task in the first queue. If there is a pending task in the first queue whose execution time is less than or equal to the current time, the pending task is moved from the first queue to the second queue.
[0146] The task processing module 902 is used to monitor the second pending tasks in the second queue and process the second pending tasks in the second queue according to the task execution time of each second pending task in the second queue.
[0147] Optionally, the queue for storing tasks to be processed can be pre-divided into a first queue and a second queue. Optionally, the first queue (also called the limit queue) is used to store the first task to be processed that meets the first condition, and the second queue (also called the normal queue) is used to store the second task to be processed that meets the second condition. Meeting the first condition means that the task has a first time difference greater than a set duration, and meeting the second condition means that the task has a first time difference less than or equal to the set duration. The first time difference refers to the time difference between the task execution time and the task acquisition time.
[0148] Optionally, for a task to be processed, the task execution time refers to the time when the task will be triggered, that is, the time when the task will be executed. For ease of description, the task execution time can be represented as T. deal Task acquisition time refers to the time it takes to acquire the task, that is, the time it takes for the electronic device to acquire (e.g., receive) the task, which can be expressed as T. obtainIn this context, if the task to be processed is received from another device, the task acquisition time can refer to the time when the task sender sends the task, or the time when the task receiver receives the task, such as the system time of the electronic device when it receives the task. The current time refers to the current system time of the electronic device, which can be represented as T. now It is understandable that the current time is a changing time; it is different at different moments.
[0149] Optionally, assuming the above-mentioned duration is Δt, where Δt is a positive number and the time can be in seconds (s), the first queue stores the task execution time T. deal With task acquisition time T obtain The first queue contains tasks with a time difference greater than Δt. The second queue stores tasks with execution times T. deal With task acquisition time T obtain The tasks to be processed whose first time difference is less than or equal to Δt are designated as pending tasks. For ease of description, in this embodiment, the pending tasks stored in the first queue are referred to as first pending tasks, and the pending tasks stored in the second queue are referred to as second pending tasks.
[0150] In this embodiment, after each task to be processed is acquired, it is determined whether to store the task in the first queue or the second queue based on the acquisition time (i.e., the current time when the task was acquired) and the task execution time. Furthermore, it is determined whether to move any first tasks to be processed in the first queue to the second queue by scanning each first task to be processed in the first queue. Specifically, if there are tasks with execution time T in the first queue... deal With current time T now If a task has a first time difference less than or equal to a set duration Δt, then the task is moved from the first queue to the second queue and removed from the first queue.
[0151] Optionally, the second pending tasks in the second queue are monitored, and the second pending tasks in the second queue are processed accordingly according to the task execution time of each second pending task in the second queue. This includes removing the second pending task whose task processing time has arrived from the second queue (also known as dequeuing) and processing the pending task according to the type and content of the pending task.
[0152] For example, if the task to be processed is a reminder, then a reminder message will be sent to the client. If the task to be processed is an execution task, then the corresponding operation will be performed based on the content of the task to be processed.
[0153] The task processing device in this embodiment can execute the task processing method provided in the embodiments of this application. The implementation principle is similar and will not be described again here.
[0154] The embodiments of this application store pending tasks whose execution time differs from the current time by a set time greater than a predetermined duration in a first queue, and pending tasks whose execution time differs from the current time by a predetermined time less than or equal to a predetermined time in a second queue. By monitoring the first queue, pending tasks whose execution time differs from the current time by a predetermined time less than or equal to a predetermined time in the first queue are moved to the second queue. The pending tasks in the second queue are also monitored, and processed according to their respective execution times. That is, pending tasks are added to different queues based on their execution times. By scanning each pending task in the first queue, it is determined whether a pending task in the first queue needs to be moved to the second queue. During polling, only pending tasks in the second queue need to be scanned, reducing the amount of data scanned. This solves the problems of untimely triggering of scheduled tasks and low data processing efficiency, thereby increasing task processing performance and improving task processing efficiency.
[0155] Figure 10 This is a schematic diagram of another task processing device provided in an embodiment of this application. Figure 10 As shown, the task processing device 1000 of this embodiment may include: an acquisition module 1001, an enqueue module 1002, a task monitoring module 1003, and a task processing module 1004. The task monitoring module 1003 and the processing module 1004 can be connected to... Figure 9 The task monitoring module 901 and the task processing module 902 shown are the same.
[0156] In one embodiment, the acquisition module is used to acquire task processing requests. The task processing request includes at least one third task to be processed and the task execution time for each task.
[0157] In one embodiment, the queuing module is used to determine, for each third task to be processed, the second time difference between the task execution time of the third task to be processed and the current time; for each third task to be processed, if the second time difference is less than or equal to a set duration, the third task to be processed is added to the second queue, and if the second time difference is greater than the set duration, the third task to be processed is added to the first queue.
[0158] In one embodiment, the second queue includes multiple sub-queues, each sub-queue corresponding to a task time period. Each sub-queue stores second pending tasks whose execution time belongs to the corresponding task time period. The task processing module includes a task processing sub-module, which determines the task time period to which the current time belongs, monitors the second pending tasks in the sub-queue corresponding to that task time period, and processes the second pending tasks in the sub-queue whose execution time has arrived.
[0159] In one embodiment, the second tasks to be processed in the second queue are stored in chronological order according to their task execution times.
[0160] In one embodiment, the task processing module is further configured to perform the following operations sequentially on each second task to be processed according to the storage order of the tasks to be processed, until the task execution time of the current task to be processed has not arrived, and to process each second task to be processed that is stored before the current task to be processed:
[0161] Based on the current time and the execution time of the current pending task, determine whether the execution time of the current pending task has been reached. If the execution time of the current pending task has been reached, then the next pending task will be taken as the new current pending task.
[0162] In one embodiment, the queuing module is further configured to add the third task to the third queue if the task execution time of the third task to be processed is less than the current time and the time difference between the current time and the task execution time is greater than a predetermined interval.
[0163] In one embodiment, the task monitoring module is further configured to move the first task to be processed from the first queue to the third queue if there is a first task to be processed in the first queue whose task execution time is less than the current time and whose time difference between the current time and the task execution time is greater than a predetermined interval.
[0164] In one embodiment, the task processing module is further configured to process the pending tasks in the third queue when pre-configured abnormal task processing conditions are met.
[0165] In one embodiment, the task monitoring module can scan the first pending task in the first queue in real time or at a set period.
[0166] Figure 11 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. This electronic device can be a server. Figure 11As shown, the electronic device 1100 includes a processor 1101 and a memory 1103. The processor 1101 and the memory 1103 are connected, for example, via a bus 1102. Optionally, the electronic device 1100 may also include a transceiver 1104. It should be noted that in practical applications, the transceiver 1104 is not limited to one, and the structure of the electronic device 1100 does not constitute a limitation on the embodiments of this application.
[0167] The processor 1101 is used in this embodiment of the application to implement... Figure 10 The functions of the queuing module, task processing module, and task monitoring module are shown. Transceiver 1104 includes a receiver and a transmitter. Transceiver 1104 is used in this embodiment to implement... Figure 10 The functions of the acquisition module are shown.
[0168] Processor 1101 may be a CPU, a general-purpose processor, a DSP, an ASIC, an FPGA, or other programmable logic device, transistor logic device, hardware component, or any combination thereof. Processor 1101 may implement or execute the various exemplary logic blocks, modules, and circuits described in conjunction with the disclosure of this application. Processor 1101 may also be a combination that implements computing functions, such as including one or more microprocessor combinations, a combination of a DSP and a microprocessor, etc.
[0169] Bus 1102 may include a pathway for transmitting information between the aforementioned components. Bus 1102 may be a PCI bus or an EISA bus, etc. Bus 1102 may be divided into address bus, data bus, control bus, etc. For ease of representation, Figure 11 The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.
[0170] The memory 1103 may be a ROM or other type of static storage device capable of storing static information and instructions, RAM or other type of dynamic storage device capable of storing information and instructions, or it may be an EEPROM, CD-ROM or other optical disc storage, optical disc storage (including compressed optical discs, laser discs, optical discs, digital universal optical discs, Blu-ray discs, etc.), magnetic disk storage media or other magnetic storage devices, or any other medium capable of carrying or storing desired program code in the form of instructions or data structures and accessible by a computer, but is not limited thereto.
[0171] The memory 1103 stores the application code that executes the scheme of this application, and its execution is controlled by the processor 1101. The processor 1101 executes the application code stored in the memory 1103 to implement... Figure 10 The operation of the task processing device provided in the illustrated embodiment.
[0172] Compared with existing technologies, this method stores tasks with a time difference between their execution time and the current time greater than a set duration in a first queue, and tasks with a time difference less than or equal to the set duration in a second queue. By monitoring the first queue, tasks with a time difference less than or equal to the set duration are moved to the second queue. The second queue is also monitored, and tasks are processed according to their execution times. In other words, tasks are added to different queues based on their execution times. The method also scans the tasks in the first queue to determine whether they need to be moved to the second queue. During polling, only tasks in the second queue need to be scanned, reducing the amount of data scanned. This solves the problems of untimely triggering of scheduled tasks and low data processing efficiency, thus improving task processing performance and efficiency.
[0173] This application provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the aforementioned task processing method. Compared with the prior art, by storing tasks whose execution time differs from the current time by a set time interval in a first queue, and storing tasks whose execution time differs from the current time by a set time interval in a second queue, and by monitoring the first queue, tasks whose execution time differs from the current time by a set time interval are moved to the second queue. The second queue is also monitored, and tasks are processed according to their execution times. In other words, tasks are added to different queues based on their execution times. By scanning each task in the first queue to determine whether to move it to the second queue, only the tasks in the second queue need to be scanned during polling, reducing the amount of data scanned. This solves the problems of untimely triggering of scheduled tasks and low data processing efficiency, thereby increasing task processing performance and improving task processing efficiency.
[0174] This application provides an embodiment of a computer-readable storage medium suitable for the above-described task processing method. Further details are omitted here.
[0175] It should be understood that although the steps in the flowcharts of the accompanying figures are shown sequentially as indicated by the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the accompanying figures may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily completed at the same time, but can be executed at different times, and their execution order is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the sub-steps or stages of other steps.
[0176] The above description is only a partial embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. A task processing method, characterized in that, include: Scan the first pending task in the first queue. If there is a pending task in the first queue whose execution time is less than or equal to the current time, then move the pending task from the first queue to the second queue. The first queue stores first pending tasks that meet a first condition, and the second queue stores second pending tasks that meet a second condition. Meeting the first condition means that a first time difference is greater than a set duration, and meeting the second condition means that a first time difference is less than or equal to the set duration. The first time difference is the time difference between the task execution time and the task acquisition time. The second queue includes multiple sub-queues, each sub-queue corresponding to a task time period, and each sub-queue stores second pending tasks whose task execution time belongs to the task time period corresponding to that sub-queue. Monitor the second pending tasks in the second queue and process them according to their respective execution times. The step of processing the second tasks in the second queue according to their respective execution times includes: Determine the task time period to which the current time belongs, monitor only the second pending task in the sub-queue corresponding to the task time period to which the current time belongs, and process the second pending task in the sub-queue whose task execution time has arrived.
2. The task processing method according to claim 1, characterized in that, Also includes: Obtain a task processing request, the task processing request including at least one third task to be processed and the task execution time of each third task to be processed; For each of the third pending tasks, a second time difference between the task execution time of the third pending task and the current time is determined. If the second time difference is less than or equal to the set duration, the third pending task is added to the second queue. If the second time difference is greater than the set duration, the third pending task is added to the first queue.
3. The task processing method according to claim 1 or 2, characterized in that, The second queue contains the second tasks to be processed, which are stored in chronological order according to their execution times. The step of processing the second tasks in the second queue according to their respective task execution times also includes: According to the storage order of each of the second pending tasks, perform the following operations on each of the second pending tasks in sequence until the execution time of the current second pending task has not arrived, and then process the second pending tasks that are stored before the current pending task: Based on the current pending task's execution time, determine whether the current pending task's execution time has been reached. If the current pending task's execution time has been reached, then the next pending task will be taken as the new current pending task.
4. The task processing method according to claim 2, characterized in that, Also includes: If the execution time of the third task to be processed is less than the current time, and the time difference between the current time and the execution time of the third task to be processed is greater than a predetermined interval, then the third task to be processed is added to the third queue. If there is a first task to be processed in the first queue whose execution time is less than the current time and whose time difference between the current time and the task execution time is greater than a predetermined interval, then the first task to be processed is moved from the first queue to the third queue. When the pre-configured abnormal task handling conditions are met, the tasks to be processed in the third queue are processed.
5. The task processing method according to claim 1, characterized in that, The scanning of the first task to be processed in the first queue includes: The first task to be processed in the first queue is scanned in real time or at a set periodic interval.
6. A task processing device, characterized in that, include: The task monitoring module is used to scan the first pending task in the first queue. If there is a pending task in the first queue whose execution time is less than or equal to the current time, the pending task is moved from the first queue to the second queue. The first queue stores first pending tasks that meet a first condition, and the second queue stores second pending tasks that meet a second condition. Meeting the first condition means that a first time difference is greater than a set duration, and meeting the second condition means that a first time difference is less than or equal to the set duration. The first time difference is the time difference between the task execution time and the task acquisition time. The second queue includes multiple sub-queues, each sub-queue corresponding to a task time period, and each sub-queue stores second pending tasks whose task execution time belongs to the task time period corresponding to that sub-queue. The task processing module is used to process the second tasks in the second queue according to the task execution time of each second task in the second queue. The step of processing the second tasks in the second queue according to their respective execution times includes: Determine the task time period to which the current time belongs, monitor only the second pending task in the sub-queue corresponding to the task time period to which the current time belongs, and process the second pending task in the sub-queue whose task execution time has arrived.
7. An electronic device, characterized in that, include: processor; as well as The memory is configured to store machine-readable instructions that, when executed by the processor, cause the processor to perform the task processing method according to any one of claims 1-5.
8. A computer-readable storage medium, characterized in that, The storage medium stores a computer program, which, when executed by a processor, implements the task processing method according to any one of claims 1-5.
Citation Information
Patent Citations
Message queue implementation method and system, storage medium and electronic equipment
CN109684108A
Timing task processing method, system and equipment
CN110245008A