MCPcopy Create free account
hub / github.com/apache/cloudberry / PollForTasks

Function PollForTasks

src/backend/task/pg_cron.c:790–976  ·  view source on GitHub ↗

* PollForTasks calls poll() for the sockets of all tasks. It checks for * read or write events based on the pollingStatus of the task. */

Source from the content-addressed store, hash-verified

788 * read or write events based on the pollingStatus of the task.
789 */
790static void
791PollForTasks(List *taskList)
792{
793 TimestampTz currentTime = 0;
794 TimestampTz nextEventTime = 0;
795 int pollTimeout = 0;
796 long waitSeconds = 0;
797 int waitMicros = 0;
798 CronTask **polledTasks = NULL;
799 struct pollfd *pollFDs = NULL;
800 int pollResult = 0;
801
802 int taskIndex = 0;
803 int taskCount = list_length(taskList);
804 int activeTaskCount = 0;
805 ListCell *taskCell = NULL;
806
807 polledTasks = (CronTask **) palloc0(taskCount * sizeof(CronTask *));
808 pollFDs = (struct pollfd *) palloc0(taskCount * sizeof(struct pollfd));
809
810 currentTime = GetCurrentTimestamp();
811
812 /*
813 * At the latest, wake up when the next minute starts.
814 */
815 nextEventTime = TimestampMinuteEnd(currentTime);
816
817 foreach(taskCell, taskList)
818 {
819 CronTask *task = (CronTask *) lfirst(taskCell);
820 PostgresPollingStatusType pollingStatus = task->pollingStatus;
821 struct pollfd *pollFileDescriptor = &pollFDs[activeTaskCount];
822
823 if (activeTaskCount >= max_running_tasks)
824 {
825 /* already polling the maximum number of tasks */
826 break;
827 }
828
829 if (task->state == CRON_TASK_ERROR || task->state == CRON_TASK_DONE ||
830 CanStartTask(task))
831 {
832 /* there is work to be done, don't wait */
833 pfree(polledTasks);
834 pfree(pollFDs);
835 return;
836 }
837
838 if (task->state == CRON_TASK_WAITING && task->pendingRunCount == 0)
839 {
840 /*
841 * Make sure we do not wait past the next run time of an interval
842 * job.
843 */
844 if (task->secondsInterval > 0)
845 {
846 TimestampTz nextRunTime =
847 TimestampTzPlusMilliseconds(task->lastStartTime,

Callers 1

WaitForCronTasksFunction · 0.85

Calls 11

list_lengthFunction · 0.85
GetCurrentTimestampFunction · 0.85
TimestampMinuteEndFunction · 0.85
CanStartTaskFunction · 0.85
PQsocketFunction · 0.85
TimestampDifferenceFunction · 0.85
WaitForLatchFunction · 0.85
palloc0Function · 0.50
foreachFunction · 0.50
pfreeFunction · 0.50

Tested by

no test coverage detected