Represents a worker in the persistent store.
예제 #1
0
        /// <summary>
        /// Creates a default worker.
        /// </summary>
        /// <returns>The created worker.</returns>
        private Worker CreateDefaultWorker()
        {
            WorkerRecord record = new WorkerRecord()
            {
                ApplicationName = this.applicationName,
                MachineAddress  = Machine.Address,
                MachineName     = Machine.Name,
                Name            = "Default",
                Signal          = WorkerSignal.None,
                Status          = WorkerStatus.Working,
                Startup         = WorkerStartupType.Automatic,
                UpdatedOn       = DateTime.UtcNow
            };

            using (IRepository repository = this.repositoryFactory.Create())
            {
                repository.CreateWorker(record, null);
            }

            return(new Worker(
                       this.applicationName,
                       record.Id.Value,
                       record.Name,
                       QueueNameFilters.Parse(record.QueueNames),
                       this.workerHeartbeat,
                       this.schedulerEnabled,
                       this.repositoryFactory,
                       this.logger));
        }
예제 #2
0
        /// <summary>
        /// Creates a default worker.
        /// </summary>
        /// <returns>The created worker.</returns>
        private Worker CreateDefaultWorker()
        {
            WorkerRecord record = new WorkerRecord()
            {
                ApplicationName = this.applicationName,
                MachineAddress = Machine.Address,
                MachineName = Machine.Name,
                Name = "Default",
                Signal = WorkerSignal.None,
                Status = WorkerStatus.Working,
                Startup = WorkerStartupType.Automatic,
                UpdatedOn = DateTime.UtcNow
            };

            using (IRepository repository = this.repositoryFactory.Create())
            {
                repository.CreateWorker(record, null);
            }

            return new Worker(
                this.applicationName,
                record.Id.Value,
                record.Name,
                QueueNameFilters.Parse(record.QueueNames),
                this.workerHeartbeat,
                this.schedulerEnabled,
                this.repositoryFactory,
                this.logger);
        }
        public WorkerRecord CreateWorker(WorkerRecord record, IDbTransaction transaction)
        {
            const string Sql =
            @"INSERT INTO [BlueCollarWorker]([ApplicationName],[Name],[MachineName],[MachineAddress],[QueueNames],[Status],[Signal],[Startup],[UpdatedOn])
            VALUES(@ApplicationName,@Name,@MachineName,@MachineAddress,@QueueNames,@StatusString,@SignalString,@StartupString,@UpdatedOn);
            SELECT CAST(SCOPE_IDENTITY() AS bigint);";

            record.Id = this.connection.Query<long>(
                Sql,
                record,
                transaction,
                true,
                null,
                null).First();

            return record;
        }
예제 #4
0
        internal void RefreshWorkers()
        {
            List <WorkerRecord> records;

            using (IRepository repository = this.repositoryFactory.Create())
            {
                records = repository.GetWorkers(this.applicationName, this.address, this.name, null).ToList();

                this.logger.Debug(
                    "Machine {0} ({1}) loaded {2} worker records from repository '{3}', using connection string '{4}'.",
                    this.name,
                    this.address,
                    records.Count,
                    repository.GetType().ToString(),
                    repository.ConnectionString);
            }

            lock (this)
            {
                List <Worker> newWorkers    = new List <Worker>();
                List <Worker> removeWorkers = new List <Worker>();

                // Figure out which of the current workers get to stay alive.
                foreach (Worker worker in this.workers)
                {
                    WorkerRecord record = records.Where(r => r.Id == worker.Id).FirstOrDefault();

                    if (record != null)
                    {
                        newWorkers.Add(worker);
                        records.Remove(record);
                    }
                    else
                    {
                        removeWorkers.Add(worker);
                    }
                }

                // Prune orphaned current workers.
                foreach (Worker worker in removeWorkers)
                {
                    try
                    {
                        worker.Dispose();
                    }
                    catch (Exception ex)
                    {
                        this.logger.Error(ex);
                    }
                }

                // Create workers for all of the new records. Records corresponding
                // to existing workers where pruned earlier.
                foreach (WorkerRecord record in records)
                {
                    Worker worker = null;

                    try
                    {
                        worker = new Worker(
                            this.applicationName,
                            record.Id.Value,
                            record.Name,
                            QueueNameFilters.Parse(record.QueueNames),
                            this.workerHeartbeat,
                            this.schedulerEnabled,
                            this.repositoryFactory,
                            this.logger);

                        newWorkers.Add(worker);

                        if (record.Status == WorkerStatus.Working || record.Startup == WorkerStartupType.Automatic)
                        {
                            worker.Start();
                        }
                    }
                    catch (Exception ex)
                    {
                        if (worker != null)
                        {
                            newWorkers.Remove(worker);
                            worker.Dispose();
                        }

                        this.logger.Error(ex);
                    }
                }

                // Ensure a default worker if necessary.
                if (newWorkers.Count == 0 && this.ensureDefaultWorker)
                {
                    Worker worker = this.CreateDefaultWorker();
                    newWorkers.Add(worker);
                    worker.Start();
                }

                this.workers = newWorkers;
                this.logger.Debug("Machine {0} ({1}) refreshed its worker list and is now tracking {2} workers.", this.name, this.address, this.workers.Count);
            }
        }
        /// <summary>
        /// Updates the given worker.
        /// </summary>
        /// <param name="record">The worker record to update.</param>
        /// <param name="transaction">The transaction to use, if applicable.</param>
        /// <returns>The updated worker.</returns>
        public WorkerRecord UpdateWorker(WorkerRecord record, IDbTransaction transaction)
        {
            const string Sql =
            @"UPDATE [BlueCollarWorker]
            SET
            [Name] = @Name,
            [MachineName] = @MachineName,
            [MachineAddress] = @MachineAddress,
            [QueueNames] = @QueueNames,
            [Status] = @StatusString,
            [Signal] = @SignalString,
            [Startup] = @StartupString,
            [UpdatedOn] = @UpdatedOn
            WHERE
            [Id] = @Id;";

            this.connection.Execute(
                Sql,
                record,
                transaction,
                null,
                null);

            return record;
        }