~ubuntu-branches/debian/squeeze/movabletype-opensource/squeeze

« back to all changes in this revision

Viewing changes to lib/MT/TheSchwartz.pm

  • Committer: Bazaar Package Importer
  • Author(s): Dominic Hargreaves
  • Date: 2009-06-19 23:03:15 UTC
  • mfrom: (1.2.1 upstream) (9.1.1 karmic)
  • Revision ID: james.westby@ubuntu.com-20090619230315-rm1vcgg5iymu2zh1
Tags: 4.2.6.1-1
* New upstream release
* Update Standards-Version (no changes)
* Don't specify full path to apache2ctl in postinst (thanks, Lintian)
* Remove unused Lintian overrides
* Don't install empty directory in extlib

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
 
1
# Movable Type (r) Open Source (C) 2001-2009 Six Apart, Ltd.
2
2
# This program is distributed under the terms of the
3
3
# GNU General Public License, version 2.
4
4
#
5
 
# $Id: TheSchwartz.pm 2769 2008-07-14 19:56:22Z bchoate $
 
5
# $Id: TheSchwartz.pm 3733 2009-05-18 20:27:46Z jmarcotte $
6
6
 
7
7
package MT::TheSchwartz;
8
8
 
9
9
use strict;
10
10
use base qw( TheSchwartz );
11
 
use MT::ObjectDriver::Driver::DBI;
 
11
use MT::ObjectDriver::Driver::Cache::RAM;
12
12
use List::Util qw( shuffle );
13
13
 
14
14
my $instance;
105
105
}
106
106
 
107
107
sub driver_for {
108
 
    my MT::TheSchwartz $client = shift;
109
 
    return MT::Object->dbi_driver;
 
108
    require MT::TheSchwartz::Job;
 
109
    return MT::TheSchwartz::Job->dbi_driver;
110
110
}
111
111
 
112
112
sub shuffled_databases {
126
126
    return 0;
127
127
}
128
128
 
 
129
sub _has_enough_swap {
 
130
    my $memory_module;
 
131
    eval {
 
132
        require Sys::MemInfo;
 
133
        $memory_module = q{Sys::MemInfo};
 
134
    };
 
135
 
 
136
    my ($mem_limit) = @_;
 
137
    if ( !defined($mem_limit) ) {
 
138
        $mem_limit = MT->config('SchwartzSwapMemoryLimit');
 
139
    }
 
140
 
 
141
    if ( $mem_limit && $memory_module ) {
 
142
        my $decoded_limit;
 
143
        if ( $mem_limit =~ /^\d+[KGM]B?$/ ) {
 
144
            my $multiplier = 1;
 
145
            ( $mem_limit =~ /GB?$/ ) and $multiplier = 1073741824;
 
146
            ( $mem_limit =~ /MB?$/ ) and $multiplier = 1048576;
 
147
            ( $mem_limit =~ /KB?$/ ) and $multiplier = 1024;
 
148
            $mem_limit =~ s/[KGM]B?$//;
 
149
            $mem_limit = $mem_limit * $multiplier;
 
150
        }
 
151
        if ( $mem_limit =~ /\d+/ ) {
 
152
            my $swap;
 
153
            if ( $memory_module eq q{Sys::MemInfo} ) {
 
154
                $swap = Sys::MemInfo::get("freeswap");
 
155
            }
 
156
 
 
157
            # not enough swap, lets get out of here!
 
158
            if ( $swap < $mem_limit ) {
 
159
                return 0;
 
160
            }
 
161
        }
 
162
    }
 
163
 
 
164
    # default to returning true
 
165
    # i.e., yes there is enough
 
166
    return 1;
 
167
}
 
