/
fullblown.t
361 lines (300 loc) · 11.9 KB
/
fullblown.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#! perl -w
use strict;
use warnings;
use Test::More;
use Test::MockDateTime;
use DateTime;
use Schedule::LongSteps;
use Class::Load;
# use Log::Any::Adapter qw/Stderr/;
BEGIN{
eval "use Test::mysqld";
plan skip_all => "Test::mysqld is required for this test" if $@;
eval "use DBIx::Class";
plan skip_all => "DBIx::Class is required for this test" if $@;
eval "use SQL::Translator";
plan skip_all => "SQL::Translator is required for this test" if $@;
eval "use DBIx::Class::InflateColumn::Serializer";
plan skip_all => "DBIx::Class::InflateColumn::Serializer is required for this test" if $@;
eval "use DateTime::Format::MySQL";
plan skip_all => "DateTime::Format::MySQL is required for this test" if $@;
eval "use Net::EmptyPort";
plan skip_all => "Net::EmptyPort is required for this test" if $@;
}
#
# This is a real life test. Using a common DB engine
#
my $test_mysql = Test::mysqld->new(my_cnf => {
port => Net::EmptyPort::empty_port()
}) or plan skip_all => $Test::mysqld::errstr;
#
# We build a DBIx::Class::Schema, starting with the Result classes.
#
{
package MyApp::Schema::Result::Patient;
use base qw/DBIx::Class::Core/;
__PACKAGE__->table('patient');
__PACKAGE__->add_columns(
id =>
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
name =>
{ data_type => 'varchar', is_nullable => 0, size => 50 },
looks_cancerous =>
{ data_type => 'integer', is_nullable => 0 , default_value => 0 },
family_history =>
{ data_type => 'integer', is_nullable => 0 , default_value => 0 },
);
__PACKAGE__->set_primary_key("id");
1;
}
{
package MyApp::Schema::Result::Process;
use base qw/DBIx::Class::Core/;
__PACKAGE__->table('longprocess');
__PACKAGE__->load_components(qw/InflateColumn::DateTime InflateColumn::Serializer/);
__PACKAGE__->add_columns(
id =>
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
process_class =>
{ data_type => "varchar", is_nullable => 0, size => 255 },
what =>
{ data_type => "varchar", is_nullable => 1, size => 255 },
status =>
{ data_type => "varchar", is_nullable => 0, size => 50 , default_value => 'pending' },
run_at =>
{ data_type => "datetime", datetime_undef_if_invalid => 1, is_nullable => 1 },
run_id =>
{ data_type => "varchar", is_nullable => 1, size => 36 },
state =>
{ data_type => "text",
serializer_class => 'JSON',
is_nullable => 0,
},
error =>
{ data_type => "text", is_nullable => 1 }
);
__PACKAGE__->set_primary_key("id");
sub sqlt_deploy_hook {
my ($self, $sqlt_table) = @_;
$sqlt_table->add_index(name => 'idx_longprocess_run_id', fields => ['run_id']);
$sqlt_table->add_index(name => 'idx_longprocess_run_at', fields => ['run_at']);
}
1;
}
{
package MyApp::Schema;
use base qw/DBIx::Class::Schema/;
__PACKAGE__->load_classes({ 'MyApp::Schema::Result' => [ 'Process', 'Patient' ] });
sub connection{
my ($class, @args ) = @_;
unless( ( ref $args[0] || '' ) eq 'CODE' ){
defined( $args[3] ) or ( $args[3] = {} );
$args[3]->{AutoCommit} = 1;
$args[3]->{RaiseError} = 1;
$args[3]->{mysql_enable_utf8} = 1;
## Only for mysql DSNs
$args[3]->{on_connect_do} = ["SET SESSION sql_mode = 'TRADITIONAL'"];
}
my $self = $class->next::method(@args);
return $self;
}
1;
}
#
# Then we build our test Process.
#
{
package MyMedicalProcess;
# Inspired by https://en.wikipedia.org/wiki/XPDL Medical process example
use Moose;
extends qw/Schedule::LongSteps::Process/;
use DateTime;
has 'schema' => ( is => 'ro', isa => 'DBIx::Class::Schema', required => 1);
has 'patient' => ( is => 'ro', lazy_build => 1 );
sub _build_patient{
my ($self) = @_;
return $self->schema()->resultset('Patient')->find($self->state()->{patient_id});
}
sub build_first_step{
my ($self) = @_;
return $self->new_step({ what => 'do_first_look', run_at => DateTime->now() });
}
sub do_first_look{
my ($self) = @_;
my $state = $self->state();
my $patient = $self->patient();
if( ! $patient->looks_cancerous() ){
return $self->final_step({ state => { %$state , has_cancer => 0 } });
}
return $self->new_step({ what => 'do_analyze_more', run_at => DateTime->now() });
}
sub do_analyze_more{
my ($self) = @_;
my $state = $self->state();
my $p1 = $self->longsteps->instantiate_process('AnalyzePatient', { schema => $self->schema() }, { %$state });
my $p2 = $self->longsteps->instantiate_process('AnalyzeFamily', { schema => $self->schema() } , { %$state });
return $self->new_step({ what => 'do_synthetize_analyzes', run_at => DateTime->now()->add( days => 3 ),
state => { %$state , processes => [ $p1->id(), $p2->id() ] } });
}
sub do_synthetize_analyzes{
my ($self) = @_;
return $self->wait_processes(
$self->state()->{processes},
sub{
my ( @processes ) = @_;
return $self->new_step({
what => 'do_prescribe',
run_at => DateTime->now(),
state => {
%{$self->state()},
map{ %{$_->state()} } @processes
}});
});
}
sub do_prescribe{
my ($self) = @_;
my $state = $self->state();
if( $self->patient()->looks_cancerous() &&
( $state->{has_cancer_cells} ||
$state->{has_cancer_history} )
){
return $self->final_step({ state => { %$state , have_treatment => 1 } });
}
return $self->final_step({ state => { %$state , have_treatment => 0 } });
}
__PACKAGE__->meta->make_immutable();
}
{
package AnalyzePatient;
use Moose;
extends qw/Schedule::LongSteps::Process/;
has 'schema' => ( is => 'ro', isa => 'DBIx::Class::Schema', required => 1);
sub build_first_step{
my ($self) = @_;
return $self->new_step({ what => 'do_analyze_patient', run_at => DateTime->now() });
}
sub do_analyze_patient{
my ($self) = @_;
return $self->final_step({ state => { has_cancer_cells => 0 } });
}
__PACKAGE__->meta->make_immutable();
}
{
package AnalyzeFamily;
use Moose;
extends qw/Schedule::LongSteps::Process/;
has 'schema' => ( is => 'ro', isa => 'DBIx::Class::Schema', required => 1);
has 'patient' => ( is => 'ro', lazy_build => 1);
sub _build_patient{
my ($self) = @_;
return $self->schema->resultset('Patient')->find($self->state()->{patient_id});
}
sub build_first_step{
my ($self) = @_;
return $self->new_step({ what => 'do_analyze_family', run_at => DateTime->now()->add(days => 2) });
}
sub do_analyze_family{
my ($self) = @_;
return $self->final_step({ state => { has_cancer_history => $self->patient->family_history() } });
}
__PACKAGE__->meta->make_immutable();
}
#
# Time to make all the wheels turn.
#
my $schema = MyApp::Schema->connect( $test_mysql->dsn(), '', '' );
$schema->deploy();
use_ok('Schedule::LongSteps::Storage::DBIxClass');
use_ok('Schedule::LongSteps::Storage::AutoDBIx');
my @longsteps = ();
my $storage_dbixclass = Schedule::LongSteps::Storage::DBIxClass->new({
schema => $schema,
resultset_name => 'Process'
});
my $long_steps_dbixclass = Schedule::LongSteps->new({
storage => $storage_dbixclass
});
push @longsteps , $long_steps_dbixclass;
my $storage_auto = Schedule::LongSteps::Storage::AutoDBIx->new({ get_dbh => sub{ return $schema->storage()->dbh(); } });
my $long_steps_auto = Schedule::LongSteps->new({
storage => $storage_auto
});
push @longsteps, $long_steps_auto;
my $dynamo_storage = undef;
if( $ENV{DYNAMODB_LOCAL} ){
my @to_load = qw/Paws
Paws::Credential::Explicit
Paws::Net::LWPCaller
Schedule::LongSteps::Storage::DynamoDB
/;
map{ Class::Load::load_class( $_ ) } @to_load;
my $dynamo_config = {
region => 'eu-west-1',
endpoint => $ENV{DYNAMODB_LOCAL},
access_key => 'foo',
secret_key => 'bar',
};
my $credentials = Paws::Credential::Explicit->new($dynamo_config);
my $caller = Paws::Net::LWPCaller->new();
my $dynamo_db = Paws->service(
'DynamoDB',
credentials => $credentials,
caller => $caller,
max_attempts => 10,
%{$dynamo_config}
);
$dynamo_storage = Schedule::LongSteps::Storage::DynamoDB->new({ dynamo_db => $dynamo_db, table_prefix => 'testdeletethis' });
$dynamo_storage->vivify_table();
my $long_steps_dynamo = Schedule::LongSteps->new({
storage => $dynamo_storage
});
diag("Will test DynamoDB (with DYNAMODB_LOCAL)");
push @longsteps, $long_steps_dynamo;
}
foreach my $long_steps ( @longsteps ){
# Build some data, a process and run it.
my $patient = $schema->resultset('Patient')->create({ name => 'Joe Foobar' });
my $cancerous_patient = $schema->resultset('Patient')->create({ name => 'Joe BarBaz' , looks_cancerous => 1 });
my $cancerous_family = $schema->resultset('Patient')->create({ name => 'Joe BarBaz' , looks_cancerous => 1 , family_history => 1 });
ok( my $healthy_process = $long_steps->instantiate_process('MyMedicalProcess', { schema => $schema } , { patient_id => $patient->id() }) );
ok( my $cancer_process = $long_steps->instantiate_process('MyMedicalProcess', { schema => $schema } , { patient_id => $cancerous_patient->id() }) );
ok( my $family_process = $long_steps->instantiate_process('MyMedicalProcess', { schema => $schema } , { patient_id => $cancerous_family->id() }) );
# This would run in a completely separate process
ok( $long_steps->run_due_processes({ schema => $schema }) );
$healthy_process->discard_changes(); # This is needed, cause the framework only does 'update'
$cancer_process->discard_changes();
is( $healthy_process->status(), 'terminated' );
is( $cancer_process->status(), 'paused' );
is( $cancer_process->what() , 'do_analyze_more');
ok( $long_steps->run_due_processes({ schema => $schema }) );
$cancer_process->discard_changes();
is( $cancer_process->what() , 'do_synthetize_analyzes');
# Some stuff should run now.
ok( $long_steps->run_due_processes({ schema => $schema }) );
# Simulate 3 days after now.
my $three_days = DateTime->now()->add( days => 3 )->add( hours => 1 );
on $three_days.'' => sub{
# And more stuff should run three days after
ok( $long_steps->run_due_processes({ schema => $schema }) );
# Give it another go.
$long_steps->run_due_processes({ schema => $schema });
$cancer_process->discard_changes();
is( $cancer_process->what() , 'do_prescribe' );
# Give it another go and check that the final state is reached.
ok( $long_steps->run_due_processes({ schema => $schema }) );
$cancer_process->discard_changes();
is( $cancer_process->status() , 'terminated' );
is( $cancer_process->state()->{have_treatment}, 0 );
$family_process->discard_changes();
is( $family_process->status() , 'terminated' );
is( $family_process->state()->{have_treatment}, 1 );
};
}
END{
if( $dynamo_storage ){
if( $dynamo_storage->table_exists() ){
$dynamo_storage->destroy_table('I am very sure and I am not insane');
}
}
done_testing();
}