public override void StartTask(ITaskExecutionContext context)
        {
            int iterations = 10;

            for (int i = 1; i <= iterations; i++)
            {
                context.ThrowIfCancelled();

                // Everything we log will be outputted in the Hangfire UI/dashboard
                //  - This was enabled by the .EnableConsole() method on .UseHangfire()
                context.Log.Message("Iteration: [{0}/{1}]", i, iterations);

                if (i != iterations)
                {
                    context.CancellationToken.WaitHandle.WaitOne(TimeSpan.FromMilliseconds(500));
                }
            }

            if (Interlocked.Increment(ref _iterations) % 4 == 0)
            {
                throw new InvalidOperationException("Simulating that the task is throwing an exception. {json}");
            }

            context.Log.Message("Done - including some {json}");
            context.Log.Message("...");
        }
Example #2
0
        public override void StartTask(ITaskExecutionContext context)
        {
            uint count;

            if (!uint.TryParse(context.Arguments["Iterations"], out count))
            {
                count = 10;
            }

            int[] iterations = Enumerable.Range(1, (int)count).ToArray();

            IProgressBar progressBar = _hangfireContextProvider.Current.WriteProgressBar();

            foreach (int i in iterations.WithProgress(progressBar))
            {
                context.ThrowIfCancelled();

                // Everything we log will be outputted in the Hangfire UI/dashboard
                //  - This was enabled by the .EnableConsole() method on .UseHangfire()
                context.Log.Message("Iteration: [{0}/{1}]", i, iterations.Length);

                if (i != iterations.Length)
                {
                    context.CancellationToken.WaitHandle.WaitOne(TimeSpan.FromMilliseconds(500));
                }
            }

            context.Log.Message("Done");
            context.Log.Message("...");
        }
Example #3
0
        public override void StartTask(ITaskExecutionContext context)
        {
            MigrationDb[] destinations = _dbs;

            // if specific migration dbs have been provided by argument, only they'll be run
            string[] names = (context.Arguments["Names"] ?? string.Empty)
                             .Split(new[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);

            if (names.Length > 0)
            {
                ILookup <string, MigrationDb> migrationsByName =
                    _dbs.ToLookup(db => db.IdentifyingName, db => db, StringComparer.OrdinalIgnoreCase);

                destinations = names
                               .SelectMany(x => migrationsByName[x])
                               .ToArray();
            }

            string action = (context.Arguments["Action"] ?? string.Empty).ToLowerInvariant();

            foreach (MigrationDb destination in destinations)
            {
                context.ThrowIfCancelled();

                MigrationRunner runner = CreateRunner(destination, out StringBuilder output);

                switch (action)
                {
                case "list":
                    destination.List(runner, context, _kernel);
                    break;

                case "rollback":
                    destination.Rollback(runner, context, _kernel);
                    break;

                default:
                    destination.MigrateUp(runner, context, _kernel);
                    break;
                }

                runner.Processor.Dispose();

                if (output.Length > 0)
                {
                    context.Log.Message(output.ToString());
                }

                if (_disabledFeatures != null)
                {
                    foreach (FeatureAttribute feature in _disabledFeatures)
                    {
                        feature.Enable(_featureToggler);
                    }
                }
            }

            if (_databaseCreated)
            {
                context.Log.Warning(
                    Target.Service,
                    "Created new database (using Simple Recovery) and applied migrations to this. Make sure to configure this new database (auto growth, backup etc).");
            }
        }