1 package DBIx::Class::Schema::Role::AtQueryInterval;
4 use MooseX::AttributeHelpers;
5 use DBIx::Class::Schema::Job;
6 use DBIx::Class::Schema::QueryInterval;
7 use DBIx::Class::Schema::AtQueryInterval;
11 DBIx::Class::Schema::Role::AtQueryInterval; Execute code at query intervals
15 The follow will execute the 'do_something' method each and every 10 queries,
16 excute a subref each 20 queries, and do both each 30 queries. This first
17 example is the long, hard way.
19 ## ISA DBIx::Class::Schema::Job
21 my $job1 = $schema->create_job(
22 runs => 'do_something',
25 my $job2 = $schema->create_job(
26 runs => sub {warn 'queries counted'},
30 ## ISA DBIx::Class::Schema::QueryInterval
32 my $interval_10 = $schema->create_query_interval(every => 10);
33 my $interval_20 = $schema->create_query_interval(every => 20);
34 my $interval_30 = $schema->create_query_interval(every => 30);
37 ## USA DBIx::Class::Schema::AtQueryInterval
39 my $at1 = $schema->create_at_query_interval(
40 interval => $interval_10, job => $job1,
43 my $at2 = $schema->create_at_query_interval(
44 interval => $interval_20, job => $job2,
47 my $at3 = $schema->create_at_query_interval(
48 interval => $interval_30, job=>$job1,
51 my $at4 = $schema->create_at_query_interval(
52 interval => $interval_30, job=>$job2,
55 $schema->query_intervals([$1, $at2, $at3, $at4]);
57 Or you can take the express trip (assuming you are not creating any custom
58 Query Intervals, Jobs, etc.) Notice that this method allows jobs to be defined
59 as an arrayref to make it easier to defined multiple jobs for a given interval.
61 In order to perform the needed object instantiation, this class will use the
62 methods 'query_interval_class', 'job_class' and 'at_query_interval_class'.
64 $schema->create_and_add_at_query_intervals(
66 runs => 'do_something',
70 warn 'queries counted';
74 {runs => 'do_something'},
76 warn 'queries counted';
81 All the above sit in a DBIx::Class::Schema that consumes the proper roles and
82 defines a function which receives three arguments:
85 my ($job, $schema, $at_query_interval) = @_;
90 Sometime you'd like to perform certain housekeeping activities at preset query
91 intervals. For example, every 100 queries you want to update a reporting table
92 that contains denormalized information. This role allows you to assign a
93 scalar containing the name of a method in your schema class, an anonymous sub,
94 or an arrayref of either to a particular interval.
98 This package defines the following attributes.
100 =head2 query_interval_class
102 The default class used to create an interval class from a hash of initializing
107 has 'query_interval_class' => (
111 default=>'DBIx::Class::Schema::QueryInterval',
113 'create_query_interval' => 'new',
120 The default class used to create an job class from a hash of initializing
129 default=>'DBIx::Class::Schema::Job',
131 'create_job' => 'new',
136 =head2 at_query_interval_class
138 The default class used to create an job class from a hash of initializing
143 has 'at_query_interval_class' => (
147 default=>'DBIx::Class::Schema::AtQueryInterval',
149 'create_at_query_interval' => 'new',
154 =head2 at_query_intervals
156 This is an arrayref of L<DBIx::Class::Schema::AtQueryInterval> objects which
157 holds all the jobs that need to be run at the given interval.
161 has 'at_query_intervals' => (
163 metaclass => 'Collection::Array',
165 isa=>'ArrayRef[DBIx::Class::Schema::AtQueryInterval]',
167 push => 'add_at_query_interval',
174 This module defines the following methods.
176 =head2 execute_jobs_at_query_interval ($int)
178 Execute all the jobs which match the given interval
182 sub execute_jobs_at_query_interval {
183 my ($self, $query_count, @args) = @_;
185 foreach my $at ($self->at_query_intervals) {
187 $at->execute_if_matches($query_count, $self, @args);
193 =head2 create_and_add_at_query_intervals (%definitions)
195 Uses the shortcut method shown above to quickly build a plan from a simple perl
200 sub create_and_add_at_query_intervals {
201 my ($self, @definitions) = @_;
202 while (@definitions) {
203 my $interval = $self->normalize_query_interval(shift @definitions);
204 my @jobs = $self->normalize_to_jobs(shift @definitions);
205 foreach my $job (@jobs) {
206 my $at = $self->create_at_query_interval(interval=>$interval, job=>$job);
207 $self->add_at_query_interval($at);
213 =head2 normalize_query_interval ($hash||$obj)
215 Given an argument, make sure it's a L<DBIx::Class::Schema::QueryInterval>,
216 coercing it if neccessary.
220 sub normalize_query_interval {
221 my ($self, $arg) = @_;
222 if(blessed $arg && $arg->isa('DBIx::Class::Schema::QueryInterval')) {
225 return $self->create_query_interval($arg);
229 =head2 normalize_to_jobs ($hash||$obj||$arrayref)
231 Incoming jobs need to be normalized to an array, so that we can handle adding
232 multiple jobs per interval.
236 sub normalize_to_jobs {
237 my ($self, $arg) = @_;
238 my @jobs = ref $arg eq 'ARRAY' ? @$arg : ($arg);
239 return map {$self->normalize_job($_)} @jobs;
243 =head2 normalize_job ($hash||$obj)
245 Given an argument, make sure it's a L<DBIx::Class::Schema::Job>,
246 coercing it if neccessary.
251 my ($self, $arg) = @_;
252 if(blessed $arg && $arg->isa('DBIx::Class::Schema::Job')) {
255 return $self->create_job($arg);
262 See L<DBIx::Class> for more information regarding authors.
266 You may distribute this code under the same terms as Perl itself.