Exemplo n.º 1
0
        public void With_Workflow_Job()
        {
            IAutomatedJob _getAuthToken = InlineJob.GetDefault((ctx) =>
            {
                //call actual api and get Auth token
                var token = Guid.NewGuid().ToString();

                //store the token in to Context store
                ctx.SetValue(token, "token-key");
                //Console.WriteLine(token);

                return(new JobResult(JobStatus.Completed));
            });

            IAutomatedJob _download = InlineJob.GetDefault((ctx) =>
            {
                //get Auth token from Context store
                var token = ctx.GetValue <string>("token-key");
                //Console.WriteLine(token);

                // download the data using Auth token
                Task.Delay(200).Wait();

                return(new JobResult(JobStatus.Completed));
            });

            //build integraion workflow - sequential by default - parallel also be possible
            IAutomatedJob _integraion = Builder.BuildWorkflow(InlineJob.GetJobId())
                                        .WithOption(WhenFailure.StopOrExitJob, ShareContext.previous)
                                        .AddJob(_getAuthToken)
                                        .ThenAdd(_download)
                                        .NothingElse().Create();

            //get notofication manager, from service repo
            var manager = ServiceRepo.Instance.GetServiceOf <INotificationManager <JobId> >();

            //setup Message hooks
            var hanlder = new AggregateHandler();

            manager.RegisterHook(_download.Id, MessageType.Info, hanlder);
            manager.RegisterHook(_getAuthToken.Id, MessageType.Info, hanlder);

            var result = _integraion.Doable();           //run the integration job

            Console.WriteLine(result.Status.ToString()); //Completed

            //wait for handler to aggregate the message hooks, since push is ASync
            Task.Delay(500).Wait();

            //print the messages recieved
            hanlder.MessagesRecieved.ToList().ForEach(m =>
            {
                Console.WriteLine(m.Item2.ToString());
            });
        }
Exemplo n.º 2
0
        public JobBuilder ThenAdd(IAutomatedJob job)
        {
            if (job == default(IAutomatedJob))
            {
                throw new ArgumentNullException("job");
            }

            _workflow.AddJob(job);

            return(this);
        }
Exemplo n.º 3
0
        public JobBuilder AddJob(IAutomatedJob job)
        {
            if (job == default(IAutomatedJob))
            {
                throw new ArgumentNullException("job");
            }

            _workflow = new WorkflowJob(_id, _context, _failure, _share);
            _workflow.AddJob(job);

            return(new JobBuilder(_workflow));
        }
        private JobResult Run(IAutomatedJob job)
        {
            JobResult result = null;

            result = ErrorHandle.Expect(() => job.Doable(),
                                        out bool anyError,
                                        string.Format(_msgWError, Current.Id.Id, Current.Id.Name),
                                        string.Format(_msgJobError, job.Id.Id, job.Id.Name));

            if (anyError)
            {
                result = new JobResult(JobStatus.CompletedWithError, new AutoJobException(job.Id, null, _msgError));
            }

            _jobStatus[job.Id.ToString()] = result;
            return(result);
        }
        private JobResult ProcessJob(IAutomatedJob job, IJobContext context, out bool anyError)
        {
            JobResult innerResult = null;

            anyError = false;

            if (!job.mLock.WaitOne())
            {
                return(new JobResult(
                           JobStatus.CompletedWithError,
                           new AutoJobException(Current.Id,
                                                null,
                                                string.Format(_msgAbortError,
                                                              job.Id.Id, job.Id.Name))));
            }

            innerResult = ProcessWithHooks(job, context, out anyError);
            job.mLock.ReleaseMutex();

            return(innerResult);
        }
