Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
WP Plugins
Cron-Control
Commits
a6b025c6
Verified
Commit
a6b025c6
authored
Dec 13, 2016
by
Erick Hitter
Browse files
Introduce logic to shuffle events to ensure events of the same action don't dominate
Fixes #56
parent
4b7d30c4
Changes
1
Hide whitespace changes
Inline
Side-by-side
includes/class-events.php
View file @
a6b025c6
...
...
@@ -341,7 +341,60 @@ class Events extends Singleton {
* @return array
*/
private
function
reduce_queue
(
$events
)
{
$reduced_queue
=
array_slice
(
$events
,
0
,
JOB_QUEUE_SIZE
);
// Loop through events, adding one of each action during each iteration
$reduced_queue
=
array
();
$action_counts
=
array
();
$i
=
1
;
// Intentionally not zero-indexed to facilitate comparisons against $action_counts members
while
(
count
(
$reduced_queue
)
<
(
JOB_QUEUE_SIZE
*
3
)
&&
!
empty
(
$events
)
)
{
// Circuit breaker
if
(
$i
>
15
)
{
break
;
}
// Each time the events array is iterated over, move one instance of an action to the current queue
foreach
(
$events
as
$key
=>
$event
)
{
$action
=
$event
[
'action'
];
// Prime the count
if
(
!
isset
(
$action_counts
[
$action
]
)
)
{
$action_counts
[
$action
]
=
0
;
}
// Check and do the move
if
(
$action_counts
[
$action
]
<
$i
)
{
$reduced_queue
[]
=
$event
;
$action_counts
[
$action
]
++
;
unset
(
$events
[
$key
]
);
}
}
// When done with an iteration and events remain, start again from the beginning of the $events array
if
(
empty
(
$events
)
)
{
break
;
}
else
{
$i
++
;
reset
(
$events
);
continue
;
}
}
/**
* IMPORTANT: DO NOT re-sort the $reduced_queue array from this point forward.
* Doing so defeats the preceding effort.
*
* While the events are now out of order with respect to timestamp, they're ordered
* such that one of each action is run before another of an already-run action.
* The timestamp mis-ordering is trivial given that we're only dealing with events
* for the current JOB_QUEUE_WINDOW_IN_SECONDS.
*/
// Finally, ensure that we don't have more than we need
if
(
count
(
$reduced_queue
)
>
JOB_QUEUE_SIZE
)
{
$reduced_queue
=
array_slice
(
$reduced_queue
,
0
,
JOB_QUEUE_SIZE
);
}
return
$reduced_queue
;
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment