summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Command/work.pm
blob: fcb505cc5753b2ff8bfae50c3c924675b921602e (plain)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package Travelynx::Command::work;

# Copyright (C) 2020-2023 Birte Kristina Friesel
#
# SPDX-License-Identifier: AGPL-3.0-or-later
use Mojo::Base 'Mojolicious::Command';
use Mojo::Promise;

use DateTime;
use JSON;
use List::Util;

has description => 'Update real-time data of active journeys';

has usage => sub { shift->extract_usage };

sub run {
	my ($self) = @_;

	my $now              = DateTime->now( time_zone => 'Europe/Berlin' );
	my $checkin_deadline = $now->clone->subtract( hours => 48 );
	my $json             = JSON->new;

	if ( -e 'maintenance' ) {
		$self->app->log->debug('work: "maintenance" file found, aborting');
		return;
	}

	my $num_incomplete = $self->app->in_transit->delete_incomplete_checkins(
		earlier_than => $checkin_deadline );

	if ($num_incomplete) {
		$self->app->log->debug("Removed ${num_incomplete} incomplete checkins");
	}

	my $errors = 0;

	for my $entry ( $self->app->in_transit->get_all_active ) {

		if ( -e 'maintenance' ) {
			$self->app->log->debug('work: "maintenance" file found, aborting');
			return;
		}

		my $uid      = $entry->{user_id};
		my $dep      = $entry->{dep_eva};
		my $arr      = $entry->{arr_eva};
		my $train_id = $entry->{train_id};

		if ( $entry->{is_hafas} ) {

			eval {

				$self->app->hafas->get_journey_p(
					trip_id => $train_id,
					service => $entry->{backend_name}
				)->then(
					sub {
						my ($journey) = @_;

						my $found_dep;
						my $found_arr;
						for my $stop ( $journey->route ) {
							if ( $stop->loc->eva == $dep ) {
								$found_dep = $stop;
							}
							if ( $arr and $stop->loc->eva == $arr ) {
								$found_arr = $stop;
								last;
							}
						}
						if ( not $found_dep ) {
							$self->app->log->debug(
								"Did not find $dep within journey $train_id");
							return;
						}

						if ( $found_dep->rt_dep ) {
							$self->app->in_transit->update_departure_hafas(
								uid     => $uid,
								journey => $journey,
								stop    => $found_dep,
								dep_eva => $dep,
								arr_eva => $arr
							);
							if (    $entry->{backend_id} <= 1
								and $journey->class <= 16
								and $found_dep->rt_dep->epoch > $now->epoch )
							{
								$self->app->add_wagonorder(
									uid          => $uid,
									train_id     => $journey->id,
									is_departure => 1,
									eva          => $dep,
									datetime     => $found_dep->sched_dep,
									train_type   => $journey->type,
									train_no     => $journey->number,
								);
								$self->app->add_stationinfo( $uid, 1,
									$journey->id, $found_dep->loc->eva );
							}
						}

						if ( $found_arr and $found_arr->rt_arr ) {
							$self->app->in_transit->update_arrival_hafas(
								uid     => $uid,
								journey => $journey,
								stop    => $found_arr,
								dep_eva => $dep,
								arr_eva => $arr
							);
							if (    $entry->{backend_id} <= 1
								and $journey->class <= 16
								and $found_arr->rt_arr->epoch - $now->epoch
								< 600 )
							{
								$self->app->add_wagonorder(
									uid        => $uid,
									train_id   => $journey->id,
									is_arrival => 1,
									eva        => $arr,
									datetime   => $found_arr->sched_dep,
									train_type => $journey->type,
									train_no   => $journey->number,
								);
								$self->app->add_stationinfo( $uid, 0,
									$journey->id, $found_dep->loc->eva,
									$found_arr->loc->eva );
							}
						}
					}
				)->catch(
					sub {
						my ($err) = @_;
						if ( $err
							=~ m{svcResL\[0\][.]err is (?:FAIL|PARAMETER)$} )
						{
							# HAFAS do be weird. These are not actionable.
							$self->app->log->debug("work($uid)/journey: $err");
						}
						else {
							$self->app->log->error("work($uid)/journey: $err");
						}
					}
				)->wait;

				if (    $arr
					and $entry->{real_arr_ts}
					and $now->epoch - $entry->{real_arr_ts} > 600 )
				{
					$self->app->checkout_p(
						station => $arr,
						force   => 2,
						dep_eva => $dep,
						arr_eva => $arr,
						uid     => $uid
					)->wait;
				}
			};
			if ($@) {
				$errors += 1;
				$self->app->log->error("work($uid)/hafas: $@");
			}
			next;
		}

		# TODO irgendwo ist hier ne race condition wo ein neuer checkin (in HAFAS) mit IRIS-Daten überschrieben wird.
		# Die ganzen updates brauchen wirklich mal sanity checks mit train id ...

		# Note: IRIS data is not always updated in real-time. Both departure and
		# arrival delays may take several minutes to appear, especially in case
		# of large-scale disturbances. We work around this by continuing to
		# update departure data for up to 15 minutes after departure and
		# delaying automatic checkout by at least 10 minutes.

		eval {
			if ( $now->epoch - $entry->{real_dep_ts} < 900 ) {
				my $status = $self->app->iris->get_departures(
					station    => $dep,
					lookbehind => 30,
					lookahead  => 30
				);
				if ( $status->{errstr} ) {
					die("get_departures($dep): $status->{errstr}\n");
				}

				my ($train) = List::Util::first { $_->train_id eq $train_id }
				@{ $status->{results} };

				if ( not $train ) {
					$self->app->log->debug(
						"could not find train $train_id at $dep\n");
					return;
				}

				$self->app->in_transit->update_departure(
					uid     => $uid,
					train   => $train,
					dep_eva => $dep,
					arr_eva => $arr,
					route   => [ $self->app->iris->route_diff($train) ]
				);

				if ( $train->departure_is_cancelled and $arr ) {
					my $checked_in
					  = $self->app->in_transit->update_departure_cancelled(
						uid     => $uid,
						train   => $train,
						dep_eva => $dep,
						arr_eva => $arr,
					  );

					# depending on the amount of users in transit, some time may
					# have passed between fetching $entry from the database and
					# now. Only check out if the user is still checked into this
					# train.
					if ($checked_in) {

						# check out (adds a cancelled journey and resets journey state
						# to checkin
						$self->app->checkout_p(
							station => $arr,
							force   => 2,
							dep_eva => $dep,
							arr_eva => $arr,
							uid     => $uid
						)->wait;
					}
				}
				else {
					$self->app->add_route_timestamps( $uid, $train, 1 );
					$self->app->add_wagonorder(
						uid          => $uid,
						train_id     => $train->train_id,
						is_departure => 1,
						eva          => $dep,
						datetime     => $train->sched_departure,
						train_type   => $train->type,
						train_no     => $train->train_no
					);
					$self->app->add_stationinfo( $uid, 1, $train->train_id,
						$dep, $arr );
				}
			}
		};
		if ($@) {
			$errors += 1;
			$self->app->log->error("work($uid)/departure: $@");
		}

		eval {
			if (
				$arr
				and ( not $entry->{real_arr_ts}
					or $now->epoch - $entry->{real_arr_ts} < 600 )
			  )
			{
				my $status = $self->app->iris->get_departures(
					station    => $arr,
					lookbehind => 20,
					lookahead  => 220
				);
				if ( $status->{errstr} ) {
					die("get_departures($arr): $status->{errstr}\n");
				}

				# Note that a train may pass the same station several times.
				# Notable example: S41 / S42 ("Ringbahn") both starts and
				# terminates at Berlin Südkreuz
				my ($train) = List::Util::first {
					$_->train_id eq $train_id
					  and $_->sched_arrival
					  and $_->sched_arrival->epoch > $entry->{sched_dep_ts}
				}
				@{ $status->{results} };

				$train //= List::Util::first { $_->train_id eq $train_id }
				@{ $status->{results} };

				if ( not $train ) {

					# If we haven't seen the train yet, its arrival is probably
					# too far in the future. This is not critical.
					return;
				}

				my $checked_in = $self->app->in_transit->update_arrival(
					uid     => $uid,
					train   => $train,
					route   => [ $self->app->iris->route_diff($train) ],
					dep_eva => $dep,
					arr_eva => $arr,
				);

				if ( $checked_in and $train->arrival_is_cancelled ) {

					# check out (adds a cancelled journey and resets journey state
					# to destination selection)
					$self->app->checkout_p(
						station => $arr,
						force   => 0,
						dep_eva => $dep,
						arr_eva => $arr,
						uid     => $uid
					)->wait;
				}
				else {
					$self->app->add_route_timestamps(
						$uid, $train, 0,
						(
							defined $entry->{real_arr_ts}
							  and $now->epoch > $entry->{real_arr_ts}
						) ? 1 : 0
					);
					$self->app->add_wagonorder(
						uid        => $uid,
						train_id   => $train->train_id,
						is_arrival => 1,
						eva        => $arr,
						datetime   => $train->sched_departure,
						train_type => $train->type,
						train_no   => $train->train_no
					);
					$self->app->add_stationinfo( $uid, 0, $train->train_id,
						$dep, $arr );
				}
			}
			elsif ( $entry->{real_arr_ts} ) {
				my ( undef, $error ) = $self->app->checkout_p(
					station => $arr,
					force   => 2,
					dep_eva => $dep,
					arr_eva => $arr,
					uid     => $uid
				)->catch(
					sub {
						my ($error) = @_;
						$self->app->log->error("work($uid)/arrival: $error");
						$errors += 1;
					}
				)->wait;
			}
		};
		if ($@) {
			$self->app->log->error("work($uid)/arrival: $@");
			$errors += 1;
		}

		eval { };
	}

	my $started_at       = $now;
	my $main_finished_at = DateTime->now( time_zone => 'Europe/Berlin' );
	my $worker_duration  = $main_finished_at->epoch - $started_at->epoch;

	if ( $self->app->config->{influxdb}->{url} ) {
		if ( $self->app->mode eq 'development' ) {
			$self->app->log->debug( 'POST '
				  . $self->app->config->{influxdb}->{url}
				  . " worker runtime_seconds=${worker_duration},errors=${errors}"
			);
		}
		else {
			$self->app->ua->post_p( $self->app->config->{influxdb}->{url},
				"worker runtime_seconds=${worker_duration},errors=${errors}" )
			  ->wait;
		}
	}

	if ( not $self->app->config->{traewelling}->{separate_worker} ) {
		$self->app->start('traewelling');
	}

	# add_wagonorder and add_stationinfo assume a permanently running IOLoop
	# and do not allow Mojolicious commands to wait until they have completed.
	# Hence, some add_wagonorder and add_stationinfo calls made here may not
	# complete before the work command exits, and thus have no effect.
	#
	# This is not ideal and will need fixing at some point.  Until then, here
	# is the pragmatic solution for 99% of the associated issues.
	Mojo::Promise->timer(5)->wait;
}

1;

__END__

=head1 SYNOPSIS

  Usage: index.pl work

  Work Work Work.

  Should be called from a cronjob every three minutes or so.