I want to dispatch a job to the queue and wait for it to finish, but only for a certain timespan (e.g. 10 seconds). If it does not finish in that timespan I want to do A, otherwise B.
How can this be accomplished? The only way I have seen is using events or the Queue triggers, but there seems to be no uniform way of checking if a certain job is finished.
Maybe I'm just blind and there is an easy solution, but I'm looking forward to your replies.
try this may it will help you
in your controller
use Carbon/Carbon;
public function yourmethodname (){
if(carbon::now()<=carbon::now()->addHours(2){
//dispatch a job to the queue
}else{
//do whatever you want here
}
}
you can add any time day or month in above carbon::now method for eg to add month carbon::now()->addMonths(whatever)
hope this will help you
Related
I'm reading docs but I'm not sure if this is possible on spring-cloud-stream using binder for kinesis.
I want to wait consuming messages from stream with some delay or configuration or by positive condition.
For example, I want wait 30 minutes after consumer process the message.
First aproximation is use condition with SPEL based on header message and current time, but the condition is created on startup. Then new Date is always the same.
I know that condition in below code is invalid.
#StreamListener(StreamProcessor.MY_STREAM, condition="#{headers['creation-date'] + 30minutes < new java.util.Date().getTime()}")
public void checkOut(Message<String> myMessage) {
//Do something
}
Do you know if is this possible without sleeping threads?
All you need is use Polled Consumer, this way you have full control over frequency, acks etc.
I need to design a rate limiter service for throttling requests.
For every incoming request a method will check if the requests per second has exceeded its limit or not. If it has exceeded then it will return the amount of time it needs to wait for being handled.
Looking for a simple solution which just uses system tick count and rps(request per second). Should not use queue or complex rate limiting algorithms and data structures.
Edit: I will be implementing this in c++. Also, note I don't want to use any data structures to store the request currently getting executed.
API would be like:
if (!RateLimiter.Limit())
{
do work
RateLimiter.Done();
}
else
reject request
The most common algorithm used for this is token bucket. There is no need to invent a new thing, just search for an implementation on your technology/language.
If your app is high avalaible / load balanced you might want to keep the bucket information on some sort of persistent storage. Redis is a good candidate for this.
I wrote Limitd is a different approach, is a daemon for limits. The application ask the daemon using a limitd client if the traffic is conformant. The limit is configured on the limitd server and the app is agnostic to the algorithm.
since you give no hint of language or platform I'll just give out some pseudo code..
things you are gonna need
a list of current executing requests
a wait to get notified where a requests is finished
and the code can be as simple as
var ListOfCurrentRequests; //A list of the start time of current requests
var MaxAmoutOfRequests;// just a limit
var AverageExecutionTime;//if the execution time is non deterministic the best we can do is have a average
//for each request ether execute or return the PROBABLE amount to wait
function OnNewRequest(Identifier)
{
if(count(ListOfCurrentRequests) < MaxAmoutOfRequests)//if we have room
{
Struct Tracker
Tracker.Request = Identifier;
Tracker.StartTime = Now; // save the start time
AddToList(Tracker) //add to list
}
else
{
return CalculateWaitTime()//return the PROBABLE time it will take for a 'slot' to be available
}
}
//when request as ended release a 'slot' and update the average execution time
function OnRequestEnd(Identifier)
{
Tracker = RemoveFromList(Identifier);
UpdateAverageExecutionTime(Now - Tracker.StartTime);
}
function CalculateWaitTime()
{
//the one that started first is PROBABLY the first to finish
Tracker = GetTheOneThatIsRunnigTheLongest(ListOfCurrentRequests);
//assume the it will finish in avg time
ProbableTimeToFinish = AverageExecutionTime - Tracker.StartTime;
return ProbableTimeToFinish
}
but keep in mind that there are several problems with this
assumes that by returning the wait time the client will issue a new request after the time as passed. since the time is a estimation, you can not use it to delay execution, or you can still overflow the system
since you are not keeping a queue and delaying the request, a client can be waiting for more time that what he needs.
and for last, since you do not what to keep a queue, to prioritize and delay the requests, this mean that you can have a live lock, where you tell a client to return later, but when he returns someone already took its spot, and he has to return again.
so the ideal solution should be a actual execution queue, but since you don't want one.. I guess this is the next best thing.
according to your comments you just what a simple (not very precise) requests per second flag. in that case the code can be something like this
var CurrentRequestCount;
var MaxAmoutOfRequests;
var CurrentTimestampWithPrecisionToSeconds
function CanRun()
{
if(Now.AsSeconds > CurrentTimestampWithPrecisionToSeconds)//second as passed reset counter
CurrentRequestCount=0;
if(CurrentRequestCount>=MaxAmoutOfRequests)
return false;
CurrentRequestCount++
return true;
}
doesn't seem like a very reliable method to control whatever.. but.. I believe it's what you asked..
I've been looking through the documentation on SDL_Events. Is there a way to remove an SDL_UserEvent from the event queue before it gets polled by SDL_PollEvent? I've tried googling "sdl remove event", but came up with bubkis.
You can examine events that are on the queue before taking them out of the queue... and then decide to take them out.
Both can be done with SDL_PeepEvents ( http://wiki.libsdl.org/SDL_PeepEvents ). You can look at the next N events by passing in SDL_PEEKEVENT as the parameter action; or take them out with SDL_GETEVENT.
It should be possible also to take them with SDL_GETEVENT; edit the events array you get to remove the event you wanted discarded; then call it again with SDL_ADDEVENT to put them back.
At this point, though, I'd have to wonder if you're really getting done what you wanted. Why not just poll events, and ignore events of the type you wanted discarded? That's the way I do it.
You can take a look at SDL_FlushEvent or SDL_FlushEvents.
SDL_FlushEvent can be used to remove one specific type of event from the queue, while SDL_FlushEvents can be used with values SDL_USEREVENT and SDL_LASTEVENT in order to remove every user events from the queue.
You might want to use SDL_PumpEvents before, just to update the event queue.
You can also take a look at SDL_PeepEvents (with the same minType and maxType as SDL_FlushEvents).
I don't know of an easy way to remove just ONE event of that type, but you can do:
SDL_FlushEvents(SDL_USEREVENT, SDL_LASTEVENT);
To remove all pending user events already on the queue.
void Game::handleEvent() {
SDL_Event event;
SDL_PumpEvents();
SDL_FlushEvent(SDL_JOYHATMOTION);
SDL_FlushEvent(SDL_JOYAXISMOTION);
SDL_FlushEvent(SDL_CONTROLLERAXISMOTION);
SDL_FlushEvent(SDL_MOUSEMOTION);
SDL_FlushEvent(SDL_MOUSEBUTTONDOWN);
SDL_FlushEvent(SDL_MOUSEBUTTONUP);
SDL_FlushEvent(SDL_FINGERDOWN);
SDL_FlushEvent(SDL_FINGERUP);
SDL_FlushEvent(SDL_FINGERMOTION);
if (SDL_PollEvent(&event))
scene->handleEvent(screen, event)
}
I have a concurrent collection that contains 100K items. The processing of each item in the collection can take as little as 100ms or as long as 10 seconds. I want to speed things up by parallelizing the processing, and have a 100 minions doing the work simultaneously. I also have to report some specific data to the UI as this processing occurs, not simply a percentage complete.
I want the parallelized sub-tasks to nibble away at the concurrent collection like a school of minnows attacking a piece of bread tossed into a pond. How do I expose the concurrent collection to the parallelized tasks? Can I have a normal loop and simply launch an async task inside the loop and pass it an IProgress? Do I even need the concurrent collection for this?
It has been recommended to me that I use Parallel.ForEach but I don't see how each sub-process established by the degrees of parallelism could report a custom object back to the UI with each item it processes, not only after it has finished processing its share of the 100K items.
The framework already provides the IProgress inteface for this purpose, and an implementation in Progress. To report progress, call IProgress.Report with a progressvalue. The value T can be any type, not just a number.
Each IProgress implementation can work in its own way. Progress raises an event and calls a callback you pass to it when you create it.
Additionally, Progress.Report executes asynchronously. Under the covers, it uses SychronizationContext.Post to execute its callback and all event handlers on the thread that created the Progress instance.
Assuming you create a progress value class like this:
class ProgressValue
{
public long Step{get;set;}
public string Message {get;set;}
}
You could write something like this:
IProgress<ProgressValue> myProgress=new Progress<ProgressValue>(p=>
{
myProgressBar.Value=p.Step;
});
IList<int> myVeryLargeList=...;
Parallel.ForEach(myVeryLargeList,item,state,step=>
{
//Do some heavy work
myProgress.Report(new ProgressValue
{
Step=step,
Message=String.Format("Processed step {0}",step);
});
});
EDIT
Oops! Progress implements IProgress explicitly. You have to cast it to IProgress , as #Tim noticed.
Fixed the code to explicitly declare myProgress as an IProgress.
I m developping a Winjs/HTML windows Store application .
I have to do some tests every period of time so let's me explain my need.
when i navigate to my specific page , I have to test (without a specific time in advance=loop)
So when my condition is verified it Will render a Flyout(Popup) and then exit from the Promise. (Set time out need a specific time but i need to verify periodically )
I read the msdn but i can't fullfill this goal .
If someone has an idea how to do it , i will be thankful.
Every help will be appreciated.
setInterval can be used.
var timerId = setInternal(function ()
{
// do you work.
}, 2000); // timer event every 2s
// invoke this when timer needs to be stopped or you move out of the page; that is unload() method
clearInternal(timerId);
Instead of polling at specific intervals, you should check if you can't adapt your code to use events or databinding instead.
In WinJS you can use databinding to bind input values to a view model and then check in its setter functions if your condition has been fulfilled.
Generally speaking, setInterval et al should be avoided for anything that's not really time-related domain logic (clocks, countdowns, timeouts or such). Of course there are situations when there's no other way (like polling remote services), so this may not apply to your situation at hand.