diff options
author | Daniel Friesel <derf@finalrewind.org> | 2019-04-23 21:30:31 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2019-04-23 21:30:31 +0200 |
commit | ffaa0c44a78bb4bab632fcb76b668215da4c0a82 (patch) | |
tree | e4379158253dbdfbc0b6217e351da5a410ad1074 /lib/Travelynx/Command | |
parent | 5af134ef1ec968597a8fb9feb44372657d178bb8 (diff) |
Automatic checkout \o/
Diffstat (limited to 'lib/Travelynx/Command')
-rw-r--r-- | lib/Travelynx/Command/work.pm | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/lib/Travelynx/Command/work.pm b/lib/Travelynx/Command/work.pm new file mode 100644 index 0000000..b8f8e52 --- /dev/null +++ b/lib/Travelynx/Command/work.pm @@ -0,0 +1,111 @@ +package Travelynx::Command::work; +use Mojo::Base 'Mojolicious::Command'; + +use DateTime; +use List::Util qw(first); + +has description => + 'Perform automatic checkout when users arrive at their destination'; + +has usage => sub { shift->extract_usage }; + +sub run { + my ($self) = @_; + + my $now = DateTime->now( time_zone => 'Europe/Berlin' ); + + my $db = $self->app->pg->db; + + for my $entry ( + $db->select( 'in_transit_str', '*', { cancelled => 0 } )->hashes->each ) + { + + my $uid = $entry->{user_id}; + my $dep = $entry->{dep_ds100}; + my $arr = $entry->{arr_ds100}; + my $train = $entry->{train_id}; + + $self->app->log->debug("Processing $uid"); + + eval { + if ( $now->epoch - $entry->{real_dep_ts} < 300 ) { + $self->app->log->debug(" - updating departure"); + my $status = $self->app->get_departures( $dep, 30, 30 ); + if ( $status->{errstr} ) { + die("get_departures($dep): $status->{errstr}\n"); + } + + my ($train) + = first { $_->train_id eq $train } @{ $status->{results} }; + + if ( not $train ) { + die("could not find train $train at $dep\n"); + } + + $db->update( + 'in_transit', + { real_departure => $train->departure }, + { user_id => $uid } + ); + } + }; + if ($@) { + $self->app->log->error("work($uid)/departure: $@"); + } + + eval { + if ( + $entry->{arr_name} + and ( not $entry->{real_arr_ts} + or $now->epoch - $entry->{real_arr_ts} < 60 ) + ) + { + $self->app->log->debug(" - updating arrival"); + my $status = $self->app->get_departures( $arr, 20, 220 ); + if ( $status->{errstr} ) { + die("get_departures($arr): $status->{errstr}\n"); + } + + my ($train) + = first { $_->train_id eq $train } @{ $status->{results} }; + + if ( not $train ) { + die("could not find train $train at $arr\n"); + } + + $db->update( + 'in_transit', + { + sched_arrival => $train->sched_arrival, + real_arrival => $train->arrival, + }, + { user_id => $uid } + ); + } + elsif ( $entry->{real_arr_ts} ) { + $self->app->log->debug(" - checking out"); + my ( undef, $error ) = $self->app->checkout( $arr, 1, $uid ); + if ($error) { + die("${error}\n"); + } + } + }; + if ($@) { + $self->app->log->error("work($uid)/arrival: $@"); + } + + eval { } + } +} + +1; + +__END__ + +=head1 SYNOPSIS + + Usage: index.pl work + + Work Work Work. + + Should be called from a cronjob every three minutes or so. |