public void onTaskCompleted(RTask rTask, RTaskResult rTaskResult)
            {
                Console.WriteLine("onTaskCompleted: " + rTask + ", result: " + rTaskResult);

                /*
                 * Retrieve Job identifier from RTaskResult.
                 */
                String jobID = rTaskResult.getID();

                Console.WriteLine("onTaskCompleted: " + rTask + ", background Job ID: " + jobID);

                if (m_rBroker != null)
                {
                    /*
                     * Important:
                     *
                     * To handle the results of a Background RTask you
                     * must transition from using the RBroker Framework
                     * API to using the .NET Client Library API.
                     */
                    RUser rUser = m_rBroker.owner();

                    if (rUser != null)
                    {
                        try
                        {
                            RJob rJob = rUser.queryJob(jobID);

                            Console.WriteLine("onTaskCompleted: " + rTask + ", rJob: " + rJob);

                            /*
                             * Next handle the result of the RJob as appropriate.
                             * In this example, simly cancel and delete the job.
                             */

                            try
                            {
                                rJob.cancel();
                            }
                            catch (Exception cex)
                            {
                                Console.WriteLine("rJob.cancel ex=" + cex.ToString());
                            }
                            try
                            {
                                rJob.delete();
                            }
                            catch (Exception dex)
                            {
                                Console.WriteLine("rJob.delete ex=" + dex.ToString());
                            }
                        }
                        catch (Exception jex)
                        {
                            Console.WriteLine("rUser.queryJob ex=" + jex.ToString());
                        }
                    }

                    m_rBroker.shutdown();
                    Console.WriteLine("BackgroundBasics: rBroker has been shutdown.");
                }
            }
Exemplo n.º 2
0
        /// <summary>
        /// Run the acutal background task using Job API
        /// </summary>
        /// <returns>Results of the background task</returns>
        /// <remarks></remarks>
        public RTaskResult call() 
        {


            RTaskResult taskResult = null;
            RJob rJob = null;

            long timeOnCall = 0L;
            //long timeOnServer = 0L;

            try 
            {

                long startTime = Environment.TickCount;

                JobExecutionOptions options = ROptionsTranslator.translate(m_task.options, m_isPriorityTask);

                if(m_task.code != "") 
                {
                    rJob = m_rUser.submitJobCode(m_task.name,
                                        m_task.description,
                                        m_task.code,
                                        options);
                } 
                else
                {
                    if(m_task.external != "") 
                    {
                        rJob = m_rUser.submitJobExternal(m_task.external,
                                        m_task.description,
                                        m_task.code,
                                        options);
                    } 
                    else 
                    {

                        rJob = m_rUser.submitJobScript(m_task.name,
                                          m_task.description,
                                          m_task.filename,
                                          m_task.directory,
                                          m_task.author,
                                          m_task.version,
                                          options);
                    }
                }

                timeOnCall = Environment.TickCount - startTime;

                taskResult = new RTaskResultImpl(rJob.about().id,
                                                 RTaskType.BACKGROUND,
                                                 true,
                                                 0L,
                                                 0L,
                                                 timeOnCall,
                                                 null);

            } 
            catch (Exception ex) 
            {

                //if(ex. instanceof InterruptedException) 
                //{
                    try 
                    {
                        /*
                         * If RTaskToken.cancel() call raises InterruptedException
                         * then ensure any corresponding scheduled RJob is
                         * also cancelled.
                         */
                        rJob.cancel();
                    } 
                    catch(Exception iex) 
                    {
                        throw new Exception("RBroker: could not cancel job, cause:  " + iex.ToString());
                    }
                //}

                taskResult = new RTaskResultImpl(null,
                                                 RTaskType.BACKGROUND,
                                                 false,
                                                 0L,
                                                 0L,
                                                 0L,
                                                 ex);
            } 
            finally 
            {

                /*
                 * Callback to PooledTaskBroker to release
                 * RProject back into pool for other tasks.
                 */
                m_rBroker.callback(m_task, taskResult);
            }

            return taskResult;
        }