Ejemplo n.º 1
0
        public ServerBootstrapperFacts()
        {
            _context = new ServerContext();
            _storage = new Mock<JobStorage>();
            _supervisor = new Mock<IServerSupervisor>();
            _supervisorFactory = new Lazy<IServerSupervisor>(() => _supervisor.Object);
            _connection = new Mock<IStorageConnection>();
            _cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));

            _storage.Setup(x => x.GetConnection()).Returns(_connection.Object);
        }
        public ServerBootstrapperFacts()
        {
            _context = new ServerContext();
            _storage = new Mock<JobStorage>();
            _supervisor = new Mock<IServerSupervisor>();
            _supervisorFactory = new Lazy<IServerSupervisor>(() => _supervisor.Object);
            _connection = new Mock<IStorageConnection>();
            _token = new CancellationToken(true);

            _storage.Setup(x => x.GetConnection()).Returns(_connection.Object);
        }
Ejemplo n.º 3
0
        public WorkerManager(JobStorage storage, ServerContext context, int workerCount)
        {
            _workers = new DisposableCollection<Worker>();

            for (var i = 1; i <= workerCount; i++)
            {
                var workerContext = new WorkerContext(context, i);

                var worker = new Worker(storage, workerContext);
                worker.Start();

                _workers.Add(worker);
            }
        }
Ejemplo n.º 4
0
        public ServerBootstrapper(
            string serverId,
            ServerContext context,
            JobStorage storage,
            Lazy<IServerSupervisor> supervisorFactory)
        {
            if (storage == null) throw new ArgumentNullException("storage");
            if (serverId == null) throw new ArgumentNullException("serverId");
            if (context == null) throw new ArgumentNullException("context");
            if (supervisorFactory == null) throw new ArgumentNullException("supervisorFactory");

            _storage = storage;
            _serverId = serverId;
            _context = context;
            _supervisorFactory = supervisorFactory;
        }
Ejemplo n.º 5
0
        public ServerBootstrapper(
            string serverId,
            ServerContext context,
            JobStorage storage,
            Lazy<IServerSupervisor> supervisorFactory)
        {
            if (storage == null) throw new ArgumentNullException("storage");
            if (serverId == null) throw new ArgumentNullException("serverId");
            if (context == null) throw new ArgumentNullException("context");
            if (supervisorFactory == null) throw new ArgumentNullException("supervisorFactory");

            _storage = storage;
            _serverId = serverId;
            _context = context;
            _supervisorFactory = supervisorFactory;

			if (!RunningWithMono()) 
			{
				_globalMutex = new Mutex (false, String.Format (@"Global\{0}_{1}", BootstrapperId, _serverId));
			}
        }
Ejemplo n.º 6
0
        public JobServer(
            JobStorage storage,
            string serverName,
            int workerCount,
            IEnumerable<string> queues)
        {
            _storage = storage;
            _workerCount = workerCount;
            _queues = queues;

            if (queues == null) throw new ArgumentNullException("queues");

            _context = new ServerContext(
                serverName,
                _queues,
                new JobPerformancePipeline());

            _serverThread = new Thread(RunServer)
                {
                    Name = typeof(JobServer).Name,
                    IsBackground = true
                };
            _serverThread.Start();
        }
Ejemplo n.º 7
0
        internal virtual IServerSupervisor GetBootstrapSupervisor()
        {
            var context = new ServerContext
            {
                Queues = _options.Queues,
                WorkerCount = _options.WorkerCount
            };

            var bootstrapper = new ServerBootstrapper(
                _serverId, 
                context, 
                _storage, 
                new Lazy<IServerSupervisor>(GetSupervisors));

            return new ServerSupervisor(
                bootstrapper, 
                new ServerSupervisorOptions
                {
                    ShutdownTimeout = _options.ShutdownTimeout
                });
        }
Ejemplo n.º 8
0
 internal ServerContext(ServerContext context)
     : this(context.ServerName, context.QueueNames, context.PerformancePipeline)
 {
 }
Ejemplo n.º 9
0
        public void AnnounceServer(string serverId, ServerContext context)
        {
            using (var transaction = Redis.CreateTransaction())
            {
                transaction.QueueCommand(x => x.AddItemToSet(
                    RedisStorage.Prefix + "servers", serverId));

                transaction.QueueCommand(x => x.SetRangeInHash(
                    String.Format(RedisStorage.Prefix + "server:{0}", serverId),
                    new Dictionary<string, string>
                        {
                            { "WorkerCount", context.WorkerCount.ToString(CultureInfo.InvariantCulture) },
                            { "StartedAt", JobHelper.ToStringTimestamp(DateTime.UtcNow) },
                        }));

                foreach (var queue in context.Queues)
                {
                    var queue1 = queue;
                    transaction.QueueCommand(x => x.AddItemToList(
                        String.Format(RedisStorage.Prefix + "server:{0}:queues", serverId),
                        queue1));
                }

                transaction.Commit();
            }
        }
        public void AnnounceServer_CreatesOrUpdatesARecord()
        {
            UseConnections((sql, connection) =>
            {
                var context1 = new ServerContext
                {
                    Queues = new[] { "critical", "default" },
                    WorkerCount = 4
                };
                connection.AnnounceServer("server", context1);

                var server = sql.Query("select * from HangFire.Server").Single();
                Assert.Equal("server", server.Id);
                Assert.True(((string)server.Data).StartsWith(
                    "{\"WorkerCount\":4,\"Queues\":[\"critical\",\"default\"],\"StartedAt\":"),
                    server.Data);
                Assert.NotNull(server.LastHeartbeat);

                var context2 = new ServerContext
                {
                    Queues = new[] { "default" },
                    WorkerCount = 1000 
                };
                connection.AnnounceServer("server", context2);
                var sameServer = sql.Query("select * from HangFire.Server").Single();
                Assert.Equal("server", sameServer.Id);
                Assert.Contains("1000", sameServer.Data);
            });
        }
Ejemplo n.º 11
0
        public void AnnounceServer(string serverId, ServerContext context)
        {
            if (serverId == null) throw new ArgumentNullException("serverId");
            if (context == null) throw new ArgumentNullException("context");

            var data = new ServerData
            {
                WorkerCount = context.WorkerCount,
                Queues = context.Queues,
                StartedAt = DateTime.UtcNow,
            };

            _connection.Execute(
                @"merge HangFire.Server as Target "
                + @"using (VALUES (@id, @data, @heartbeat)) as Source (Id, Data, Heartbeat) "
                + @"on Target.Id = Source.Id "
                + @"when matched then update set Data = Source.Data, LastHeartbeat = Source.Heartbeat "
                + @"when not matched then insert (Id, Data, LastHeartbeat) values (Source.Id, Source.Data, Source.Heartbeat);",
                new { id = serverId, data = JobHelper.ToJson(data), heartbeat = DateTime.UtcNow });
        }
Ejemplo n.º 12
0
 internal WorkerContext(ServerContext serverContext, int workerNumber)
     : base(serverContext)
 {
     WorkerNumber = workerNumber;
 }