/Provider/SchedulingProviders/DNNScheduler/DotNetNuke.Services.Scheduling.DNNScheduling/Scheduler.cs

# · C# · 1278 lines · 1171 code · 107 blank · 0 comment · 130 complexity · d6f4090a5134abf36d015d63f19bfd36 MD5 · raw file

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