/Provider/SchedulingProviders/DNNScheduler/Scheduler.cs

# · C# · 1279 lines · 1165 code · 113 blank · 1 comment · 130 complexity · 6cd47733af7acf6df612f09acd89edcf MD5 · raw file

  1. namespace DotNetNuke.Services.Scheduling.DNNScheduling
  2. {
  3. using DotNetNuke.Common;
  4. using DotNetNuke.Common.Utilities;
  5. using DotNetNuke.Services.Exceptions;
  6. using DotNetNuke.Services.Log.EventLog;
  7. using DotNetNuke.Services.Scheduling;
  8. using System;
  9. using System.Collections;
  10. using System.Text;
  11. using System.Threading;
  12. using VB2CS;
  13. /**[StandardModule]**/
  14. internal sealed class Scheduler
  15. {
  16. public class CoreScheduler
  17. {
  18. private static Collection _ScheduleInProgress;
  19. private static Collection _ScheduleQueue;
  20. private static int ActiveThreadCount;
  21. private static ProcessGroup[] arrProcessGroup;
  22. private static bool Debug = false;
  23. private static bool ForceReloadSchedule = false;
  24. public static bool KeepRunning = true;
  25. public static bool KeepThreadAlive = true;
  26. private static int MaxThreadCount;
  27. private static int NumberOfProcessGroups;
  28. private static ReaderWriterLock objInProgressReadWriteLock = new ReaderWriterLock();
  29. private static ReaderWriterLock objQueueReadWriteLock = new ReaderWriterLock();
  30. private static ReaderWriterLock objStatusReadWriteLock = new ReaderWriterLock();
  31. private static int ReaderTimeouts = 0;
  32. private static int Reads = 0;
  33. private static int ReadTimeout = 0xafc8;
  34. private static ScheduleStatus Status = ScheduleStatus.STOPPED;
  35. private static bool ThreadPoolInitialized = false;
  36. private static int WriterTimeouts = 0;
  37. private static int Writes = 0;
  38. private static int WriteTimeout = 0xafc8;
  39. public CoreScheduler(int MaxThreads)
  40. {
  41. if (!ThreadPoolInitialized)
  42. {
  43. this.InitializeThreadPool(MaxThreads);
  44. }
  45. }
  46. public CoreScheduler(bool boolDebug, int MaxThreads)
  47. {
  48. Debug = boolDebug;
  49. if (!ThreadPoolInitialized)
  50. {
  51. this.InitializeThreadPool(MaxThreads);
  52. }
  53. }
  54. public static ScheduleHistoryItem AddScheduleHistory(ScheduleHistoryItem objScheduleHistoryItem)
  55. {
  56. try
  57. {
  58. int intScheduleHistoryID = new SchedulingController().AddScheduleHistory(objScheduleHistoryItem);
  59. objScheduleHistoryItem.ScheduleHistoryID = intScheduleHistoryID;
  60. }
  61. catch (Exception exception1)
  62. {
  63. Exception exc = exception1;
  64. DotNetNuke.Services.Exceptions.Exceptions.ProcessSchedulerException(exc);
  65. }
  66. return objScheduleHistoryItem;
  67. }
  68. private static void AddToScheduleInProgress(ScheduleHistoryItem objScheduleHistoryItem)
  69. {
  70. try
  71. {
  72. objInProgressReadWriteLock.AcquireWriterLock(WriteTimeout);
  73. try
  74. {
  75. ScheduleInProgress.Add(objScheduleHistoryItem, objScheduleHistoryItem.ScheduleID.ToString(), null, null);
  76. Interlocked.Increment(ref Writes);
  77. }
  78. finally
  79. {
  80. objInProgressReadWriteLock.ReleaseWriterLock();
  81. }
  82. }
  83. catch (ApplicationException exception1)
  84. {
  85. ApplicationException ex = exception1;
  86. Interlocked.Increment(ref WriterTimeouts);
  87. DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
  88. }
  89. }
  90. public static void AddToScheduleQueue(ScheduleHistoryItem objScheduleHistoryItem)
  91. {
  92. if (!ScheduleQueueContains(objScheduleHistoryItem))
  93. {
  94. try
  95. {
  96. objQueueReadWriteLock.AcquireWriterLock(WriteTimeout);
  97. try
  98. {
  99. ScheduleQueue.Add(objScheduleHistoryItem, objScheduleHistoryItem.ScheduleID.ToString(), null, null);
  100. Interlocked.Increment(ref Writes);
  101. }
  102. finally
  103. {
  104. objQueueReadWriteLock.ReleaseWriterLock();
  105. }
  106. }
  107. catch (ApplicationException exception1)
  108. {
  109. ApplicationException ex = exception1;
  110. Interlocked.Increment(ref WriterTimeouts);
  111. DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
  112. }
  113. }
  114. }
  115. private static void ClearScheduleQueue()
  116. {
  117. try
  118. {
  119. int intCount = GetScheduleQueueCount();
  120. if (intCount != 0)
  121. {
  122. objQueueReadWriteLock.AcquireWriterLock(WriteTimeout);
  123. try
  124. {
  125. int refIntHelperL0 = intCount;
  126. for (int i = 1; i <= refIntHelperL0; i++)
  127. {
  128. ScheduleQueue.Remove(i);
  129. }
  130. Interlocked.Increment(ref Writes);
  131. }
  132. finally
  133. {
  134. objQueueReadWriteLock.ReleaseWriterLock();
  135. }
  136. }
  137. }
  138. catch (ApplicationException exception1)
  139. {
  140. ApplicationException ex = exception1;
  141. Interlocked.Increment(ref WriterTimeouts);
  142. DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
  143. }
  144. }
  145. ~CoreScheduler()
  146. {
  147. }
  148. public static void FireEvents(bool Asynchronous)
  149. {
  150. int intScheduleQueueCount = GetScheduleQueueCount();
  151. int numToRun = intScheduleQueueCount;
  152. int numRun = 0;
  153. int refIntHelperL0 = intScheduleQueueCount - 1;
  154. for (int i = 0; i <= refIntHelperL0; i++)
  155. {
  156. if (!KeepRunning)
  157. {
  158. break;
  159. }
  160. int ProcessGroup = GetProcessGroup();
  161. ScheduleItem objScheduleItem = (ScheduleItem) ScheduleQueue[i + 1];
  162. if (((((DateTime.Compare(objScheduleItem.NextStart, DateTime.Now) <= 0) && objScheduleItem.Enabled) && ((!IsInProgress(objScheduleItem) && !HasDependenciesConflict(objScheduleItem)) && (numRun < numToRun))) ? 1 : 0) != 0)
  163. {
  164. objScheduleItem.ProcessGroup = ProcessGroup;
  165. if (SchedulingProvider.SchedulerMode == SchedulerMode.TIMER_METHOD)
  166. {
  167. objScheduleItem.ScheduleSource = ScheduleSource.STARTED_FROM_TIMER;
  168. }
  169. else if (SchedulingProvider.SchedulerMode == SchedulerMode.REQUEST_METHOD)
  170. {
  171. objScheduleItem.ScheduleSource = ScheduleSource.STARTED_FROM_BEGIN_REQUEST;
  172. }
  173. if (Asynchronous)
  174. {
  175. arrProcessGroup[ProcessGroup].AddQueueUserWorkItem(objScheduleItem);
  176. }
  177. else
  178. {
  179. arrProcessGroup[ProcessGroup].RunSingleTask(objScheduleItem);
  180. }
  181. if (Debug)
  182. {
  183. EventLogController objEventLog = new EventLogController();
  184. LogInfo objEventLogInfo = new LogInfo();
  185. objEventLogInfo.AddProperty("EVENT ADDED TO PROCESS GROUP " + objScheduleItem.ProcessGroup.ToString(), objScheduleItem.TypeFullName);
  186. objEventLogInfo.AddProperty("SCHEDULE ID", objScheduleItem.ScheduleID.ToString());
  187. objEventLogInfo.LogTypeKey = "DEBUG";
  188. objEventLog.AddLog(objEventLogInfo);
  189. }
  190. numRun++;
  191. }
  192. else if (Debug)
  193. {
  194. bool appended = false;
  195. StringBuilder strDebug = new StringBuilder("Task not run because ");
  196. if (DateTime.Compare(objScheduleItem.NextStart, DateTime.Now) > 0)
  197. {
  198. strDebug.Append(" task is scheduled for " + objScheduleItem.NextStart.ToString());
  199. appended = true;
  200. }
  201. if (!objScheduleItem.Enabled)
  202. {
  203. if (appended)
  204. {
  205. strDebug.Append(" and");
  206. }
  207. strDebug.Append(" task is not enabled");
  208. appended = true;
  209. }
  210. if (IsInProgress(objScheduleItem))
  211. {
  212. if (appended)
  213. {
  214. strDebug.Append(" and");
  215. }
  216. strDebug.Append(" task is already in progress");
  217. appended = true;
  218. }
  219. if (HasDependenciesConflict(objScheduleItem))
  220. {
  221. if (appended)
  222. {
  223. strDebug.Append(" and");
  224. }
  225. strDebug.Append(" task has conflicting dependency");
  226. appended = true;
  227. }
  228. EventLogController objEventLog = new EventLogController();
  229. LogInfo objEventLogInfo = new LogInfo();
  230. objEventLogInfo.AddProperty("EVENT NOT RUN REASON", strDebug.ToString());
  231. objEventLogInfo.AddProperty("SCHEDULE ID", objScheduleItem.ScheduleID.ToString());
  232. objEventLogInfo.AddProperty("TYPE FULL NAME", objScheduleItem.TypeFullName);
  233. objEventLogInfo.LogTypeKey = "DEBUG";
  234. objEventLog.AddLog(objEventLogInfo);
  235. }
  236. }
  237. }
  238. public static int GetActiveThreadCount()
  239. {
  240. return ActiveThreadCount;
  241. }
  242. public static int GetFreeThreadCount()
  243. {
  244. return FreeThreads;
  245. }
  246. public static int GetMaxThreadCount()
  247. {
  248. return MaxThreadCount;
  249. }
  250. private static int GetProcessGroup()
  251. {
  252. Random r = new Random();
  253. return r.Next(0, NumberOfProcessGroups - 1);
  254. }
  255. public static Collection GetScheduleInProgress()
  256. {
  257. Collection c = null;
  258. try
  259. {
  260. objInProgressReadWriteLock.AcquireReaderLock(ReadTimeout);
  261. try
  262. {
  263. c = ScheduleInProgress;
  264. Interlocked.Increment(ref Reads);
  265. }
  266. finally
  267. {
  268. objInProgressReadWriteLock.ReleaseReaderLock();
  269. }
  270. }
  271. catch (ApplicationException exception1)
  272. {
  273. ApplicationException ex = exception1;
  274. Interlocked.Increment(ref ReaderTimeouts);
  275. }
  276. return c;
  277. }
  278. public static int GetScheduleInProgressCount()
  279. {
  280. int intCount=0;
  281. try
  282. {
  283. objInProgressReadWriteLock.AcquireReaderLock(ReadTimeout);
  284. try
  285. {
  286. intCount = ScheduleInProgress.Count;
  287. Interlocked.Increment(ref Reads);
  288. }
  289. finally
  290. {
  291. objInProgressReadWriteLock.ReleaseReaderLock();
  292. }
  293. }
  294. catch (ApplicationException exception1)
  295. {
  296. ApplicationException ex = exception1;
  297. Interlocked.Increment(ref ReaderTimeouts);
  298. }
  299. return intCount;
  300. }
  301. public static Collection GetScheduleQueue()
  302. {
  303. Collection c = null;
  304. try
  305. {
  306. objQueueReadWriteLock.AcquireReaderLock(ReadTimeout);
  307. try
  308. {
  309. c = ScheduleQueue;
  310. Interlocked.Increment(ref Reads);
  311. }
  312. finally
  313. {
  314. objQueueReadWriteLock.ReleaseReaderLock();
  315. }
  316. }
  317. catch (ApplicationException exception1)
  318. {
  319. ApplicationException ex = exception1;
  320. Interlocked.Increment(ref ReaderTimeouts);
  321. }
  322. return c;
  323. }
  324. public static int GetScheduleQueueCount()
  325. {
  326. int intCount=0;
  327. try
  328. {
  329. objQueueReadWriteLock.AcquireReaderLock(ReadTimeout);
  330. try
  331. {
  332. intCount = ScheduleQueue.Count;
  333. Interlocked.Increment(ref Reads);
  334. }
  335. finally
  336. {
  337. objQueueReadWriteLock.ReleaseReaderLock();
  338. }
  339. }
  340. catch (ApplicationException exception1)
  341. {
  342. ApplicationException ex = exception1;
  343. Interlocked.Increment(ref ReaderTimeouts);
  344. }
  345. return intCount;
  346. }
  347. public static ScheduleStatus GetScheduleStatus()
  348. {
  349. ScheduleStatus s=ScheduleStatus.NOT_SET;
  350. try
  351. {
  352. objStatusReadWriteLock.AcquireReaderLock(ReadTimeout);
  353. try
  354. {
  355. s = Status;
  356. }
  357. finally
  358. {
  359. objStatusReadWriteLock.ReleaseReaderLock();
  360. }
  361. }
  362. catch (ApplicationException exception1)
  363. {
  364. ApplicationException ex = exception1;
  365. Interlocked.Increment(ref ReaderTimeouts);
  366. }
  367. return s;
  368. }
  369. public static void Halt(string SourceOfHalt)
  370. {
  371. if (SchedulingProvider.SchedulerMode != SchedulerMode.REQUEST_METHOD)
  372. {
  373. int endLoopIntHelperS0;
  374. EventLogController objEventLog = new EventLogController();
  375. LogInfo objEventLogInfo = new LogInfo();
  376. SetScheduleStatus(ScheduleStatus.SHUTTING_DOWN);
  377. objEventLogInfo = new LogInfo();
  378. objEventLogInfo.AddProperty("Initiator", SourceOfHalt);
  379. objEventLogInfo.LogTypeKey = "SCHEDULER_SHUTTING_DOWN";
  380. objEventLog.AddLog(objEventLogInfo);
  381. KeepRunning = false;
  382. int i = 0;
  383. do
  384. {
  385. if (GetScheduleStatus() == ScheduleStatus.STOPPED)
  386. {
  387. return;
  388. }
  389. Thread.Sleep(0x3e8);
  390. i++;
  391. endLoopIntHelperS0 = 120;
  392. }
  393. while (i <= endLoopIntHelperS0);
  394. }
  395. ActiveThreadCount = 0;
  396. }
  397. public static bool HasDependenciesConflict(ScheduleItem objScheduleItem)
  398. {
  399. bool objReturn = false;
  400. try
  401. {
  402. objInProgressReadWriteLock.AcquireReaderLock(ReadTimeout);
  403. try
  404. {
  405. if ((ScheduleInProgress != null) & (objScheduleItem.ObjectDependencies.Length > 0))
  406. {
  407. int refIntHelperL0 = ScheduleInProgress.Count;
  408. for (int i = 1; i <= refIntHelperL0; i++)
  409. {
  410. ScheduleItem obj = (ScheduleItem) ScheduleInProgress[i];
  411. if ((obj.ObjectDependencies.Length > 0) && obj.HasObjectDependencies(objScheduleItem.ObjectDependencies))
  412. {
  413. objReturn = true;
  414. }
  415. }
  416. }
  417. Interlocked.Increment(ref Reads);
  418. }
  419. finally
  420. {
  421. objInProgressReadWriteLock.ReleaseReaderLock();
  422. }
  423. }
  424. catch (ApplicationException exception1)
  425. {
  426. ApplicationException ex = exception1;
  427. Interlocked.Increment(ref ReaderTimeouts);
  428. }
  429. return objReturn;
  430. }
  431. private void InitializeThreadPool(int MaxThreads)
  432. {
  433. if (MaxThreads == -1)
  434. {
  435. MaxThreads = 1;
  436. }
  437. NumberOfProcessGroups = MaxThreads;
  438. MaxThreadCount = MaxThreads;
  439. int refIntHelperL0 = NumberOfProcessGroups - 1;
  440. for (int i = 0; i <= refIntHelperL0; i++)
  441. {
  442. arrProcessGroup = (ProcessGroup[]) Utils.CopyArray((Array) arrProcessGroup, new ProcessGroup[i + 1]);
  443. arrProcessGroup[i] = new ProcessGroup();
  444. }
  445. ThreadPoolInitialized = true;
  446. }
  447. private static bool IsInProgress(ScheduleItem objScheduleItem)
  448. {
  449. bool objReturn = false;
  450. try
  451. {
  452. objInProgressReadWriteLock.AcquireReaderLock(ReadTimeout);
  453. try
  454. {
  455. if (ScheduleInProgress.Count > 0)
  456. {
  457. int refIntHelperL0 = ScheduleInProgress.Count;
  458. for (int i = 1; i <= refIntHelperL0; i++)
  459. {
  460. ScheduleItem obj = (ScheduleItem) ScheduleInProgress[i];
  461. if (obj.ScheduleID == objScheduleItem.ScheduleID)
  462. {
  463. objReturn = true;
  464. }
  465. }
  466. }
  467. Interlocked.Increment(ref Reads);
  468. }
  469. finally
  470. {
  471. objInProgressReadWriteLock.ReleaseReaderLock();
  472. }
  473. }
  474. catch (ApplicationException exception1)
  475. {
  476. ApplicationException ex = exception1;
  477. Interlocked.Increment(ref ReaderTimeouts);
  478. }
  479. return objReturn;
  480. }
  481. internal static bool IsInQueue(ScheduleItem objScheduleItem)
  482. {
  483. bool objReturn = false;
  484. try
  485. {
  486. objQueueReadWriteLock.AcquireReaderLock(ReadTimeout);
  487. try
  488. {
  489. if (GetScheduleQueueCount() > 0)
  490. {
  491. int refIntHelperL0 = GetScheduleQueueCount();
  492. for (int i = 1; i <= refIntHelperL0; i++)
  493. {
  494. ScheduleItem obj = (ScheduleItem) ScheduleQueue[i];
  495. if (obj.ScheduleID == objScheduleItem.ScheduleID)
  496. {
  497. objReturn = true;
  498. }
  499. }
  500. }
  501. Interlocked.Increment(ref Reads);
  502. }
  503. finally
  504. {
  505. objQueueReadWriteLock.ReleaseReaderLock();
  506. }
  507. }
  508. catch (ApplicationException exception1)
  509. {
  510. ApplicationException ex = exception1;
  511. Interlocked.Increment(ref ReaderTimeouts);
  512. }
  513. return objReturn;
  514. }
  515. public static void LoadQueueFromEvent(EventName EventName)
  516. {
  517. ArrayList a = new SchedulingController().GetScheduleByEvent(EventName.ToString(), DotNetNuke.Common.Globals.ServerName);
  518. int refIntHelperL0 = a.Count - 1;
  519. for (int i = 0; i <= refIntHelperL0; i++)
  520. {
  521. ScheduleHistoryItem objScheduleHistoryItem = (ScheduleHistoryItem) a[i];
  522. if ((((!IsInQueue(objScheduleHistoryItem) && !IsInProgress(objScheduleHistoryItem)) && (!HasDependenciesConflict(objScheduleHistoryItem) && objScheduleHistoryItem.Enabled)) ? 1 : 0) != 0)
  523. {
  524. objScheduleHistoryItem.ScheduleSource = ScheduleSource.STARTED_FROM_EVENT;
  525. AddToScheduleQueue(objScheduleHistoryItem);
  526. }
  527. }
  528. }
  529. public static void LoadQueueFromTimer()
  530. {
  531. ForceReloadSchedule = false;
  532. ArrayList a = new SchedulingController().GetSchedule();
  533. int refIntHelperL0 = a.Count - 1;
  534. for (int i = 0; i <= refIntHelperL0; i++)
  535. {
  536. ScheduleHistoryItem objScheduleHistoryItem = (ScheduleHistoryItem) a[i];
  537. if ((((!IsInQueue(objScheduleHistoryItem) && (objScheduleHistoryItem.TimeLapse != Null.NullInteger)) && ((objScheduleHistoryItem.TimeLapseMeasurement != Null.NullString) && objScheduleHistoryItem.Enabled)) ? 1 : 0) != 0)
  538. {
  539. if (SchedulingProvider.SchedulerMode == SchedulerMode.TIMER_METHOD)
  540. {
  541. objScheduleHistoryItem.ScheduleSource = ScheduleSource.STARTED_FROM_TIMER;
  542. }
  543. else if (SchedulingProvider.SchedulerMode == SchedulerMode.REQUEST_METHOD)
  544. {
  545. objScheduleHistoryItem.ScheduleSource = ScheduleSource.STARTED_FROM_BEGIN_REQUEST;
  546. }
  547. AddToScheduleQueue(objScheduleHistoryItem);
  548. }
  549. }
  550. }
  551. public static void PurgeScheduleHistory()
  552. {
  553. new SchedulingController().PurgeScheduleHistory();
  554. }
  555. public static void ReloadSchedule()
  556. {
  557. ForceReloadSchedule = true;
  558. }
  559. private static void RemoveFromScheduleInProgress(ScheduleItem objScheduleItem)
  560. {
  561. try
  562. {
  563. objInProgressReadWriteLock.AcquireWriterLock(WriteTimeout);
  564. try
  565. {
  566. ScheduleInProgress.Remove(objScheduleItem.ScheduleID.ToString());
  567. Interlocked.Increment(ref Writes);
  568. }
  569. catch (ArgumentException exception1)
  570. {
  571. ArgumentException exc = exception1;
  572. }
  573. finally
  574. {
  575. objInProgressReadWriteLock.ReleaseWriterLock();
  576. }
  577. }
  578. catch (ApplicationException exception2)
  579. {
  580. ApplicationException ex = exception2;
  581. Interlocked.Increment(ref WriterTimeouts);
  582. DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
  583. }
  584. }
  585. public static void RemoveFromScheduleQueue(ScheduleItem objScheduleItem)
  586. {
  587. try
  588. {
  589. objQueueReadWriteLock.AcquireWriterLock(WriteTimeout);
  590. try
  591. {
  592. ScheduleQueue.Remove(objScheduleItem.ScheduleID.ToString());
  593. Interlocked.Increment(ref Writes);
  594. }
  595. catch (ArgumentException exception1)
  596. {
  597. ArgumentException exc = exception1;
  598. }
  599. finally
  600. {
  601. objQueueReadWriteLock.ReleaseWriterLock();
  602. }
  603. }
  604. catch (ApplicationException exception2)
  605. {
  606. ApplicationException ex = exception2;
  607. Interlocked.Increment(ref WriterTimeouts);
  608. DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
  609. }
  610. }
  611. public static void RunEventSchedule(EventName EventName)
  612. {
  613. try
  614. {
  615. EventLogController objEventLog = new EventLogController();
  616. LogInfo objEventLogInfo = new LogInfo();
  617. objEventLogInfo.AddProperty("EVENT", EventName.ToString());
  618. objEventLogInfo.LogTypeKey = "SCHEDULE_FIRED_FROM_EVENT";
  619. objEventLog.AddLog(objEventLogInfo);
  620. LoadQueueFromEvent(EventName);
  621. while (GetScheduleQueueCount() > 0)
  622. {
  623. SetScheduleStatus(ScheduleStatus.RUNNING_EVENT_SCHEDULE);
  624. try
  625. {
  626. objQueueReadWriteLock.AcquireReaderLock(ReadTimeout);
  627. try
  628. {
  629. if (GetScheduleQueueCount() > 0)
  630. {
  631. FireEvents(true);
  632. }
  633. Interlocked.Increment(ref Reads);
  634. }
  635. finally
  636. {
  637. objQueueReadWriteLock.ReleaseReaderLock();
  638. }
  639. }
  640. catch (ApplicationException exception1)
  641. {
  642. ApplicationException ex = exception1;
  643. Interlocked.Increment(ref ReaderTimeouts);
  644. }
  645. if ((WriterTimeouts > 20) | (ReaderTimeouts > 20))
  646. {
  647. Thread.Sleep(0x927c0);
  648. }
  649. else
  650. {
  651. Thread.Sleep(0x2710);
  652. }
  653. if (GetScheduleQueueCount() == 0)
  654. {
  655. return;
  656. }
  657. }
  658. }
  659. catch (Exception exception2)
  660. {
  661. Exception exc = exception2;
  662. DotNetNuke.Services.Exceptions.Exceptions.ProcessSchedulerException(exc);
  663. }
  664. }
  665. private static bool ScheduleQueueContains(ScheduleItem objScheduleItem)
  666. {
  667. int refIntHelperL0 = GetScheduleQueueCount();
  668. for (int i = 1; i <= refIntHelperL0; i++)
  669. {
  670. ScheduleItem objScheduleItem2 = (ScheduleItem) ScheduleQueue[i];
  671. if (objScheduleItem2.ScheduleID == objScheduleItem.ScheduleID)
  672. {
  673. return true;
  674. }
  675. }
  676. return false;
  677. }
  678. public static void SetScheduleStatus(ScheduleStatus objScheduleStatus)
  679. {
  680. try
  681. {
  682. objStatusReadWriteLock.AcquireWriterLock(WriteTimeout);
  683. try
  684. {
  685. Status = objScheduleStatus;
  686. Interlocked.Increment(ref Writes);
  687. }
  688. finally
  689. {
  690. objStatusReadWriteLock.ReleaseWriterLock();
  691. }
  692. }
  693. catch (ApplicationException exception1)
  694. {
  695. ApplicationException ex = exception1;
  696. Interlocked.Increment(ref WriterTimeouts);
  697. DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
  698. }
  699. }
  700. public static void Start()
  701. {
  702. try
  703. {
  704. ActiveThreadCount = 0;
  705. if ((SchedulingProvider.SchedulerMode != SchedulerMode.REQUEST_METHOD) | Debug)
  706. {
  707. EventLogController objEventLog = new EventLogController();
  708. LogInfo objEventLogInfo = new LogInfo();
  709. objEventLogInfo.LogTypeKey = "SCHEDULER_STARTED";
  710. objEventLog.AddLog(objEventLogInfo);
  711. }
  712. while (KeepRunning)
  713. {
  714. try
  715. {
  716. if (SchedulingProvider.SchedulerMode == SchedulerMode.TIMER_METHOD)
  717. {
  718. SetScheduleStatus(ScheduleStatus.RUNNING_TIMER_SCHEDULE);
  719. }
  720. else
  721. {
  722. SetScheduleStatus(ScheduleStatus.RUNNING_REQUEST_SCHEDULE);
  723. }
  724. LoadQueueFromTimer();
  725. DateTime LastQueueRefresh = DateTime.Now;
  726. bool RefreshQueueSchedule = false;
  727. while ((((FreeThreads > 0) & !RefreshQueueSchedule) & KeepRunning) & !ForceReloadSchedule)
  728. {
  729. try
  730. {
  731. if (SchedulingProvider.SchedulerMode == SchedulerMode.TIMER_METHOD)
  732. {
  733. SetScheduleStatus(ScheduleStatus.RUNNING_TIMER_SCHEDULE);
  734. }
  735. else
  736. {
  737. SetScheduleStatus(ScheduleStatus.RUNNING_REQUEST_SCHEDULE);
  738. }
  739. objQueueReadWriteLock.AcquireReaderLock(ReadTimeout);
  740. try
  741. {
  742. if (GetScheduleQueueCount() > 0)
  743. {
  744. FireEvents(true);
  745. }
  746. if (!KeepThreadAlive)
  747. {
  748. return;
  749. }
  750. Interlocked.Increment(ref Reads);
  751. }
  752. finally
  753. {
  754. objQueueReadWriteLock.ReleaseReaderLock();
  755. }
  756. }
  757. catch (ApplicationException exception1)
  758. {
  759. ApplicationException ex = exception1;
  760. Interlocked.Increment(ref ReaderTimeouts);
  761. }
  762. if ((WriterTimeouts > 20) | (ReaderTimeouts > 20))
  763. {
  764. if (!KeepRunning)
  765. {
  766. return;
  767. }
  768. Thread.Sleep(0x927c0);
  769. }
  770. else
  771. {
  772. if (KeepRunning)
  773. {
  774. Thread.Sleep(0x2710);
  775. }
  776. else
  777. {
  778. return;
  779. }
  780. if (((DateTime.Compare(LastQueueRefresh.AddMinutes(10.0), DateTime.Now) <= 0) | ForceReloadSchedule) & (FreeThreads == MaxThreadCount))
  781. {
  782. RefreshQueueSchedule = true;
  783. break;
  784. }
  785. }
  786. }
  787. if (!KeepRunning)
  788. {
  789. return;
  790. }
  791. if (!RefreshQueueSchedule)
  792. {
  793. SetScheduleStatus(ScheduleStatus.WAITING_FOR_OPEN_THREAD);
  794. Thread.Sleep(0x2710);
  795. }
  796. continue;
  797. }
  798. catch (Exception exception2)
  799. {
  800. Exception exc = exception2;
  801. DotNetNuke.Services.Exceptions.Exceptions.ProcessSchedulerException(exc);
  802. Thread.Sleep(0x927c0);
  803. }
  804. }
  805. }
  806. finally
  807. {
  808. if ((SchedulingProvider.SchedulerMode == SchedulerMode.TIMER_METHOD) | (SchedulingProvider.SchedulerMode == SchedulerMode.DISABLED))
  809. {
  810. SetScheduleStatus(ScheduleStatus.STOPPED);
  811. }
  812. else
  813. {
  814. SetScheduleStatus(ScheduleStatus.WAITING_FOR_REQUEST);
  815. }
  816. if ((SchedulingProvider.SchedulerMode != SchedulerMode.REQUEST_METHOD) | Debug)
  817. {
  818. EventLogController objEventLog = new EventLogController();
  819. LogInfo objEventLogInfo = new LogInfo();
  820. objEventLogInfo.LogTypeKey = "SCHEDULER_STOPPED";
  821. objEventLog.AddLog(objEventLogInfo);
  822. }
  823. }
  824. }
  825. public static void UpdateScheduleHistory(ScheduleHistoryItem objScheduleHistoryItem)
  826. {
  827. try
  828. {
  829. new SchedulingController().UpdateScheduleHistory(objScheduleHistoryItem);
  830. }
  831. catch (Exception exception1)
  832. {
  833. Exception exc = exception1;
  834. DotNetNuke.Services.Exceptions.Exceptions.ProcessSchedulerException(exc);
  835. }
  836. }
  837. public static void WorkCompleted(ref SchedulerClient objSchedulerClient)
  838. {
  839. try
  840. {
  841. ScheduleHistoryItem objScheduleHistoryItem = objSchedulerClient.ScheduleHistoryItem;
  842. RemoveFromScheduleInProgress(objScheduleHistoryItem);
  843. Interlocked.Decrement(ref ActiveThreadCount);
  844. objScheduleHistoryItem.EndDate = DateTime.Now;
  845. if (objScheduleHistoryItem.ScheduleSource == ScheduleSource.STARTED_FROM_EVENT)
  846. {
  847. objScheduleHistoryItem.NextStart = Null.NullDate;
  848. }
  849. else if (objScheduleHistoryItem.CatchUpEnabled)
  850. {
  851. switch (objScheduleHistoryItem.TimeLapseMeasurement)
  852. {
  853. case "s":
  854. objScheduleHistoryItem.NextStart = objScheduleHistoryItem.NextStart.AddSeconds((double) objScheduleHistoryItem.TimeLapse);
  855. break;
  856. case "m":
  857. objScheduleHistoryItem.NextStart = objScheduleHistoryItem.NextStart.AddMinutes((double) objScheduleHistoryItem.TimeLapse);
  858. break;
  859. case "h":
  860. objScheduleHistoryItem.NextStart = objScheduleHistoryItem.NextStart.AddHours((double) objScheduleHistoryItem.TimeLapse);
  861. break;
  862. case "d":
  863. objScheduleHistoryItem.NextStart = objScheduleHistoryItem.NextStart.AddDays((double) objScheduleHistoryItem.TimeLapse);
  864. break;
  865. }
  866. }
  867. else
  868. {
  869. switch (objScheduleHistoryItem.TimeLapseMeasurement)
  870. {
  871. case "s":
  872. objScheduleHistoryItem.NextStart = objScheduleHistoryItem.StartDate.AddSeconds((double) objScheduleHistoryItem.TimeLapse);
  873. break;
  874. case "m":
  875. objScheduleHistoryItem.NextStart = objScheduleHistoryItem.StartDate.AddMinutes((double) objScheduleHistoryItem.TimeLapse);
  876. break;
  877. case "h":
  878. objScheduleHistoryItem.NextStart = objScheduleHistoryItem.StartDate.AddHours((double) objScheduleHistoryItem.TimeLapse);
  879. break;
  880. case "d":
  881. objScheduleHistoryItem.NextStart = objScheduleHistoryItem.StartDate.AddDays((double) objScheduleHistoryItem.TimeLapse);
  882. break;
  883. }
  884. }
  885. UpdateScheduleHistory(objScheduleHistoryItem);
  886. LogInfo objEventLogInfo = new LogInfo();
  887. if (DateTime.Compare(objScheduleHistoryItem.NextStart, Null.NullDate) != 0)
  888. {
  889. objScheduleHistoryItem.StartDate = Null.NullDate;
  890. objScheduleHistoryItem.EndDate = Null.NullDate;
  891. objScheduleHistoryItem.LogNotes = "";
  892. objScheduleHistoryItem.ProcessGroup = -1;
  893. AddToScheduleQueue(objScheduleHistoryItem);
  894. }
  895. if (objSchedulerClient.ScheduleHistoryItem.RetainHistoryNum > 0)
  896. {
  897. EventLogController objEventLog = new EventLogController();
  898. objEventLogInfo.AddProperty("TYPE", objSchedulerClient.GetType().FullName);
  899. objEventLogInfo.AddProperty("THREAD ID", Thread.CurrentThread.GetHashCode().ToString());
  900. objEventLogInfo.AddProperty("NEXT START", Convert.ToString(objScheduleHistoryItem.NextStart));
  901. objEventLogInfo.AddProperty("SOURCE", objSchedulerClient.ScheduleHistoryItem.ScheduleSource.ToString());
  902. objEventLogInfo.AddProperty("ACTIVE THREADS", ActiveThreadCount.ToString());
  903. objEventLogInfo.AddProperty("FREE THREADS", FreeThreads.ToString());
  904. objEventLogInfo.AddProperty("READER TIMEOUTS", ReaderTimeouts.ToString());
  905. objEventLogInfo.AddProperty("WRITER TIMEOUTS", WriterTimeouts.ToString());
  906. objEventLogInfo.AddProperty("IN PROGRESS", GetScheduleInProgressCount().ToString());
  907. objEventLogInfo.AddProperty("IN QUEUE", GetScheduleQueueCount().ToString());
  908. objEventLogInfo.LogTypeKey = "SCHEDULER_EVENT_COMPLETED";
  909. objEventLog.AddLog(objEventLogInfo);
  910. }
  911. }
  912. catch (Exception exception1)
  913. {
  914. Exception exc = exception1;
  915. DotNetNuke.Services.Exceptions.Exceptions.ProcessSchedulerException(exc);
  916. }
  917. }
  918. public static void WorkErrored(ref SchedulerClient objSchedulerClient, ref Exception objException)
  919. {
  920. try
  921. {
  922. ScheduleHistoryItem objScheduleHistoryItem = objSchedulerClient.ScheduleHistoryItem;
  923. RemoveFromScheduleInProgress(objScheduleHistoryItem);
  924. Interlocked.Decrement(ref ActiveThreadCount);
  925. DotNetNuke.Services.Exceptions.Exceptions.ProcessSchedulerException(objException);
  926. objScheduleHistoryItem.EndDate = DateTime.Now;
  927. if (objScheduleHistoryItem.ScheduleSource == ScheduleSource.STARTED_FROM_EVENT)
  928. {
  929. objScheduleHistoryItem.NextStart = Null.NullDate;
  930. }
  931. else if (objScheduleHistoryItem.RetryTimeLapse != Null.NullInteger)
  932. {
  933. switch (objScheduleHistoryItem.RetryTimeLapseMeasurement)
  934. {
  935. case "s":
  936. objScheduleHistoryItem.NextStart = objScheduleHistoryItem.StartDate.AddSeconds((double) objScheduleHistoryItem.RetryTimeLapse);
  937. goto Label_0150;
  938. case "m":
  939. objScheduleHistoryItem.NextStart = objScheduleHistoryItem.StartDate.AddMinutes((double) objScheduleHistoryItem.RetryTimeLapse);
  940. goto Label_0150;
  941. case "h":
  942. objScheduleHistoryItem.NextStart = objScheduleHistoryItem.StartDate.AddHours((double) objScheduleHistoryItem.RetryTimeLapse);
  943. break;
  944. case "d":
  945. objScheduleHistoryItem.NextStart = objScheduleHistoryItem.StartDate.AddDays((double) objScheduleHistoryItem.RetryTimeLapse);
  946. break;
  947. }
  948. }
  949. Label_0150:
  950. UpdateScheduleHistory(objScheduleHistoryItem);
  951. if ((((DateTime.Compare(objScheduleHistoryItem.NextStart, Null.NullDate) != 0) && (objScheduleHistoryItem.RetryTimeLapse != Null.NullInteger)) ? 1 : 0) != 0)
  952. {
  953. objScheduleHistoryItem.StartDate = Null.NullDate;
  954. objScheduleHistoryItem.EndDate = Null.NullDate;
  955. objScheduleHistoryItem.LogNotes = "";
  956. objScheduleHistoryItem.ProcessGroup = -1;
  957. AddToScheduleQueue(objScheduleHistoryItem);
  958. }
  959. if (objSchedulerClient.ScheduleHistoryItem.RetainHistoryNum > 0)
  960. {
  961. EventLogController objEventLog = new EventLogController();
  962. LogInfo objEventLogInfo = new LogInfo();
  963. objEventLogInfo.AddProperty("THREAD ID", Thread.CurrentThread.GetHashCode().ToString());
  964. objEventLogInfo.AddProperty("TYPE", objSchedulerClient.GetType().FullName);
  965. if (objException != null)
  966. {
  967. objEventLogInfo.AddProperty("EXCEPTION", objException.Message);
  968. }
  969. objEventLogInfo.AddProperty("RESCHEDULED FOR", Convert.ToString(objScheduleHistoryItem.NextStart));
  970. objEventLogInfo.AddProperty("SOURCE", objSchedulerClient.ScheduleHistoryItem.ScheduleSource.ToString());
  971. objEventLogInfo.AddProperty("ACTIVE THREADS", ActiveThreadCount.ToString());
  972. objEventLogInfo.AddProperty("FREE THREADS", FreeThreads.ToString());
  973. objEventLogInfo.AddProperty("READER TIMEOUTS", ReaderTimeouts.ToString());
  974. objEventLogInfo.AddProperty("WRITER TIMEOUTS", WriterTimeouts.ToString());
  975. objEventLogInfo.AddProperty("IN PROGRESS", GetScheduleInProgressCount().ToString());
  976. objEventLogInfo.AddProperty("IN QUEUE", GetScheduleQueueCount().ToString());
  977. objEventLogInfo.LogTypeKey = "SCHEDULER_EVENT_FAILURE";
  978. objEventLog.AddLog(objEventLogInfo);
  979. }
  980. }
  981. catch (Exception exception1)
  982. {
  983. Exception exc = exception1;
  984. DotNetNuke.Services.Exceptions.Exceptions.ProcessSchedulerException(exc);
  985. }
  986. }
  987. public static void WorkProgressing(ref SchedulerClient objSchedulerClient)
  988. {
  989. try
  990. {
  991. if (objSchedulerClient.ScheduleHistoryItem.RetainHistoryNum > 0)
  992. {
  993. EventLogController objEventLog = new EventLogController();
  994. LogInfo objEventLogInfo = new LogInfo();
  995. objEventLogInfo.AddProperty("THREAD ID", Thread.CurrentThread.GetHashCode().ToString());
  996. objEventLogInfo.AddProperty("TYPE", objSchedulerClient.GetType().FullName);
  997. objEventLogInfo.AddProperty("SOURCE", objSchedulerClient.ScheduleHistoryItem.ScheduleSource.ToString());
  998. objEventLogInfo.AddProperty("ACTIVE THREADS", ActiveThreadCount.ToString());
  999. objEventLogInfo.AddProperty("FREE THREADS", FreeThreads.ToString());
  1000. objEventLogInfo.AddProperty("READER TIMEOUTS", ReaderTimeouts.ToString());
  1001. objEventLogInfo.AddProperty("WRITER TIMEOUTS", WriterTimeouts.ToString());
  1002. objEventLogInfo.AddProperty("IN PROGRESS", GetScheduleInProgressCount().ToString());
  1003. objEventLogInfo.AddProperty("IN QUEUE", GetScheduleQueueCount().ToString());
  1004. objEventLogInfo.LogTypeKey = "SCHEDULER_EVENT_PROGRESSING";
  1005. objEventLog.AddLog(objEventLogInfo);
  1006. }
  1007. }
  1008. catch (Exception exception1)
  1009. {
  1010. Exception exc = exception1;
  1011. DotNetNuke.Services.Exceptions.Exceptions.ProcessSchedulerException(exc);
  1012. }
  1013. }
  1014. public static void WorkStarted(ref SchedulerClient objSchedulerClient)
  1015. {
  1016. bool ActiveThreadCountIncremented = false;
  1017. try
  1018. {
  1019. objSchedulerClient.ScheduleHistoryItem.ThreadID = Thread.CurrentThread.GetHashCode();
  1020. RemoveFromScheduleQueue(objSchedulerClient.ScheduleHistoryItem);
  1021. AddToScheduleInProgress(objSchedulerClient.ScheduleHistoryItem);
  1022. Interlocked.Increment(ref ActiveThreadCount);
  1023. ActiveThreadCountIncremented = true;
  1024. objSchedulerClient.ScheduleHistoryItem.StartDate = DateTime.Now;
  1025. AddScheduleHistory(objSchedulerClient.ScheduleHistoryItem);
  1026. if (objSchedulerClient.ScheduleHistoryItem.RetainHistoryNum > 0)
  1027. {
  1028. EventLogController objEventLog = new EventLogController();
  1029. LogInfo objEventLogInfo = new LogInfo();
  1030. objEventLogInfo.AddProperty("THREAD ID", Thread.CurrentThread.GetHashCode().ToString());
  1031. objEventLogInfo.AddProperty("TYPE", objSchedulerClient.GetType().FullName);
  1032. objEventLogInfo.AddProperty("SOURCE", objSchedulerClient.ScheduleHistoryItem.ScheduleSource.ToString());
  1033. objEventLogInfo.AddProperty("ACTIVE THREADS", ActiveThreadCount.ToString());
  1034. objEventLogInfo.AddProperty("FREE THREADS", FreeThreads.ToString());
  1035. objEventLogInfo.AddProperty("READER TIMEOUTS", ReaderTimeouts.ToString());
  1036. objEventLogInfo.AddProperty("WRITER TIMEOUTS", WriterTimeouts.ToString());
  1037. objEventLogInfo.AddProperty("IN PROGRESS", GetScheduleInProgressCount().ToString());
  1038. objEventLogInfo.AddProperty("IN QUEUE", GetScheduleQueueCount().ToString());
  1039. objEventLogInfo.LogTypeKey = "SCHEDULER_EVENT_STARTED";
  1040. objEventLog.AddLog(objEventLogInfo);
  1041. }
  1042. }
  1043. catch (Exception exception1)
  1044. {
  1045. Exception exc = exception1;
  1046. if (ActiveThreadCountIncremented)
  1047. {
  1048. Interlocked.Decrement(ref ActiveThreadCount);
  1049. }
  1050. DotNetNuke.Services.Exceptions.Exceptions.ProcessSchedulerException(exc);
  1051. }
  1052. }
  1053. public static int FreeThreads
  1054. {
  1055. get
  1056. {
  1057. return (MaxThreadCount - ActiveThreadCount);
  1058. }
  1059. }
  1060. private static Collection ScheduleInProgress
  1061. {
  1062. get
  1063. {
  1064. try
  1065. {
  1066. objQueueReadWriteLock.AcquireReaderLock(ReadTimeout);
  1067. try
  1068. {
  1069. if (_ScheduleInProgress == null)
  1070. {
  1071. _ScheduleInProgress = new Collection();
  1072. }
  1073. Interlocked.Increment(ref Reads);
  1074. }
  1075. finally
  1076. {
  1077. objQueueReadWriteLock.ReleaseReaderLock();
  1078. }
  1079. }
  1080. catch (ApplicationException exception1)
  1081. {
  1082. ApplicationException ex = exception1;
  1083. Interlocked.Increment(ref ReadTimeout);
  1084. DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
  1085. }
  1086. return _ScheduleInProgress;
  1087. }
  1088. set
  1089. {
  1090. try
  1091. {
  1092. objQueueReadWriteLock.AcquireWriterLock(WriteTimeout);
  1093. try
  1094. {
  1095. DataCache.SetCache("ScheduleQueue", value);
  1096. _ScheduleInProgress = value;
  1097. Interlocked.Increment(ref Writes);
  1098. }
  1099. finally
  1100. {
  1101. objQueueReadWriteLock.ReleaseWriterLock();
  1102. }
  1103. }
  1104. catch (ApplicationException exception1)
  1105. {
  1106. ApplicationException ex = exception1;
  1107. Interlocked.Increment(ref WriterTimeouts);
  1108. DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
  1109. }
  1110. }
  1111. }
  1112. private static Collection ScheduleQueue
  1113. {
  1114. get
  1115. {
  1116. try
  1117. {
  1118. objQueueReadWriteLock.AcquireReaderLock(ReadTimeout);
  1119. try
  1120. {
  1121. if (_ScheduleQueue == null)
  1122. {
  1123. _ScheduleQueue = new Collection();
  1124. }
  1125. Interlocked.Increment(ref Reads);
  1126. }
  1127. finally
  1128. {
  1129. objQueueReadWriteLock.ReleaseReaderLock();
  1130. }
  1131. }
  1132. catch (ApplicationException exception1)
  1133. {
  1134. ApplicationException ex = exception1;
  1135. Interlocked.Increment(ref ReadTimeout);
  1136. DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
  1137. }
  1138. return _ScheduleQueue;
  1139. }
  1140. set
  1141. {
  1142. try
  1143. {
  1144. objQueueReadWriteLock.AcquireWriterLock(WriteTimeout);
  1145. try
  1146. {
  1147. DataCache.SetCache("ScheduleQueue", value);
  1148. _ScheduleQueue = value;
  1149. Interlocked.Increment(ref Writes);
  1150. }
  1151. finally
  1152. {
  1153. objQueueReadWriteLock.ReleaseWriterLock();
  1154. }
  1155. }
  1156. catch (ApplicationException exception1)
  1157. {
  1158. ApplicationException ex = exception1;
  1159. Interlocked.Increment(ref WriterTimeouts);
  1160. DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
  1161. }
  1162. }
  1163. }
  1164. }
  1165. }
  1166. }