Running a task in background using Quartz plugin - grails

I'm planning to have a view that presents a button so that when it is clicked, it will run a Quartz job and the page will finish loading successfully (no need to wait for the job to finish). Based on this documentation, you can have a custom trigger class. Can you help me implementing it?
My job:
class ReconciliationJob {
static triggers = {
custom name:'customTrigger', triggerClass:ReconciliationTrigger, targetDate:myValue
}
def execute() {
// execute task
}
}
How can I implement ReconciliationTrigger class? Also, I need to pass a parameter to the job too.
Thanks.

I think you've mixed up jobs and queues.
Quartz jobs are background tasks which run on a time-based trigger and are not designed to be kicked off by user-driven events.
Queues, such as JMS, allow you to send an asynchronous 'message' (method call) in the manner you describe. Take a look at the Grails JMS plugin and it might be what you're looking for.

Related

How to set up logback MDC in apache beam and dataflow?

We are using apache beam and would like to setup the logback MDC. logback MDC is a great GREAT resource when you have a request come in and you store let's say a userId (in our case, it's custId, fileId, requestId), then anytime a developer logs, it magically stamps that information on to the developers log. the developer no longer forgets to add it every log statement he adds.
I am starting in an end to end integration type test with apache beam direct runner embedded in our microservice for testing (in production, the microservice calls dataflow). currently, I am see that the MDC is good up until after the expand() methods are called. Once the processElement methods are called, the context is of course gone since I am in another thread.
So, trying to fix this piece first. Where should I put this context such that I can restore it at the beginning of this thread.
As an example, if I have an Executor.execute(runnable), then I simply transfer context using that runnable like so
public class MDCContextRunnable implements Runnable {
private final Map<String, String> mdcSnapshot;
private Runnable runnable;
public MDCContextRunnable(Runnable runnable) {
this.runnable = runnable;
mdcSnapshot = MDC.getCopyOfContextMap();
}
#Override
public void run() {
try {
MDC.setContextMap(mdcSnapshot);
runnable.run();
} Catch {
//Must log errors before mdc is cleared
log.error("message", e);. /// Logs error and MDC
} finally {
MDC.clear();
}
}
}
so I need to do the same with apache beam basically. I need to
Have a point to capture the MDC
Have a point to restore the MDC
Have a point to clear out the MDC to prevent it leaking to another request(really in case I missed something which seems to happen now and then)
Any ideas on how to do this?
oh, bonus points if it the MDC can be there when any exceptions are logged by the framework!!!! (ie. ideally, frameworks are supposed to do this for you but apache beam seems like it is not doing this. Most web frameworks have this built in).
thanks,
Dean
Based on the context and examples you gave, it sounds like you want to use MDC to automatically capture more information for your own DoFns. Your best bet for this is, depending on the lifetime you need your context available for, to use either the StartBundle/FinishBundle or Setup/Teardown methods on your DoFns to create your MDC context (see this answer for an explanation of the differences between the two). The important thing is that these methods are executed for each instance of a DoFn, meaning they will be called on the new threads created to execute these DoFns.
Under the Hood
I should explain what's happening here and how this approach differs from your original goal. The way Apache Beam executes is that your written pipeline executes on your own machine and performs pipeline construction (which is where all the expand calls are occurring). However, once a pipeline is constructed, it is sent to a runner which is often executing on a separate application unless it's the Direct Runner, and then the runner either directly executes your user code or runs it in a docker environment.
In your original approach it makes sense that you would successfully apply MDC to all logs until execution begins, because execution might not only be occurring in a different thread, but potentially also a different application or machine. However, the methods described above are executed as part of your user code, so setting up your MDC there will allow it to function on whatever thread/application/machine is executing transforms.
Just keep in mind that those methods get called for every DoFn and you will often have mutiple DoFns per thread, which is something you may need to be wary of depending on how MDC works.

Execute task after transaction ends - Grails

I need to execute some tasks after grails transaction ends. I'm not looking for afterCommit of Hibernate, instead of that, I need a method to be called after the transaction of the service ends.
Is there any way? I've read the documentation but I couldn't find anything, just normal events like afterUpdate or afterCommit.
Thanks!
You can add an event listener to the current session
sessionFactory.currentSession.addEventListener(...)
class MyListener extends BaseSessionEventListener {
#Override
void transactionCompletion(boolean successful) {
}
}
You just need to verify the transaction was successful via the parameter. Also understand that if there are multiple transactions in a single session the listener will be invoked for each one.
That really depends on what you're trying to do. The best bet is probably to call a #Transactional method on a service, and then when that returns, run the code that you need to happen after the transaction.
An alternative, if you just want to spin off some task to do something simple, you can use:
new java.util.Timer().runAfter(1000) { //time in milliseconds
//code to run here
}
This is just a shortcut to spin off a new thread and runs the closure body after (in the above example) 1 second... I believe the closure still has access to injected grails objects, but does not have access to the session. I'd double-check that though. I've used this in several places for sending notifications that needed to wait until after some processing inside a transaction ended.

