I have a small API, that I need to stop running if a certain time in the day is reached, say midnight for example, and then continue on to the rest of the program. And that independant from a call from a request. Beacause one could include a function that breaks the process by checking the Sy.time(), but that will only be executed if a request come through. My guess is that I have to modify some attribute of the $run, but can't find much about it on the internet. I know that one could get the pid and kill it with a system command, but I don't know if that solution makes sure that the rest of the program runs.
Does anyone have an idea?
Thanks in advance.
The file containing the function looks like : (my_file.R)
#* @param x My argument
#* @get /lag_lead
function(x){
x <- as.numeric(x)
c(x-1, x+1)
}
and the running script :
library(plumber)
mon_api <- plumb('my_file.R')
mon_api$run(port = 8000)
print('hello')
答案 0 :(得分:0)
一种方法是,使用import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.IBinder;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.ui.PlayerNotificationManager;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import org.atsevdev.radioromania.MainActivity.*;
public class MyServices extends Service {
private SimpleExoPlayer player1;
private PlayerNotificationManager playerNotificationManager;
public boolean clickCheck = false;
//importing urls from main
public String DigiFm = MainActivity.DigiFm;
public String radioZU = MainActivity.radio_zu;
public String europaFM = MainActivity.Eur_fm;
public String radio_acc_ro = MainActivity.radio_rom_acc;
public String radio_bucuresti = MainActivity.radiobuchuresti;
public String impactFM = MainActivity.impactfm;
public String radiopopular = MainActivity.Radio_popular;
public String PROfm = MainActivity.PROFMurl;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate(){
super.onCreate();
final Context context = this;
player1 = ExoPlayerFactory.newSimpleInstance(context, new DefaultTrackSelector());
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(
context, Util.getUserAgent(context, "RadioRomania"));
//////////////////////Media Sources/////////////////////////////////
final MediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).
createMediaSource(Uri.parse(DigiFm));
final MediaSource mediaSource0 = new ExtractorMediaSource.Factory(dataSourceFactory).
createMediaSource(Uri.parse(radioZU));
final MediaSource mediaSource1 = new ExtractorMediaSource.Factory(dataSourceFactory).
createMediaSource(Uri.parse(radiopopular));
final MediaSource mediaSource2 = new ExtractorMediaSource.Factory(dataSourceFactory).
createMediaSource(Uri.parse(europaFM));
final MediaSource mediaSource3 = new ExtractorMediaSource.Factory(dataSourceFactory).
createMediaSource(Uri.parse(radio_acc_ro));
final MediaSource mediaSource4 = new ExtractorMediaSource.Factory(dataSourceFactory).
createMediaSource(Uri.parse(PROfm));
final MediaSource mediaSource5 = new ExtractorMediaSource.Factory(dataSourceFactory).
createMediaSource(Uri.parse(radio_bucuresti));
final MediaSource mediaSource6 = new ExtractorMediaSource.Factory(dataSourceFactory).
createMediaSource(Uri.parse(impactFM));
player1.prepare(mediaSource);
player1.prepare(mediaSource0);
player1.prepare(mediaSource1);
player1.prepare(mediaSource2);
player1.prepare(mediaSource3);
player1.prepare(mediaSource4);
player1.prepare(mediaSource5);
player1.prepare(mediaSource6);
player1.setPlayWhenReady(clickCheck);
////////////////////////////////////////////////////////////////////////////////
playerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
context, "Channel ID", R.string.app_name, 0,
new PlayerNotificationManager.MediaDescriptionAdapter() {
@Override
public String getCurrentContentTitle(Player player) {
return null;
}
@Nullable
@Override
public PendingIntent createCurrentContentIntent(Player player) {
Intent intent = new Intent(context, MainActivity.class);
return PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
}
@Nullable
@Override
public String getCurrentContentText(Player player) {
return null;
}
@Nullable
@Override
public Bitmap getCurrentLargeIcon(Player player, PlayerNotificationManager.BitmapCallback callback) {
return null;
}
}
);
playerNotificationManager.setNotificationListener(new PlayerNotificationManager.NotificationListener() {
@Override
public void onNotificationStarted(int notificationId, Notification notification) {
startForeground(0, notification);
clickCheck = true;
}
@Override
public void onNotificationCancelled(int notificationId) {
stopSelf();
clickCheck = false;
}
});
playerNotificationManager.setPlayer(player1);
}
public int onStardCommand(Intent intent, int flats, int stardId){
return START_STICKY;
}
@Override
public void onDestroy(){
playerNotificationManager.setPlayer(null);
player1.release();
player1 = null;
}
}
设置超时,该超时是withTimeout
的包装器
setTimeLimit
其他一些超时解决方案,其中一个使用library(R-utils)
withTimeout(mon_api$run(port = 8000), timeout = 30)
# timeout is in seconds
# calculate timeout, using difftime between now and midnight
tmo <- as.numeric(difftime(as.POSIXct("2018-12-28 00:00:00"), Sys.time(), units = "secs"))
withTimeout(mon_api$run(port = 8000), timeout = tmo)
Time out an R command via something like try()