Example #1
0
 public RepeatStatus Execute(StepContribution contribution, ChunkContext chunkContext)
 {
     // Write counter to a file - Should be running for 10 seconds roughly
     int counter = 1000;
     string[] lines = new string[counter];
     for (int i = 0; i < counter; i++)
     {
         lines[i] = DateTime.Now.Ticks.ToString();
         Thread.Sleep(10);
     }
     File.WriteAllLines(@"C:\temp\MyDummyTasklet_out_" + DateTime.Now.Ticks + ".txt", lines);
     return RepeatStatus.Finished;
 }
        /// <summary>
        /// Configures a <see cref="Sorter{T}"/> and executes it.
        /// </summary>
        /// <param name="contribution">ignored</param>
        /// <param name="chunkContext">ignored</param>
        /// <returns><see cref="RepeatStatus.Finished"/></returns>
        public new RepeatStatus Execute(StepContribution contribution, ChunkContext chunkContext)
        {
            Logger.Info("Starting ExtendedSort tasklet.");
            SplitSorter<byte[]> sorter = (SplitSorter<byte[]>)BuildSorter();

            var stopwatch = new Stopwatch();
            stopwatch.Start();
            sorter.Sort(Input.Select(r => r.GetFileInfo()).ToList(), Output.GetFileInfo());
            stopwatch.Stop();
            Logger.Info("Total sort time: {0:F2}s", stopwatch.ElapsedMilliseconds / 1000d);

            contribution.ExitStatus = ExitStatus.Completed;
            return RepeatStatus.Finished;
        }
Example #3
0
        /// <summary>
        /// Manage the StepContext lifecycle. Business processing should be
        /// delegated to <see cref="DoInChunkContext"/>. This
        /// is to ensure that the current thread has a reference to the context, even
        /// if the callback is executed in a pooled thread. Handles the registration
        /// and unregistration of the step context, so clients should not duplicate
        /// those calls.
        /// </summary>
        /// <param name="stepExecution"></param>
        /// <param name="doInChunkContext"></param>
        /// <returns></returns>
        public static RepeatCallback GetRepeatCallback(StepExecution stepExecution, DoInChunkContext doInChunkContext)
        {
            BlockingCollection <ChunkContext> attributeQueue = new BlockingCollection <ChunkContext>();

            return(context =>
            {
                // The StepContext has to be the same for all chunks,
                // otherwise step-scoped beans will be re-initialised for each chunk.
                StepContext stepContext = StepSynchronizationManager.Register(stepExecution);
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("Preparing chunk execution for StepContext: {0}",
                                 ObjectUtils.IdentityToString(stepContext));
                }

                ChunkContext chunkContext;
                attributeQueue.TryTake(out chunkContext);
                if (chunkContext == null)
                {
                    chunkContext = new ChunkContext(stepContext);
                }
                try
                {
                    Logger.Debug("Chunk execution starting: queue size= {0}", attributeQueue.Count);
                    return doInChunkContext(context, chunkContext); //Delegation
                }
                finally
                {
                    // Still some stuff to do with the data in this chunk,
                    // pass it back.
                    if (!chunkContext.Complete)
                    {
                        attributeQueue.Add(chunkContext);
                    }
                    StepSynchronizationManager.Close();
                }
            });
        }
Example #4
0
 /// <summary>
 /// Scan remote directory for files matching the given file name pattern and download
 /// them, if any, to the given local directory.
 /// @see ITasklet#Execute
 /// </summary>
 /// <param name="contribution"></param>
 /// <param name="chunkContext"></param>
 /// <returns></returns>
 public RepeatStatus Execute(StepContribution contribution, ChunkContext chunkContext)
 {
     //Delegated 
     DoExecute();
     return RepeatStatus.Finished;
 }
Example #5
0
 public RepeatStatus Execute(StepContribution contribution, ChunkContext chunkContext)
 {
     _logger.Info("OK transition was used");
     return RepeatStatus.Finished;
 }
