MediaWiki REL1_34
PoolCounterWork.php
Go to the documentation of this file.
1<?php
27abstract class PoolCounterWork {
29 protected $type = 'generic';
31 protected $cacheable = false; // does this override getCachedWork() ?
33 private $poolCounter;
34
39 public function __construct( $type, $key ) {
40 $this->type = $type;
41 $this->poolCounter = PoolCounter::factory( $type, $key );
42 }
43
48 abstract public function doWork();
49
54 public function getCachedWork() {
55 return false;
56 }
57
63 public function fallback() {
64 return false;
65 }
66
74 public function error( $status ) {
75 return false;
76 }
77
84 public function logError( $status ) {
85 $key = $this->poolCounter->getKey();
86
87 wfDebugLog( 'poolcounter', "Pool key '$key' ({$this->type}): "
88 . $status->getMessage()->inLanguage( 'en' )->useDatabase( false )->text() );
89 }
90
106 public function execute( $skipcache = false ) {
107 if ( $this->cacheable && !$skipcache ) {
108 $status = $this->poolCounter->acquireForAnyone();
109 } else {
110 $status = $this->poolCounter->acquireForMe();
111 }
112
113 if ( !$status->isOK() ) {
114 // Respond gracefully to complete server breakage: just log it and do the work
115 $this->logError( $status );
116 return $this->doWork();
117 }
118
119 switch ( $status->value ) {
121 // Better to ignore nesting pool counter limits than to fail.
122 // Assume that the outer pool limiting is reasonable enough.
123 /* no break */
125 $result = $this->doWork();
126 $this->poolCounter->release();
127 return $result;
128
130 $result = $this->getCachedWork();
131 if ( $result === false ) {
132 /* That someone else work didn't serve us.
133 * Acquire the lock for me
134 */
135 return $this->execute( true );
136 }
137 return $result;
138
141 $result = $this->fallback();
142
143 if ( $result !== false ) {
144 return $result;
145 }
146 /* no break */
147
148 /* These two cases should never be hit... */
150 default:
151 $errors = [
152 PoolCounter::QUEUE_FULL => 'pool-queuefull',
153 PoolCounter::TIMEOUT => 'pool-timeout' ];
154
155 $status = Status::newFatal( $errors[$status->value] ?? 'pool-errorunknown' );
156 $this->logError( $status );
157 return $this->error( $status );
158 }
159 }
160}
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Class for dealing with PoolCounters using class members.
error( $status)
Do something with the error, like showing it to the user.
fallback()
A work not so good (eg.
getCachedWork()
Retrieve the work from cache.
PoolCounter $poolCounter
execute( $skipcache=false)
Get the result of the work (whatever it is), or the result of the error() function.
doWork()
Actually perform the work, caching it if needed.
logError( $status)
Log an error.
__construct( $type, $key)
When you have many workers (threads/servers) giving service, and a cached item expensive to produce e...
const QUEUE_FULL
static factory( $type, $key)
Create a Pool counter.
const LOCK_HELD