Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rajat unreachable url #1

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ Add the following inside the application manifest (inside `<application>`):
* **LogseneRequiresUnmeteredNetwork**: if logs should be shipped only on unmetered network connection
* **LogseneRequiresDeviceIdle**: if logs should be shipped only when device is idle
* **LogseneRequiresBatteryNotLow**: if logs should be shipped only when battery is not low
* **LogseneCheckUnreachableUrl**: checks if url is reachable before pushing logs
* **LogseneUnreachableUrlTimeout**: socket timout to check if url is reachable
* **LogseneAutomaticLocationEnabled**: if logs should be automatically enriched with device location information. See the **Enriching Logs with Location** section for more details.

Example Application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public class LogWorker extends Worker {

private LogseneClient client;
private String appToken;
private String receiverUrl;
private String type;
private boolean checkUnreachableUrl;
private int unreachableUrlTimeout;
private final Context context;

private SqliteObjectQueue preflightQueue;
Expand All @@ -41,10 +44,13 @@ public LogWorker(@NonNull Context context, @NonNull WorkerParameters params) {

@Override
public Result doWork() {
receiverUrl = getInputData().getString(Logsene.KEY_RECEIVERURL);
appToken = getInputData().getString(Logsene.KEY_APPTOKEN);
type = getInputData().getString(Logsene.KEY_TYPE);
checkUnreachableUrl = getInputData().getBoolean(Logsene.KEY_CHECK_UNREACHABLE_URL,false);
unreachableUrlTimeout = getInputData().getInt(Logsene.KEY_UNREACHABLE_URL_TIMEOUT, Logsene.DEFAULT_UNREACHABLE_TIMEOUT);

this.client = new LogseneClient(getInputData().getString(Logsene.KEY_RECEIVERURL), appToken);
this.client = new LogseneClient(receiverUrl, appToken);
this.preflightQueue = new SqliteObjectQueue(context);

long size = preflightQueue.size();
Expand Down Expand Up @@ -96,6 +102,10 @@ private boolean attemptExecute(Bulk bulk, int leftAttempts) {
leftAttempts -= 1;
try {
Log.d(LOG_TAG, "Attempting to send bulk request");
if(checkUnreachableUrl && !Utils.isURLReachable(receiverUrl, unreachableUrlTimeout)){
Log.e(LOG_TAG, "Unreachable URL");
return attemptExecute(bulk, leftAttempts);
}
ApiResponse result = client.execute(bulk);
if (!result.isSuccessful()) {
Log.e(LOG_TAG, String.format("Bad status code (%d) returned from api. Response: %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class Logsene {
public static final String KEY_APPTOKEN = "LOGSENE_APPTOKEN";
public static final String KEY_TYPE = "LOGSENE_TYPE";

public static final String KEY_CHECK_UNREACHABLE_URL = "LOGSENE_CHECK_UNREACHABLE_URL";
public static final String KEY_UNREACHABLE_URL_TIMEOUT = "LOGSENE_UNREACHABLE_URL_TIMEOUT";
public static final int DEFAULT_UNREACHABLE_TIMEOUT = 500;
private final String TAG = getClass().getSimpleName();
private final String FLUSH_WORKER_TAG = "com.sematext.android.LogWorker.unconstrained";
private final String INTERVAL_WORKER_TAG = "com.sematext.android.LogWorker.interval";
Expand Down Expand Up @@ -86,6 +89,8 @@ public class Logsene {
private boolean sendRequiresBatteryNotLow;
private boolean isActive;
private boolean automaticLocationEnabled;
private boolean checkUnreachableUrl;
private int unreachableUrlTimeout;
private LogseneLocationListener locationListener;

// Private constructor - no instances.
Expand Down Expand Up @@ -406,6 +411,8 @@ private void config(Context context) {
sendRequiresDeviceIdle = data.getBoolean("LogseneSendRequiresDeviceIdle", false);
sendRequiresBatteryNotLow = data.getBoolean("LogseneSendRequiresBatteryNotLow", false);
automaticLocationEnabled = data.getBoolean("LogseneAutomaticLocationEnabled", false);
checkUnreachableUrl = data.getBoolean("LogseneCheckUnreachableUrl", false);
unreachableUrlTimeout = (data.getInt("LogseneUnreachableUrlTimeout", DEFAULT_UNREACHABLE_TIMEOUT));

Log.d(TAG, String.format("Logsene is configured:\n"
+ " Type: %s\n"
Expand All @@ -426,6 +433,8 @@ private Data getWorkerData() {
.putString(KEY_RECEIVERURL, receiverUrl)
.putString(KEY_APPTOKEN, appToken)
.putString(KEY_TYPE, type)
.putBoolean(KEY_CHECK_UNREACHABLE_URL, checkUnreachableUrl)
.putInt(KEY_UNREACHABLE_URL_TIMEOUT, unreachableUrlTimeout)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

import androidx.core.app.ActivityCompat;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
Expand Down Expand Up @@ -61,4 +65,23 @@ public static boolean checkLocationPermissions(Context context) {
return finePermissionState == PackageManager.PERMISSION_GRANTED ||
coarsePermissionState == PackageManager.PERMISSION_GRANTED;
}

/**
* @param url URL
* @param timeout for the socket connection
* @return <code>true</code> if url is reachable
*/
public static boolean isURLReachable(String url, int timeout) {
try {
final URL u = new URL(url);
final Socket socket = new Socket();
socket.setSoTimeout(timeout);
socket.connect(new InetSocketAddress(u.getHost(), 80),timeout);
boolean isConnected = socket.isConnected();
socket.close();
return isConnected;
}catch (IOException e){
return false;
}
}
}