Example #6
0
 /// <summary>
 /// Custom constructor
 /// </summary>
 /// <param name="chunkContext"></param>
 /// <param name="semaphore"></param>
 /// <param name="owner"></param>
 public ChunkTransactionCallback(ChunkContext chunkContext, Semaphore semaphore, TaskletStep owner)
 {
     _chunkContext = chunkContext;
     _stepExecution = chunkContext.StepContext.StepExecution;
     _semaphore = semaphore;
     _ownerStep = owner;
 }
 /// <summary>
 /// Do nothing execution, since all the logic is in after step
 /// </summary>
 /// <param name="contribution"></param>
 /// <param name="chunkContext"></param>
 /// <returns></returns>
 public RepeatStatus Execute(StepContribution contribution, ChunkContext chunkContext)
 {
     return RepeatStatus.Finished;
 }
        /// <summary>
        /// Manage the StepContext lifecycle. Business processing should be
        /// delegated to #DoInChunkContext(RepeatContext, ChunkContext). This
        /// is to ensure that the current thread has a reference to the context, even
        /// if the callback is executed in a pooled thread. Handles the registration
        /// and unregistration of the step context, so clients should not duplicate
        /// those calls.
        /// </summary>
        /// <param name="stepExecution"></param>
        /// <param name="doInChunkContext"></param>
        /// <returns></returns>
        public static RepeatCallback GetRepeatCallback(StepExecution stepExecution, DoInChunkContext doInChunkContext)
        {
            BlockingCollection<ChunkContext> attributeQueue = new BlockingCollection<ChunkContext>();
            return context =>
            {
                // The StepContext has to be the same for all chunks,
                // otherwise step-scoped beans will be re-initialised for each chunk.
                StepContext stepContext = StepSynchronizationManager.Register(stepExecution);
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("Preparing chunk execution for StepContext: {0}",
                                 ObjectUtils.IdentityToString(stepContext));
                }

                ChunkContext chunkContext;
                attributeQueue.TryTake(out chunkContext);
                if (chunkContext == null)
                {
                    chunkContext = new ChunkContext(stepContext);
                }
                try
                {
                    Logger.Debug("Chunk execution starting: queue size= {0}", attributeQueue.Count);
                    return doInChunkContext(context, chunkContext); //Delegation
                }
                finally
                {
                    // Still some stuff to do with the data in this chunk,
                    // pass it back.
                    if (!chunkContext.Complete)
                    {
                        attributeQueue.Add(chunkContext);
                    }
                    StepSynchronizationManager.Close();
                }
            };
        }
 public RepeatStatus Execute(StepContribution contribution, ChunkContext chunkContext)
 {
     // open connection
     using (DbConnection connection = _providerFactory.CreateConnection())
     {
         connection.ConnectionString = _connectionString;
         connection.Open();
         string preparedCommand = PrepareCommands(Resource);
         DbCommand command = connection.CreateCommand();
         command.CommandText = preparedCommand;
         int sqlDone = command.ExecuteNonQuery();
         if(Logger.IsTraceEnabled)
         {
             Logger.Trace("SQL script execution end with {0} return code", sqlDone);
         }
     }
     return RepeatStatus.Finished;
 }
