/// <summary>
 /// 停止周期作业
 /// </summary>
 /// <param name="JobId"></param>
 public void StopPeriodicJob(string JobId)
 {
     using (IStorageConnection connection = GetConnection())
         using (IWriteOnlyTransaction transaction = connection.CreateWriteTransaction())
         {
             transaction.RemoveFromSet(tagRecurringJobs, JobId);
             transaction.AddToSet($"{tagStopJob}", JobId);
             transaction.Commit();
         }
 }
Пример #2
0
        private void RemoveFingerprint(IStorageConnection connection, Job job)
        {
            using (connection.AcquireDistributedLock(GetFingerprintLockKey(job), LockTimeout))
                using (IWriteOnlyTransaction transaction = connection.CreateWriteTransaction())
                {
                    string fingerprintKey = GetFingerprintKey(job);

                    transaction.RemoveHash(fingerprintKey);
                    transaction.Commit();
                }
        }
        private static void OnVerify(object state)
        {
            mDetectionTimer.Change(-1, -1);
            if (string.IsNullOrEmpty(ProcessMonitor.CurrentServerId))
            {
                mDetectionTimer.Change(1000 * 1, 1000 * 1);
                return;
            }

            try
            {
                using (var connection = JobStorage.Current.GetConnection())
                {
                    //拿到所有的周期性job
                    var jobagentServerList = new Dictionary <string, Tuple <string, string, string> >();
                    var jobList            = connection.GetRecurringJobs();
                    foreach (var job in jobList)
                    {
                        if (job == null || job.Job == null || job.Job.Args == null)
                        {
                            continue;
                        }
                        var httpJob = job.Job.Args.FirstOrDefault() as HttpJobItem;
                        //只处理agentjob
                        if (httpJob == null || string.IsNullOrEmpty(httpJob.AgentClass))
                        {
                            continue;
                        }
                        var key = httpJob.GetUrlHost();
                        if (!jobagentServerList.ContainsKey(key))
                        {
                            jobagentServerList.Add(key, new Tuple <string, string, string>(httpJob.Url, httpJob.BasicUserName, httpJob.BasicPassword));
                        }
                    }

                    if (!jobagentServerList.Any())
                    {
                        return;
                    }
                    IWriteOnlyTransaction writeTransaction = null;
                    if (!isInit)
                    {
                        writeTransaction = connection.CreateWriteTransaction();
                    }
                    var index = 1;
                    foreach (var jobagent in jobagentServerList)
                    {
                        if (!isInit)
                        {
                            var data = new ProcessInfo
                            {
                                Id          = index,
                                Server      = jobagent.Key,
                                ProcessName = "waiting...",
                                CpuUsage    = 0.0,
                                WorkingSet  = 0,
                                Timestamp   = DateTimeOffset.UtcNow.AddMinutes(-2)
                            };
                            var values = new Dictionary <string, string>
                            {
                                [jobagent.Key] = SerializationHelper.Serialize(data)
                            };
                            writeTransaction?.SetRangeInHash("AgentHeart:" + ProcessMonitor.CurrentServerId, values);
                            index++;
                        }
                        new Task(async() =>
                        {
                            await SendHeartbeat(jobagent.Key, jobagent.Value.Item1, jobagent.Value.Item2, jobagent.Value.Item3);
                        }).Start();
                    }

                    if (!isInit)
                    {
                        isInit = true;
                        // if storage supports manual expiration handling
                        if (writeTransaction is JobStorageTransaction jsTransaction)
                        {
                            jsTransaction.ExpireHash("AgentHeart:" + ProcessMonitor.CurrentServerId, TimeSpan.FromMinutes(1));
                        }
                        writeTransaction?.Commit();
                        writeTransaction?.Dispose();
                    }
                }
            }
            catch
            {
                // ignored
            }
            finally
            {
                mDetectionTimer.Change(1000 * 5, 1000 * 5);
            }
        }