$row A request row. * @param int $now Current Unix time. */ public static function decide( array $row, int $now ): string { $status = (string) ( $row['status'] ?? '' ); $age = $now - self::timestamp( $row['created_at'] ?? null, $now ); if ( Status::QUEUED === $status ) { return $age > self::QUEUE_TIMEOUT ? self::FAIL_NOT_STARTED : self::LEAVE; } if ( Status::RUNNING !== $status ) { return self::LEAVE; } $lease = $row['lease_until'] ?? null; if ( null !== $lease && self::timestamp( $lease, 0 ) > $now ) { return self::LEAVE; } $too_old = $age > self::MAX_AGE; if ( 0 === (int) ( $row['attempts'] ?? 0 ) ) { return $too_old ? self::FAIL_NOT_STARTED : self::REQUEUE; } if ( '' !== (string) ( $row['remote_id'] ?? '' ) && ! $too_old ) { return self::REPOLL; } return self::FAIL_TIMEOUT; } /** * Unix time of a UTC 'Y-m-d H:i:s' string, or $fallback when it cannot be read. * * @param mixed $value Column value. * @param int $fallback Value for an empty or broken column. */ public static function timestamp( $value, int $fallback ): int { if ( ! is_string( $value ) || '' === $value ) { return $fallback; } $time = strtotime( $value . ' UTC' ); return false === $time ? $fallback : $time; } }