How to use grails template rendering method in another async thread

My grails app is sending emails asynchronously in one of its controllers. Asynchronously, because it should be sent after a lengthy process. I am currently using the executor plugin and the runAsync closure.
def sendEmails() {
...
runAsync {
// ... Some lengthy process before emailing
myMailService.send('someone#somecompany.net',
g.render(template: 'mail', model: resultOfLengthyProcess))
}
...
}
I am running it after the lengthy process, because the model in the render function call contains the result of that process.
I want to use the g.render() method because the email is a big gsp template with lots of pictures and content.
Now that g.render call will fail, because it is being called from another thread. It throws java.lang.IllegalStateException with the message:
No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
How can I solve this issue? I am open to any answers/suggestions.
You need a request in order to use render directly, and with an asynchronous block you loose it.
However you should be able to achieve what you want by injecting the PageRender in your controller and calling it from the async block.
class MyController {
grails.gsp.PageRenderer groovyPageRenderer
def sendEmails() {
// ... Some lengthy process before emailing
myMailService.send(
'someone#somecompany.net',
groovyPageRenderer.render(template: 'mail', model: resultOfLengthyProcess)
)
}
}
I would suggest to use JMS (with the jms plugin), encapsulating the lengthy process in a service.

Grails creating routines

Let's say, i have a specific information in the database that needs to be sent for a specific user by email in a specific time of the day.
a) How can i create a routine in Grails, which is basically an action that is always running - without being associated with any event? Let's say, every hour that action is runned.
I was thinking about something like this:
while(true){
...
myCodeHere
...
wait 30minutes
}
Will this actually work? Without too much processing? And how can i have an action permanently running no matter what. I there is a specific way of doing this?
Thanks in advanced,
RR
The usual way to do this in a grails app is with the Quartz scheduler plugin. The plugin provides a simple cron-like DSL for scheduling jobs. For example, to run a job every 30 minutes, you could configure it like this:
class MyJob {
static cronExpression = "0 0/30 * * * ?"
def execute(){ /* do something useful */ }
}
If you want to run a background thread all the time, take a look at the executor plugin which provides an ExecutorService wrapped up properly to get a hibernate session.
Avoiding quartz and plugins you may use pure Spring Framework
1) add to container
<task:annotation-driven executor="executor" scheduler="scheduler"/>
<task:executor id="executor" pool-size="5"/>
<task:scheduler id="scheduler" pool-size="10"/>
(do not forget to define task and tx namespaces)
2) Create some bean and add method
#Scheduled(fixedDelay=4000)
public void method() {
// do something every 4 seconds
}
finish!
For more info see spring framework

How to get executing job's name?

In my application I have a static class (singleton) that needs to be initialized with some environmental variables that's used through out my layers, I'm calling it my applicationContext. That in turn has customer and user contexts.
As each job runs it modifies these customer and user contexts depending on the situation.
The problem I have is that when 2 jobs fires concurrently they might overwrite each others contexts, therefor I need to keep multiple user and customer contexts alive for each running job and I need to be able to pick the right context by somehow being able to see what the current job is.
Is it possible to somehow get information about the current executing quartz.net job?
I'm envisioning something like this where "currentQuartzJob.Name" is made up and is the part I'm missing:
public class MyContextImpl : IApplicationContext {
private Dictionary<string,subContexts> allCustomerContexts;
public string CurrentContext
{
get { return allCustomerContexts[currentQuartzJob.Name] };
}
}
edit:
I don't think it's possible to do what I wanted, that is to be able to get the executing job's name in a class that doesn't know about Quartz.Net.
What I really needed was a way to keep a different context for each job. I managed to do that by looking at the executing thread's ID as they seem to differ for each running job.
Try this:
public void Execute(IJobExecutionContext context)
{
var yourJobName = context.JobDetail.Key.Name;
}
Given your statement above: "The problem I have is that when 2 jobs fires concurrently they might overwrite each others contexts", you may want to reduce the complexity of your application by making sure that jobs do not fire concurrently. This can be achieved by implementing the IStatefulJob interface instead of the usual IJob interface: http://quartznet.sourceforge.net/tutorial/lesson_3.html
Alternatively if that is not an option you can query the Scheduler object for the currently executing jobs via the Ischeduler.GetCurrentlyExecutingJobs() method. This method returns an IList of those jobs but note the following remarks when using that method (from version 1.0.3 API):
This method is not cluster aware. That is, it will only return Jobs currently executing in this Scheduler instance, not across the entire cluster.
Note that the list returned is an 'instantaneous' snap-shot, and that as soon as it's returned, the true list of executing jobs may be different. Also please read the doc associated with JobExecutionContext- especially if you're using remoting.
I'm not entirely sure of what you're doing with your application, but I will say that the name of the currently executing job is definitely available during job execution.
Inside the IJob Execute() method:
// implementation in IJob
public void Execute(JobExecutionContext context)
{
// get name here
string jobName = context.JobDetail.Name;
}
Is that what you're looking for?

Resources