168
 
 
169
sub _has_enough_memory {
 
170
    my $memory_module;
 
171
    eval {
 
172
        require Sys::MemInfo;
 
173
        $memory_module = q{Sys::MemInfo};
 
174
    };
 
175
 
 
176
    my ($mem_limit) = @_;
 
177
    if ( !defined($mem_limit) ) {
 
178
        $mem_limit = MT->config('SchwartzFreeMemoryLimit');
 
179
    }
 
180
 
 
181
    if ( $mem_limit && $memory_module ) {
 
182
        my $decoded_limit;
 
183
        if ( $mem_limit =~ /^\d+[KGM]B?$/ ) {
 
184
            my $multiplier = 1;
 
185
            ( $mem_limit =~ /GB?$/ ) and $multiplier = 1073741824;
 
186
            ( $mem_limit =~ /MB?$/ ) and $multiplier = 1048576;
 
187
            ( $mem_limit =~ /KB?$/ ) and $multiplier = 1024;
 
188
            $mem_limit =~ s/[KGM]B?$//;
 
189
            $mem_limit = $mem_limit * $multiplier;
 
190
        }
 
191
        if ( $mem_limit =~ /\d+/ ) {
 
192
            my $free;
 
193
            if ( $memory_module eq q{Sys::MemInfo} ) {
 
194
                $free = Sys::MemInfo::get("freemem");
 
195
            }
 
196
 
 
197
            # not enough free, lets get out of here!
 
198
            if ( $free < $mem_limit ) {
 
199
                return 0;
 
200
            }
 
201
        }
 
202
    }
 
203
 
 
204
    # default to returning true
 
205
    # i.e., yes there is enough
 
206
    return 1;
 
207
}
 
208
 
129
209
# Replacement for TheSchwartz::get_server_time
130
210
# to simply return value from dbd->sql_for_unixtime
131
211
# if it is a plain number (the driver has no function,
135
215
    my($driver) = @_;
136
216
    my $unixtime_sql = $driver->dbd->sql_for_unixtime;
137
217
    return $unixtime_sql if $unixtime_sql =~ m/^\d+$/;
138
 
    return $driver->rw_handle->selectrow_array("SELECT $unixtime_sql");
 
218
    return $driver->r_handle->selectrow_array("SELECT $unixtime_sql");
 
219
}
 
220
 
 
221
sub work_until_done {
 
222
    my TheSchwartz $client = shift;
 
223
    if ( ! $client ) {
 
224
        return;
 
225
    }
 
226
    my $cap = MT->config('SchwartzClientDeadline'); # in seconds
 
227
    my $mem_limit = MT->config('SchwartzFreeMemoryLimit');
 
228
    $mem_limit ||= 0;
 
229
    my $swap_limit = MT->config('SchwartzSwapMemoryLimit');
 
230
    $swap_limit ||= 0;
 
231
    my $deadline;
 
232
    if ( $cap ) {
 
233
        $deadline = time() + $cap;
 
234
        while ( time() < $deadline ) {
 
235
            $client->work_once or last;
 
236
            last unless _has_enough_memory( $mem_limit );
 
237
            last unless _has_enough_memory( $swap_limit );
 
238
        }
 
239
    }
 
240
    else {
 
241
        while ( 1 ) {
 
242
            $client->work_once or last;
 
243
            last unless _has_enough_memory( $mem_limit );
 
244
            last unless _has_enough_swap( $swap_limit );
 
245
        }
 
246
    }
139
247
}
140
248
 
141
249
sub work_periodically {
168
276
        }
169
277
 
170
278
        if ($did_work) {
171
 
            my $driver = MT::Object->driver;
172
 
            $driver->clear_cache
173
 
                if $driver->can('clear_cache');
 
279
            # Clear RAM cache
 
280
            MT::ObjectDriver::Driver::Cache::RAM->clear_cache;
 
281
 
174
282
            MT->request->reset();
 
283
 
175
284
            $did_work = 0;
176
285
            if ($OBJECT_REPORT) {
177
286
                my $report = leak_report(\%obj_start, \%obj_pre, \%Devel::Leak::Object::OBJECT_COUNT);