Skip to content

Commit 84a48f6

Browse files
committed
Checkpoint
1 parent 50f8a1b commit 84a48f6

20 files changed

+1501
-4
lines changed

AppConfig.pm.tt

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@ use constant mash_reference_sketch => '[% mash_reference_sketch %]';
1515
use constant binning_spades_threads => '[% binning_spades_threads %]';
1616
use constant binning_spades_ram => '[% binning_spades_ram %]';
1717

18+
use constant sched_db_host => '[% sched_db_host %]';
19+
use constant sched_db_user => '[% sched_db_user %]';
20+
use constant sched_db_pass => '[% sched_db_pass %]';
21+
use constant sched_db_name => '[% sched_db_name %]';
22+
1823
use base 'Exporter';
1924
our @EXPORT_OK = qw(data_api_url github_issue_repo_owner github_issue_repo_name github_issue_token
20-
db_host db_user db_pass db_name seedtk reference_data_dir
25+
db_host db_user db_pass db_name
26+
seedtk reference_data_dir
27+
sched_db_host sched_db_user sched_db_pass sched_db_name
2128
binning_spades_threads binning_spades_ram
2229
binning_genome_annotation_clientgroup mash_reference_sketch);
2330
our %EXPORT_TAGS = (all => [@EXPORT_OK]);

AppService.spec

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ module AppService
7070
typedef structure {
7171
task_id parent_id;
7272
workspace_id workspace;
73-
} StartParams;
73+
} start_params;
7474
funcdef start_app2(app_id, task_parameters params, StartParams start_params)
7575
returns (Task task);
7676

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ TPAGE_ARGS = \
6262
--define db_user=$(DB_USER) \
6363
--define db_pass=$(DB_PASS) \
6464
--define db_name=$(DB_NAME) \
65+
--define sched_db_host=$(SCHED_DB_HOST) \
66+
--define sched_db_user=$(SCHED_DB_USER) \
67+
--define sched_db_pass=$(SCHED_DB_PASS) \
68+
--define sched_db_name=$(SCHED_DB_NAME) \
6569
--define seedtk=$(SEEDTK) \
6670
--define github_issue_repo_owner=$(GITHUB_ISSUE_REPO_OWNER) \
6771
--define github_issue_repo_name=$(GITHUB_ISSUE_REPO_NAME) \

build-schema

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use strict;
2+
3+
use Bio::KBase::AppService::AppConfig qw(sched_db_host sched_db_user sched_db_pass sched_db_name);
4+
5+
my $dsn = 'dbi:mysql:' . sched_db_name . ";host=" . sched_db_host;
6+
7+
my @cmd = ('dbicdump',
8+
"-o", "dump_directory=lib",
9+
"-o", "preserve_case=1",
10+
"-o", 'components=["InflateColumn::DateTime"]',
11+
'Bio::KBase::AppService::Schema',
12+
$dsn, sched_db_user, sched_db_pass);
13+
14+
print "@cmd\n";
15+
system(@cmd);

lib/Bio/KBase/AppService/AppServiceImpl.pm

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use Data::Dumper;
2222
use Bio::KBase::AppService::Awe;
2323
use Bio::KBase::AppService::Shock;
2424
use Bio::KBase::AppService::Util;
25-
use Bio::KBase::DeploymentConfig;
25+
use Bio::P3::DeploymentConfig;
2626
use File::Slurp;
2727
use Data::UUID;
2828
use Plack::Request;
@@ -288,7 +288,7 @@ sub new
288288
bless $self, $class;
289289
#BEGIN_CONSTRUCTOR
290290

291-
my $cfg = Bio::KBase::DeploymentConfig->new($ENV{KB_SERVICE_NAME} || "AppService");
291+
my $cfg = Bio::P3::DeploymentConfig->new($ENV{KB_SERVICE_NAME} || "AppService");
292292
my $awe_server = $cfg->setting("awe-server");
293293
$self->{awe_server} = $awe_server;
294294
my $shock_server = $cfg->setting("shock-server");

lib/Bio/KBase/AppService/AppSpecs.pm

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#
2+
# Manage app-spec files.
3+
#
4+
5+
package Bio::KBase::AppService::AppSpecs;
6+
7+
use strict;
8+
use Data::Dumper;
9+
use base 'Class::Accessor';
10+
use JSON::XS;
11+
use File::Slurp;
12+
13+
__PACKAGE__->mk_accessors(qw(dir));
14+
15+
sub new
16+
{
17+
my($class, $dir) = @_;
18+
my $self = {
19+
dir => $dir,
20+
};
21+
return bless $self, $class;
22+
}
23+
24+
sub enumerate
25+
{
26+
my($self) = @_;
27+
28+
my $dh;
29+
my $dir = $self->dir;
30+
31+
#
32+
# We allow relaxed parsing of app definition files so that
33+
# we may put comments into them.
34+
#
35+
36+
my $json = JSON::XS->new->relaxed(1);
37+
38+
my @list;
39+
40+
if (!$dir) {
41+
warn "No app directory specified\n";
42+
} elsif (opendir($dh, $dir)) {
43+
my @files = sort { $a cmp $b } grep { /\.json$/ && -f "$dir/$_" } readdir($dh);
44+
closedir($dh);
45+
for my $f (@files)
46+
{
47+
my $obj = $json->decode(scalar read_file("$dir/$f"));
48+
if (!$obj)
49+
{
50+
warn "Could not read $dir/$f\n";
51+
}
52+
else
53+
{
54+
push(@list, $obj);
55+
}
56+
}
57+
} else {
58+
warn "Could not open app-dir $dir: $!";
59+
}
60+
return @list;
61+
}
62+
63+
sub find
64+
{
65+
my($self, $app_id) = @_;
66+
67+
my $dh;
68+
my $dir = $self->dir;
69+
70+
my @list;
71+
72+
#
73+
# We allow relaxed parsing of app definition files so that
74+
# we may put comments into them.
75+
#
76+
77+
my $json = JSON::XS->new->relaxed(1);
78+
79+
if (!$dir) {
80+
warn "No app directory specified\n";
81+
} elsif (opendir($dh, $dir)) {
82+
my @files = grep { /\.json$/ && -f "$dir/$_" } readdir($dh);
83+
closedir($dh);
84+
for my $f (@files)
85+
{
86+
my $obj = $json->decode(scalar read_file("$dir/$f"));
87+
if (!$obj)
88+
{
89+
warn "Could not read $dir/$f\n";
90+
}
91+
else
92+
{
93+
if ($obj->{id} eq $app_id)
94+
{
95+
return $obj;
96+
}
97+
}
98+
}
99+
} else {
100+
warn "Could not open app-dir $dir: $!";
101+
}
102+
return undef;
103+
}
104+
105+
1;

