|
| 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; |
0 commit comments