Example #10
0
        /// <summary>
        /// Configures a <see cref="Sorter{T}"/> and executes it.
        /// </summary>
        /// <param name="contribution">ignored</param>
        /// <param name="chunkContext">ignored</param>
        /// <returns><see cref="RepeatStatus.Finished"/></returns>
        public RepeatStatus Execute(StepContribution contribution, ChunkContext chunkContext)
        {
            Logger.Info("Starting sort tasklet.");
            var sorter = BuildSorter();

            var stopwatch = new Stopwatch();
            stopwatch.Start();
            sorter.Sort();
            stopwatch.Stop();
            Logger.Info("Total sort time: {0:F2}s", stopwatch.ElapsedMilliseconds / 1000d);

            contribution.ExitStatus = ExitStatus.Completed;
            return RepeatStatus.Finished;
        }
 private void CheckStoppingState(ChunkContext chunkContext)
 {
     if (_stoppable)
     {
         JobExecution jobExecution = JobExplorer.GetJobExecution(chunkContext.StepContext.StepExecution.GetJobExecutionId().Value);
         if (jobExecution.IsStopping())
         {
             _stopped = true;
         }
     }
 }
        /// <summary>
        /// Wraps command execution into system process call.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public RepeatStatus Execute(StepContribution contribution, ChunkContext chunkContext)
        {
            if (Logger.IsTraceEnabled)
            {
                Logger.Trace("*** Executing PowerShell Script File: {0}", ScriptResource.GetFullPath());
            }

            //=> PowerShell will throw an error if we do not Suppress ambient transaction...
            //   see https://msdn.microsoft.com/en-us/library/system.transactions.transaction.current(v=vs.110).aspx#NotExistJustToMakeTheAElementVisible
            using (var transactionScope = new TransactionScope(TransactionScopeOption.Suppress))
            {
                //=> Runspace configuration information includes the assemblies, commands, format and type files, 
                //   providers, and scripts that are available within the runspace.
                RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
                
                //Creates a single runspace that uses the default host and runspace configuration
                using (Runspace runSpace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
                {
                    //=> When this runspace is opened, the default host and runspace configuration 
                    //   that are defined by Windows PowerShell will be used. 
                    runSpace.Open();

                    //=> Set Variables so they are available to user script...
                    if (Variables != null  && Variables.Any())
                    {
                        foreach (KeyValuePair<string, object> variable in Variables)
                        {
                            runSpace.SessionStateProxy.SetVariable(variable.Key, variable.Value);
                        }
                    }

                    //=> this is exit status variables to be tested on exit from power shell script...
                    //   it is defined in PwerShell global scope...and must be set by scipt writer on exit...
                    runSpace.SessionStateProxy.SetVariable("ScriptExitStatus", _scriptExitStatus);

                    //=> Allows the execution of commands from a CLR
                    //RunspaceInvoke scriptInvoker = new RunspaceInvoke(runSpace);
                    //scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted"); 

                    using (PowerShell psInstance = PowerShell.Create())
                    {
                        try
                        {
                            // prepare a new collection to store output stream objects
                            PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();
                            outputCollection.DataAdded += AllStreams_DataAdded;
                            psInstance.Streams.Error.DataAdded += AllStreams_DataAdded;
                            psInstance.Streams.Verbose.DataAdded += AllStreams_DataAdded;
                            psInstance.Streams.Warning.DataAdded += AllStreams_DataAdded;
                            psInstance.Streams.Debug.DataAdded += AllStreams_DataAdded;

                            psInstance.Runspace = runSpace;

                            //=> This tasklet should be in the same dll as ExitStatus, i.e. Summer.Batch.Core.dll 
                            //   we need to get the path to loaded Summer.Batch.Core.dll so we can load it in PowerShell
                            var assemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;

                            //=> need to load Summer.Batch.Core into runspace so we can reference ExitStatus
                            psInstance.AddScript("[System.Reflection.Assembly]::LoadFrom(\""+assemblyLocation+"\")").AddStatement();

                            //=> add user command and its parameters...
                            psInstance.AddCommand(ScriptResource.GetFullPath());
                            if (Parameters != null && Parameters.Any())
                            {
                                foreach (KeyValuePair<string, object> variable in Parameters)
                                {
                                    psInstance.AddParameter(variable.Key, variable.Value);
                                }
                            }

                            //=> Invoke Asynchronously...
                            IAsyncResult asyncResult = psInstance.BeginInvoke<PSObject, PSObject>(null, outputCollection);

                            // do something else until execution has completed.
                            long t0 = DateTime.Now.Ticks;
                            while (!asyncResult.IsCompleted)
                            {
                                //=> take a nap and let script do its job...
                                Thread.Sleep(new TimeSpan(_checkInterval));
                                
                                //=> to check if job was told to stop...
                                CheckStoppingState(chunkContext);

                                //=> lets make sure we did not exceed alloted time...
                                long timeFromT0 = (long)(new TimeSpan(DateTime.Now.Ticks - t0)).TotalMilliseconds;
                                if (timeFromT0 > _timeout)
                                {
                                    //=> Stop PowerShell...
                                    psInstance.Stop();

                                    //=> behave based on TimeoutBehaviorOption
                                    if (_timeoutBehavior.Equals(TimeoutBehaviorOption.SetExitStatusToFailed))
                                    {
                                        contribution.ExitStatus = ExitStatus.Failed;
                                        break;
                                    }
                                    else if (_timeoutBehavior.Equals(TimeoutBehaviorOption.ThrowException))
                                    {
                                        //=> lets dump what we got before throwing an error...
                                        LogStreams();
                                        throw new FatalStepExecutionException("Execution of PowerShell script exceeded allotted time.", null);
                                    }
                                }
                                else if (_execution.TerminateOnly)
                                {
                                    //=> Stop PowerShell...
                                    psInstance.Stop();

                                    //=> lets dump what we got before throwing an error...
                                    LogStreams();

                                    throw new JobInterruptedException(
                                        string.Format("Job interrupted while executing PowerShell script '{0}'", ScriptResource.GetFilename()));
                                }
                                else if (_stopped)
                                {
                                    psInstance.Stop();
                                    contribution.ExitStatus = ExitStatus.Stopped;
                                    break;
                                }
                            } // end while scope

                            //=> Wait to the end of execution...
                            //psInstance.EndInvoke(_asyncResult);

                            //NOTE: asyncResult.IsCompleted will be set to true if PowerShell.Stop was called or
                            //      PowerShell completed its work

                            //=> if status not yet set (script completed)...handle completion...
                            if (contribution.ExitStatus.IsRunning())
                            {
                                //=> script needs to set exit code...if exit code not set we assume 0
                                var lastExitCode = (int)runSpace.SessionStateProxy.PSVariable.GetValue("LastExitCode", 0);

                                _scriptExitStatus = runSpace.SessionStateProxy.GetVariable("ScriptExitStatus") as ExitStatus;

                                //=> set exit status...
                                if (_scriptExitStatus != null && !_scriptExitStatus.IsRunning())
                                {
                                    if (Logger.IsTraceEnabled)
                                    {
                                        Logger.Trace("***> ScriptExitStatus returned by script => {0}", _scriptExitStatus);
                                    }

                                    contribution.ExitStatus = _scriptExitStatus;
                                }
                                else //=> let user decide on ExitStatus
                                {
                                    if (Logger.IsTraceEnabled)
                                    {
                                        if (_scriptExitStatus == null)
                                        {
                                            Logger.Trace("***> ScriptExitStatus is null. Using PowerShellExitCodeMapper to determine ExitStatus.");
                                        }
                                        else if (_scriptExitStatus.IsRunning())
                                        {
                                            Logger.Trace("***> ScriptExitStatus is EXECUTING or UNKNOWN. Using PowerShellExitCodeMapper to determine ExitStatus.");
                                        }                        
                                    }

                                    if (PowerShellExitCodeMapper != null)
                                    {
                                        //=> determine exit status using User Provided PowerShellExitCodeMapper
                                        contribution.ExitStatus = PowerShellExitCodeMapper.GetExitStatus(lastExitCode);
                                    }
                                    else //at this point we are not able to determine exit status, user needs to fix this...
                                    {
                                        //=> lets dump what we got before throwing an error...
                                        LogStreams();
                                        throw new FatalStepExecutionException(
                                            "PowerShellTasklet is not able to determine ExitStatus. ScriptExitStatus is null or (is EXECUTING or UNKNOWN) and "+
                                            "PowerShellExitCodeMapper is NOT defined. Please set $global:ScriptExitStatus or define PowerShellExitCodeMapper.", null);
                                    }

                                }
                            }

                            if (Logger.IsInfoEnabled)
                            {
                                Logger.Info("PowerShell execution exit status [{0}]", contribution.ExitStatus);
                            }

                            //=> output captured stream data to Log...
                            LogStreams();
                        }
                        catch (RuntimeException ex)
                        {
                            Logger.Error(ex.Message);
                            throw;
                        }

                    } // end PowerShell Scope

                    //=> close Runspace...
                    runSpace.Close();

                    //=> we are done...
                    return RepeatStatus.Finished;

                } // end of Runspace Scope

            }// end of TransactionScope
        }
Example #13
0
 public RepeatStatus Execute(StepContribution contribution, ChunkContext chunkContext)
 {
     _logger.Info("KO transition was used");
     throw new Exception("Job failed: Wrong transition used");
 }
Example #14
0
 /// <summary>
 /// @see ITasklet#Execute()
 /// </summary>
 /// <param name="contribution"></param>
 /// <param name="chunkContext"></param>
 /// <returns></returns>
 public RepeatStatus Execute(StepContribution contribution, ChunkContext chunkContext)
 {
     switch (Mode)
     {
         case FileUtilsMode.Copy:
             Copy();
             break;
         case FileUtilsMode.Delete:
             foreach (IResource target in Targets)
             {
                 if (target.Exists())
                 {
                     Delete(target);
                 }
                 else
                 {
                     Error(target);
                 }
             }
             break;
         case FileUtilsMode.Merge:
             Merge(true);
             break;
         case FileUtilsMode.MergeCopy:
             Merge(false);
             break;
         case FileUtilsMode.Reset:
             Reset();
             break;
         default:
             throw new InvalidOperationException("This mode is not supported :[" + Mode + "]");
     }
     return RepeatStatus.Finished;
 }
Example #15
0
        /// <summary>
        /// Generates the report
        /// @see ITasklet#Execute
        /// </summary>
        /// <param name="contribution"></param>
        /// <param name="chunkContext"></param>
        /// <returns></returns>
        public RepeatStatus Execute(StepContribution contribution, ChunkContext chunkContext)
        {
            LocalReport report = new LocalReport
            {
                ReportPath = ReportFile.GetFileInfo().FullName
            };

            if (Parameters != null && Parameters.Any())
            {
                if (Logger.IsTraceEnabled)
                {
                    Logger.Trace("{0} parameter(s) were given for the report ", Parameters.Count);
                }
                report.SetParameters(Parameters.Select(p=>new ReportParameter(p.Key,p.Value)));
            }
            else
            {
                if (Logger.IsTraceEnabled)
                {
                    Logger.Trace("No parameter was given for the report ");
                }
            }

            //DataSet
            DataSet ds = DbOperator.Select(Query,QueryParameterSource);

            //ReportDataSource
            ReportDataSource rds = new ReportDataSource
            {
                Name = DatasetName,
                Value = ds.Tables[0]
            };

            report.DataSources.Add(rds);

            if (Logger.IsTraceEnabled)
            {
                Logger.Trace("Report init : DONE => Preparing to render");
            }
            
            byte[] output = report.Render(ReportFormat);
            
            if (Logger.IsTraceEnabled)
            {
                Logger.Trace("Report init : rendering DONE => Preparing to serialize");
            }
            //Create target directory if required
            OutFile.GetFileInfo().Directory.Create();

            //dump to target file
            using (FileStream fs = new FileStream(OutFile.GetFileInfo().FullName, FileMode.Create))
            {
                fs.Write(output,0,output.Length);
            }
            if (Logger.IsTraceEnabled)
            {
                Logger.Info("Report init : serialization DONE - end of ReportTasklet execute.");
            }
            return RepeatStatus.Finished;
        }