Exemplo n.º 6
0
        public void With_Standalone_Job()
        {
            //setup Download job
            IAutomatedJob _download = InlineJob.GetDefault((ctx) =>
            {
                //push
                ctx.PushReportToHookAsync(ctx.ParentJobId,
                                          new MessageHook("Download Started", "Info", MessageType.Info));

                // download the data
                Task.Delay(200).Wait();

                //push
                ctx.PushReportToHookAsync(ctx.ParentJobId,
                                          new MessageHook("Download Completed", "Info", MessageType.Info));

                return(new JobResult(JobStatus.Completed));
            });

            //get notofication manager, from service repo
            var manager = ServiceRepo.Instance.GetServiceOf <INotificationManager <JobId> >();

            //setup Message hooks
            var hanlder = new AggregateHandler();

            manager.RegisterHook(_download.Id, MessageType.Info, hanlder);

            var result = _download.Doable();             //execute the job

            Console.WriteLine(result.Status.ToString()); //Completed

            //wait for handler to aggregate the message hooks, since push is ASync
            Task.Delay(500).Wait();

            //print the messages recieved
            hanlder.MessagesRecieved.ToList().ForEach(m =>
            {
                Console.WriteLine(m.Item2.ToString());
            });
        }
        private JobResult ProcessWithHooks(IAutomatedJob job, IJobContext context, out bool anyError)
        {
            JobResult innerResult = null;
            var       jId         = job.Id.ToString();
            var       wId         = Current.Id.ToString();

            anyError    = false;
            job.Context = context;
            job.Context.PushReportToHookAsync(job.Id,
                                              new MessageHook(string.Format(_msgJobSatrted, jId, wId),
                                                              MessageType.Info.ToString(), MessageType.Info));

            try
            {
                innerResult = job.Doable();
            }
            catch (Exception ex)
            {
                anyError = true;
                var error = string.Join(Environment.NewLine,
                                        string.Format(_msgWError, Current.Id.Id, Current.Id.Name),
                                        string.Format(_msgJobError, job.Id.Id, job.Id.Name),
                                        ex.Message, ex.StackTrace);

                job.Context.PushReportToHookAsync(job.Id,
                                                  new MessageHook(error,
                                                                  MessageType.Error.ToString(), MessageType.Error));
                ErrorHandle.Logger.Log(ex);
            }
            finally
            {
                job.Context.PushReportToHookAsync(job.Id,
                                                  new MessageHook(string.Format(_msgJobCompleted, jId, wId),
                                                                  MessageType.Info.ToString(), MessageType.Info));
            }

            return(innerResult);
        }
Exemplo n.º 8
0
        public void RetryJob()
        {
            IAutomatedJob _processFile = InlineJob.GetDefault((ctx) =>
            {
                //to simulate the retry when job failed
                throw new Exception("making the job failed");

                return(new JobResult(JobStatus.Completed));
            });

            //make the job as retry, when error/exception is anticipated
            var retry = _processFile.ConvertToRetry(3, TimeSpan.FromMilliseconds(500));

            ErrorHandle.Logger = NullLogger.Instance; //disable the default console logging

            var rResult = retry.Doable();             // run retry job

            Console.WriteLine(rResult.Status);        //CompletedWithError
            foreach (var r in retry.RetryResults)
            {
                Console.WriteLine(r.Error.Message); //print retry error/excpetion message
            }
        }
Exemplo n.º 9
0
        public void WorkflowJob()
        {
            IAutomatedJob _getAuthToken = InlineJob.GetDefault((ctx) =>
            {
                //call actual api and get Auth token
                var token = Guid.NewGuid().ToString();

                //store the token in to Context store
                ctx.SetValue(token, "token-key");
                //Console.WriteLine(token);

                return(new JobResult(JobStatus.Completed));
            });

            IAutomatedJob _download = InlineJob.GetDefault((ctx) =>
            {
                //get Auth token from Context store
                var token = ctx.GetValue <string>("token-key");
                //Console.WriteLine(token);

                // download the data using Auth token
                Task.Delay(200).Wait();

                return(new JobResult(JobStatus.Completed));
            });

            //build integraion workflow - sequential by default - parallel also be possible
            IAutomatedJob _integraion = Builder.BuildWorkflow(InlineJob.GetJobId())
                                        .WithOption(WhenFailure.StopOrExitJob, ShareContext.previous)
                                        .AddJob(_getAuthToken)
                                        .ThenAdd(_download)
                                        .NothingElse().Create();

            var result = _integraion.Doable();           //run the integration job

            Console.WriteLine(result.Status.ToString()); //Completed
        }
 private JobResult ProcessWorkflow(IAutomatedJob job, IJobContext context, out bool anyError)
 {
     anyError = false;
     return(ProcessWithHooks(job, context, out anyError));
 }
Exemplo n.º 11
0
 /// <summary>
 /// Runs IAutomatedJob with specified times
 /// </summary>
 /// <param name="job">IAutomatedJob</param>
 /// <param name="times">no of times, the IAutomatedJob will be repeated</param>
 /// <param name="doBeforeRepeat">validation</param>
 /// <returns></returns>
 public static IAutomatedJob Repeat(this IAutomatedJob job, int times, Func <IJobContext, ValidationResult> doBeforeRepeat = null)
 {
     return(new Retry <IAutomatedJob>(job, times, TimeSpan.Zero, doBeforeRepeat));
 }
Exemplo n.º 12
0
 /// <summary>
 /// Converts IAutomatedJob to Retry, with given times and timespan..etc
 /// </summary>
 /// <param name="job">IAutomatedJob</param>
 /// <param name="times">no of times, the IAutomatedJob will be retried</param>
 /// <param name="interval">time span between retry</param>
 /// <param name="doBeforeRetry">validation before retry</param>
 /// <returns></returns>
 public static Retry <IAutomatedJob> ConvertToRetry(this IAutomatedJob job, int times, TimeSpan interval, Func <IJobContext, ValidationResult> doBeforeRetry = null)
 {
     return(new Retry <IAutomatedJob>(job, times, interval, doBeforeRetry));
 }