lib/Bio/KBase/AppService/Scheduler.pm

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package Bio::KBase::AppService::Scheduler;
2+
3+
use 5.010;
4+
use strict;
5+
use Bio::KBase::AppService::Schema;
6+
use Bio::KBase::AppService::AppConfig qw(sched_db_host sched_db_user sched_db_pass sched_db_name);
7+
use base 'Class::Accessor';
8+
use Data::Dumper;
9+
use Try::Tiny;
10+
use DateTime;
11+
use JSON::XS;
12+
13+
__PACKAGE__->mk_accessors(qw(schema specs json));
14+
15+
sub new
16+
{
17+
my($class, %opts) = @_;
18+
19+
my $schema = Bio::KBase::AppService::Schema->connect("dbi:mysql:" . sched_db_name . ";host=" . sched_db_host,
20+
sched_db_user, sched_db_pass);
21+
$schema or die "Cannot connect to database: " . Bio::KBase::AppService::Schema->errstr;
22+
$schema->storage->ensure_connected();
23+
my $self = {
24+
schema => $schema,
25+
json => JSON::XS->new->pretty(1)->canonical(1),
26+
%opts,
27+
};
28+
29+
return bless $self, $class;
30+
}
31+
32+
=head2 Methods
33+
34+
=over 4
35+
36+
=item B<start_app>
37+
38+
$sched->start_app($app_id, $task-parameters, $start_parameters)
39+
40+
Start the given app. Validates the app ID and creates the scheduler record
41+
for it, in state Submitted. Registers an idle event for a task-start check.
42+
43+
=cut
44+
45+
sub start_app
46+
{
47+
my($self, $app_id, $task_parameters, $start_parameters) = @_;
48+
49+
my $app = $self->find_app($app_id);
50+
51+
#
52+
# Create our task.
53+
#
54+
55+
my $code = $self->schema->resultset('TaskState')->find({description => 'Submitted'});
56+
57+
my $task = $self->schema->resultset('Task')->create({
58+
parent_task => $start_parameters->{parent_id},
59+
state_code => $code,
60+
application_id => $app_id,
61+
submit_time => DateTime->now(),
62+
params => $self->json->encode($task_parameters),
63+
});
64+
65+
say "Created task " . $task->id;
66+
return $task;
67+
}
68+
69+
=item B<find_app>
70+
71+
Find this app in the database. If it is not there, use the AppSpecs instance
72+
to find the spec file in the filesystem. If that is not there, fail.
73+
74+
$app = $sched->find_app($app_id)
75+
76+
We return an Application result object.
77+
78+
=cut
79+
80+
sub find_app
81+
{
82+
my($self, $app_id) = @_;
83+
84+
my $coderef = sub {
85+
86+
my $rs = $self->schema->resultset('Application')->find($app_id);
87+
return $rs if $rs;
88+
89+
my $app = $self->specs->find($app_id);
90+
if (!$app)
91+
{
92+
die "Unable to find application '$app_id'";
93+
}
94+
95+
#
96+
# Construct our new app record.
97+
#
98+
$rs = $self->schema->resultset('Application')->create( {
99+
id => $app->{id},
100+
script => $app->{script},
101+
});
102+
103+
return $rs;
104+
};
105+
106+
my $rs;
107+
try {
108+
$rs = $self->schema->txn_do($coderef);
109+
} catch {
110+
my $error = shift;
111+
die "Failure creating app: $error";
112+
};
113+
return $rs;
114+
115+
}
116+
117+
=back
118+
119+
=cut
120+
121+
122+
1;

lib/Bio/KBase/AppService/Schema.pm

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use utf8;
2+
package Bio::KBase::AppService::Schema;
3+
4+
# Created by DBIx::Class::Schema::Loader
5+
# DO NOT MODIFY THE FIRST PART OF THIS FILE
6+
7+
use strict;
8+
use warnings;
9+
10+
use base 'DBIx::Class::Schema';
11+
12+
__PACKAGE__->load_namespaces;
13+
14+
15+
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2018-08-29 13:04:51
16+
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:X2dxIm3Dd3IdBiORLQB5Ug
17+
18+
19+
# You can replace this text with custom code or comments, and it will be preserved on regeneration
20+
1;

0 commit comments

Comments
 (0)