public override 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, }; _storage.UseConnection(connection => { string tableName = string.Format("[{0}.Server]", _storage.GetSchemaName()); // select by serverId var serverResult = connection.Query<Entities.Server>( string.Format("select * from {0} where Id = @id", tableName), new { id = serverId }).SingleOrDefault(); if (serverResult == null) { // if not found insert connection.Execute(string.Format("insert into {0} (Id, Data, LastHeartbeat) values (@id, @data, @lastHeartbeat)", tableName), new { id = serverId, data = JobHelper.ToJson(data), lastHeartbeat = DateTime.UtcNow }); } else { // if found, update data + heartbeart connection.Execute(string.Format("update {0} set Data = @data, LastHeartbeat = @lastHeartbeat where Id = @id", tableName), new { id = serverId, data = JobHelper.ToJson(data), lastHeartbeat = DateTime.UtcNow }); } }, true); //_connection.Execute( // @";merge [HangFire.Server] with (holdlock) as Target " // + @"using (VALUES (@id, @data, @heartbeat)) as Source (Id, Data, Heartbeat) " // << SOURCE // + @"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 }); }
public override 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, }; // select by serverId var serverResult = _connection.Query<Entities.Server>( "select * from [HangFire.Server] where Id = @id", new {id = serverId}).SingleOrDefault(); if (serverResult == null) { // if not found insert _connection.Execute( "insert into [HangFire.Server] (Id, Data, LastHeartbeat) values (@id, @data, datetime('now', 'utc'))", new {id = serverId, data = JobHelper.ToJson(data)}); } else { // if found, update data + heartbeart _connection.Execute( "update [HangFire.Server] set Data = @data, LastHeartbeat = datetime('now', 'utc') where Id = @id", new { id = serverId, data = JobHelper.ToJson(data) }); } //_connection.Execute( // @";merge [HangFire.Server] with (holdlock) as Target " // + @"using (VALUES (@id, @data, @heartbeat)) as Source (Id, Data, Heartbeat) " // << SOURCE // + @"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 }); }