job_test.rb 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. require 'test_helper'
  2. class JobTest < ActiveSupport::TestCase
  3. test 'case 1' do
  4. # create ticket
  5. group1 = Group.lookup(name: 'Users')
  6. group2 = Group.create_or_update(
  7. name: 'JobTest2',
  8. updated_by_id: 1,
  9. created_by_id: 1,
  10. )
  11. ticket1 = Ticket.create!(
  12. title: 'job test 1',
  13. group: group1,
  14. customer_id: 2,
  15. state: Ticket::State.lookup(name: 'new'),
  16. priority: Ticket::Priority.lookup(name: '2 normal'),
  17. created_at: Time.zone.now - 3.days,
  18. updated_at: Time.zone.now - 3.days,
  19. created_by_id: 1,
  20. updated_by_id: 1,
  21. )
  22. ticket2 = Ticket.create!(
  23. title: 'job test 2',
  24. group: group1,
  25. customer_id: 2,
  26. state: Ticket::State.lookup(name: 'new'),
  27. priority: Ticket::Priority.lookup(name: '2 normal'),
  28. created_at: Time.zone.now - 1.day,
  29. created_by_id: 1,
  30. updated_at: Time.zone.now - 1.day,
  31. updated_by_id: 1,
  32. )
  33. ticket3 = Ticket.create!(
  34. title: 'job test 3',
  35. group: group2,
  36. customer_id: 2,
  37. state: Ticket::State.lookup(name: 'open'),
  38. priority: Ticket::Priority.lookup(name: '3 high'),
  39. created_at: Time.zone.now - 1.day,
  40. created_by_id: 1,
  41. updated_at: Time.zone.now - 1.day,
  42. updated_by_id: 1,
  43. )
  44. ticket4 = Ticket.create!(
  45. title: 'job test 4',
  46. group: group2,
  47. customer_id: 2,
  48. state: Ticket::State.lookup(name: 'closed'),
  49. priority: Ticket::Priority.lookup(name: '2 normal'),
  50. created_at: Time.zone.now - 3.days,
  51. created_by_id: 1,
  52. updated_at: Time.zone.now - 3.days,
  53. updated_by_id: 1,
  54. )
  55. ticket5 = Ticket.create!(
  56. title: 'job test 5',
  57. group: group2,
  58. customer_id: 2,
  59. state: Ticket::State.lookup(name: 'open'),
  60. priority: Ticket::Priority.lookup(name: '2 normal'),
  61. created_at: Time.zone.now - 3.days,
  62. created_by_id: 1,
  63. updated_by_id: 1,
  64. updated_at: Time.zone.now - 3.days,
  65. )
  66. # create jobs
  67. job1 = Job.create_or_update(
  68. name: 'Test Job1',
  69. timeplan: {
  70. days: {
  71. Mon: false,
  72. Tue: false,
  73. Wed: false,
  74. Thu: false,
  75. Fri: false,
  76. Sat: false,
  77. Sun: false,
  78. },
  79. hours: {
  80. 0 => false,
  81. 1 => false,
  82. 2 => false,
  83. 3 => false,
  84. 4 => false,
  85. 5 => false,
  86. 6 => false,
  87. 7 => false,
  88. 8 => false,
  89. 9 => false,
  90. 10 => false,
  91. 11 => false,
  92. 12 => false,
  93. 13 => false,
  94. 14 => false,
  95. 15 => false,
  96. 16 => false,
  97. 17 => false,
  98. 18 => false,
  99. 19 => false,
  100. 20 => false,
  101. 21 => false,
  102. 22 => false,
  103. 23 => false,
  104. },
  105. minutes: {
  106. 0 => false,
  107. 10 => false,
  108. 20 => false,
  109. 30 => false,
  110. 40 => false,
  111. 50 => false,
  112. },
  113. },
  114. condition: {
  115. 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
  116. 'ticket.created_at' => { 'operator' => 'before (relative)', 'value' => '2', 'range' => 'day' },
  117. },
  118. perform: {
  119. 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s }
  120. },
  121. disable_notification: true,
  122. last_run_at: nil,
  123. active: true,
  124. created_by_id: 1,
  125. created_at: Time.zone.now,
  126. updated_by_id: 1,
  127. updated_at: Time.zone.now,
  128. )
  129. assert_not(job1.next_run_at)
  130. assert_not(job1.executable?)
  131. job1.last_run_at = Time.zone.now - 15.minutes
  132. job1.save!
  133. assert_not(job1.executable?)
  134. job1.updated_at = Time.zone.now - 15.minutes
  135. job1.save!
  136. assert(job1.executable?)
  137. job1.active = false
  138. job1.save!
  139. assert_not(job1.executable?)
  140. job1.active = true
  141. job1.save!
  142. assert_not(job1.executable?)
  143. assert_not(job1.in_timeplan?)
  144. time = Time.zone.now
  145. # "freeze" time to avoid timing issues
  146. travel_to(time)
  147. day_map = {
  148. 0 => 'Sun',
  149. 1 => 'Mon',
  150. 2 => 'Tue',
  151. 3 => 'Wed',
  152. 4 => 'Thu',
  153. 5 => 'Fri',
  154. 6 => 'Sat',
  155. }
  156. job1.timeplan['days'][day_map[time.wday]] = true
  157. job1.save!
  158. assert_not(job1.in_timeplan?(time))
  159. job1.timeplan['hours'][time.hour.to_s] = true
  160. job1.save!
  161. assert_not(job1.in_timeplan?(time))
  162. min = time.min
  163. if min < 9
  164. min = 0
  165. elsif min < 20
  166. min = 10
  167. elsif min < 30
  168. min = 20
  169. elsif min < 40
  170. min = 30
  171. elsif min < 50
  172. min = 40
  173. elsif min < 60
  174. min = 50
  175. end
  176. job1.timeplan['minutes'][min.to_s] = true
  177. job1.save!
  178. assert(job1.in_timeplan?(time))
  179. job1.timeplan['hours'][time.hour] = true
  180. job1.save!
  181. job1.timeplan['minutes'][min] = true
  182. job1.save!
  183. assert(job1.in_timeplan?(time))
  184. # execute jobs
  185. job1.updated_at = Time.zone.now - 15.minutes
  186. job1.save!
  187. Job.run
  188. assert(job1.next_run_at)
  189. assert(job1.executable?)
  190. assert(job1.in_timeplan?)
  191. # verify changes on tickets
  192. ticket1_later = Ticket.find(ticket1.id)
  193. assert_equal('closed', ticket1_later.state.name)
  194. assert_not_equal(ticket1.updated_at.to_s, ticket1_later.updated_at.to_s)
  195. ticket2_later = Ticket.find(ticket2.id)
  196. assert_equal('new', ticket2_later.state.name)
  197. assert_equal(ticket2.updated_at.to_s, ticket2_later.updated_at.to_s)
  198. ticket3_later = Ticket.find(ticket3.id)
  199. assert_equal('open', ticket3_later.state.name)
  200. assert_equal(ticket3.updated_at.to_s, ticket3_later.updated_at.to_s)
  201. ticket4_later = Ticket.find(ticket4.id)
  202. assert_equal('closed', ticket4_later.state.name)
  203. assert_equal(ticket4.updated_at.to_s, ticket4_later.updated_at.to_s)
  204. ticket5_later = Ticket.find(ticket5.id)
  205. assert_equal('closed', ticket5_later.state.name)
  206. assert_not_equal(ticket5.updated_at.to_s, ticket5_later.updated_at.to_s)
  207. # execute jobs again
  208. job1.updated_at = Time.zone.now - 15.minutes
  209. job1.save!
  210. Job.run
  211. # verify changes on tickets
  212. ticket1_later_next = Ticket.find(ticket1.id)
  213. assert_equal('closed', ticket1_later_next.state.name)
  214. assert_equal(ticket1_later.updated_at.to_s, ticket1_later_next.updated_at.to_s)
  215. ticket2_later_next = Ticket.find(ticket2.id)
  216. assert_equal('new', ticket2_later_next.state.name)
  217. assert_equal(ticket2_later.updated_at.to_s, ticket2_later_next.updated_at.to_s)
  218. ticket3_later_next = Ticket.find(ticket3.id)
  219. assert_equal('open', ticket3_later_next.state.name)
  220. assert_equal(ticket3_later.updated_at.to_s, ticket3_later_next.updated_at.to_s)
  221. ticket4_later_next = Ticket.find(ticket4.id)
  222. assert_equal('closed', ticket4_later_next.state.name)
  223. assert_equal(ticket4_later.updated_at.to_s, ticket4_later_next.updated_at.to_s)
  224. ticket5_later_next = Ticket.find(ticket5.id)
  225. assert_equal('closed', ticket5_later_next.state.name)
  226. assert_equal(ticket5_later.updated_at.to_s, ticket5_later_next.updated_at.to_s)
  227. end
  228. test 'with invalid state_id' do
  229. # create ticket
  230. group1 = Group.lookup(name: 'Users')
  231. group2 = Group.create_or_update(
  232. name: 'JobTest2',
  233. updated_by_id: 1,
  234. created_by_id: 1,
  235. )
  236. ticket1 = Ticket.create!(
  237. title: 'job test 1',
  238. group: group1,
  239. customer_id: 2,
  240. state: Ticket::State.lookup(name: 'new'),
  241. priority: Ticket::Priority.lookup(name: '2 normal'),
  242. created_at: Time.zone.now - 3.days,
  243. updated_at: Time.zone.now - 3.days,
  244. created_by_id: 1,
  245. updated_by_id: 1,
  246. )
  247. ticket2 = Ticket.create!(
  248. title: 'job test 2',
  249. group: group1,
  250. customer_id: 2,
  251. state: Ticket::State.lookup(name: 'new'),
  252. priority: Ticket::Priority.lookup(name: '2 normal'),
  253. created_at: Time.zone.now - 1.day,
  254. created_by_id: 1,
  255. updated_at: Time.zone.now - 1.day,
  256. updated_by_id: 1,
  257. )
  258. # create jobs
  259. job1 = Job.create_or_update(
  260. name: 'Test Job1',
  261. timeplan: {
  262. days: {
  263. Mon: true,
  264. Tue: true,
  265. Wed: true,
  266. Thu: true,
  267. Fri: true,
  268. Sat: true,
  269. Sun: true,
  270. },
  271. hours: {
  272. 0 => true,
  273. 1 => true,
  274. 2 => true,
  275. 3 => true,
  276. 4 => true,
  277. 5 => true,
  278. 6 => true,
  279. 7 => true,
  280. 8 => true,
  281. 9 => true,
  282. 10 => true,
  283. 11 => true,
  284. 12 => true,
  285. 13 => true,
  286. 14 => true,
  287. 15 => true,
  288. 16 => true,
  289. 17 => true,
  290. 18 => true,
  291. 19 => true,
  292. 20 => true,
  293. 21 => true,
  294. 22 => true,
  295. 23 => true,
  296. },
  297. minutes: {
  298. 0 => true,
  299. 10 => true,
  300. 20 => true,
  301. 30 => true,
  302. 40 => true,
  303. 50 => true,
  304. },
  305. },
  306. condition: {
  307. 'ticket.state_id' => { 'operator' => 'is', 'value' => '9999' },
  308. 'ticket.created_at' => { 'operator' => 'before (relative)', 'value' => '2', 'range' => 'day' },
  309. },
  310. perform: {
  311. 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s }
  312. },
  313. disable_notification: true,
  314. last_run_at: nil,
  315. updated_at: Time.zone.now - 15.minutes,
  316. active: true,
  317. updated_by_id: 1,
  318. created_by_id: 1,
  319. )
  320. assert(job1.executable?)
  321. assert(job1.in_timeplan?)
  322. Job.run
  323. # verify changes on tickets
  324. ticket1_later = Ticket.find(ticket1.id)
  325. assert_equal('new', ticket1_later.state.name)
  326. assert_equal(ticket1.updated_at.to_s, ticket1_later.updated_at.to_s)
  327. ticket2_later = Ticket.find(ticket2.id)
  328. assert_equal('new', ticket2_later.state.name)
  329. assert_equal(ticket2.updated_at.to_s, ticket2_later.updated_at.to_s)
  330. end
  331. test 'case 3' do
  332. # create jobs
  333. job1 = Job.create_or_update(
  334. name: 'Test Job1',
  335. timeplan: {
  336. days: {
  337. Mon: true,
  338. Tue: false,
  339. Wed: false,
  340. Thu: false,
  341. Fri: true,
  342. Sat: false,
  343. Sun: false,
  344. },
  345. hours: {
  346. 0 => false,
  347. 1 => true,
  348. 2 => false,
  349. 3 => false,
  350. 4 => false,
  351. 5 => false,
  352. 6 => false,
  353. 7 => false,
  354. 8 => false,
  355. 9 => false,
  356. 10 => true,
  357. 11 => false,
  358. 12 => false,
  359. 13 => false,
  360. 14 => false,
  361. 15 => false,
  362. 16 => false,
  363. 17 => false,
  364. 18 => false,
  365. 19 => false,
  366. 20 => false,
  367. 21 => false,
  368. 22 => false,
  369. 23 => false,
  370. },
  371. minutes: {
  372. 0 => true,
  373. 10 => false,
  374. 20 => false,
  375. 30 => false,
  376. 40 => true,
  377. 50 => false,
  378. },
  379. },
  380. condition: {
  381. 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
  382. 'ticket.created_at' => { 'operator' => 'before (relative)', 'value' => '2', 'range' => 'day' },
  383. },
  384. perform: {
  385. 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s }
  386. },
  387. disable_notification: true,
  388. last_run_at: nil,
  389. active: true,
  390. created_by_id: 1,
  391. created_at: Time.zone.now,
  392. updated_by_id: 1,
  393. updated_at: Time.zone.now,
  394. )
  395. time_now = Time.zone.parse('2016-03-18 09:17:13 UTC')
  396. next_run_at = job1.next_run_at_calculate(time_now)
  397. assert_equal('2016-03-18 10:00:00 UTC', next_run_at.to_s)
  398. time_now = Time.zone.parse('2016-03-18 10:37:13 UTC')
  399. next_run_at = job1.next_run_at_calculate(time_now)
  400. assert_equal('2016-03-18 10:40:00 UTC', next_run_at.to_s)
  401. time_now = Time.zone.parse('2016-03-17 09:17:13 UTC')
  402. next_run_at = job1.next_run_at_calculate(time_now)
  403. assert_equal('2016-03-18 01:00:00 UTC', next_run_at.to_s)
  404. time_now = Time.zone.parse('2016-03-17 11:17:13 UTC')
  405. next_run_at = job1.next_run_at_calculate(time_now)
  406. assert_equal('2016-03-18 01:00:00 UTC', next_run_at.to_s)
  407. time_now = Time.zone.parse('2016-03-19 11:17:13 UTC')
  408. next_run_at = job1.next_run_at_calculate(time_now)
  409. assert_equal('2016-03-21 01:00:00 UTC', next_run_at.to_s)
  410. time_now = Time.zone.parse('2016-03-22 00:59:59 UTC')
  411. next_run_at = job1.next_run_at_calculate(time_now)
  412. assert_equal('2016-03-25 01:00:00 UTC', next_run_at.to_s)
  413. time_now = Time.zone.parse('2016-03-25 00:59:59 UTC')
  414. next_run_at = job1.next_run_at_calculate(time_now)
  415. assert_equal('2016-03-25 01:00:00 UTC', next_run_at.to_s)
  416. time_now = Time.zone.parse('2016-03-24 00:59:59 UTC')
  417. next_run_at = job1.next_run_at_calculate(time_now)
  418. assert_equal('2016-03-25 01:00:00 UTC', next_run_at.to_s)
  419. time_now = Time.zone.parse('2016-03-24 23:59:59 UTC')
  420. next_run_at = job1.next_run_at_calculate(time_now)
  421. assert_equal('2016-03-25 01:00:00 UTC', next_run_at.to_s)
  422. time_now = Time.zone.parse('2016-03-25 01:00:01 UTC')
  423. next_run_at = job1.next_run_at_calculate(time_now)
  424. assert_equal('2016-03-25 01:00:00 UTC', next_run_at.to_s)
  425. time_now = Time.zone.parse('2016-03-25 01:09:01 UTC')
  426. next_run_at = job1.next_run_at_calculate(time_now)
  427. assert_equal('2016-03-25 01:40:00 UTC', next_run_at.to_s)
  428. time_now = Time.zone.parse('2016-03-25 01:09:59 UTC')
  429. next_run_at = job1.next_run_at_calculate(time_now)
  430. assert_equal('2016-03-25 01:40:00 UTC', next_run_at.to_s)
  431. job1.last_run_at = Time.zone.parse('2016-03-18 10:00:01 UTC')
  432. job1.save!
  433. time_now = Time.zone.parse('2016-03-18 10:00:02 UTC')
  434. next_run_at = job1.next_run_at_calculate(time_now)
  435. assert_equal('2016-03-18 10:40:00 UTC', next_run_at.to_s)
  436. job1.last_run_at = Time.zone.parse('2016-03-18 10:40:01 UTC')
  437. job1.save!
  438. time_now = Time.zone.parse('2016-03-18 10:40:02 UTC')
  439. next_run_at = job1.next_run_at_calculate(time_now)
  440. assert_equal('2016-03-21 01:00:00 UTC', next_run_at.to_s)
  441. end
  442. test 'case 4' do
  443. # create jobs
  444. job1 = Job.create_or_update(
  445. name: 'Test Job1',
  446. timeplan: {
  447. days: {
  448. Mon: true,
  449. Tue: false,
  450. Wed: false,
  451. Thu: false,
  452. Fri: true,
  453. Sat: false,
  454. Sun: false,
  455. },
  456. hours: {
  457. 0 => true,
  458. 1 => false,
  459. 2 => false,
  460. 3 => false,
  461. 4 => false,
  462. 5 => false,
  463. 6 => false,
  464. 7 => false,
  465. 8 => false,
  466. 9 => false,
  467. 10 => true,
  468. 11 => false,
  469. 12 => false,
  470. 13 => false,
  471. 14 => false,
  472. 15 => false,
  473. 16 => false,
  474. 17 => false,
  475. 18 => false,
  476. 19 => false,
  477. 20 => false,
  478. 21 => false,
  479. 22 => false,
  480. 23 => false,
  481. },
  482. minutes: {
  483. 0 => true,
  484. 10 => false,
  485. 20 => false,
  486. 30 => false,
  487. 40 => true,
  488. 50 => false,
  489. },
  490. },
  491. condition: {
  492. 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
  493. 'ticket.created_at' => { 'operator' => 'before (relative)', 'value' => '2', 'range' => 'day' },
  494. },
  495. perform: {
  496. 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s }
  497. },
  498. disable_notification: true,
  499. last_run_at: nil,
  500. active: true,
  501. created_by_id: 1,
  502. created_at: Time.zone.now,
  503. updated_by_id: 1,
  504. updated_at: Time.zone.now,
  505. )
  506. time_now = Time.zone.parse('2016-03-17 23:51:23 UTC')
  507. next_run_at = job1.next_run_at_calculate(time_now)
  508. assert_equal('2016-03-18 00:00:00 UTC', next_run_at.to_s)
  509. job1.last_run_at = Time.zone.parse('2016-03-17 23:45:01 UTC')
  510. job1.save!
  511. time_now = Time.zone.parse('2016-03-17 23:51:23 UTC')
  512. next_run_at = job1.next_run_at_calculate(time_now)
  513. assert_equal('2016-03-18 00:00:00 UTC', next_run_at.to_s)
  514. job1.last_run_at = Time.zone.parse('2016-03-17 23:59:01 UTC')
  515. job1.save!
  516. time_now = Time.zone.parse('2016-03-17 23:59:23 UTC')
  517. next_run_at = job1.next_run_at_calculate(time_now)
  518. assert_equal('2016-03-18 00:40:00 UTC', next_run_at.to_s)
  519. time_now = Time.zone.parse('2016-03-17 23:59:23 UTC')
  520. assert_not(job1.in_timeplan?(time_now))
  521. time_now = Time.zone.parse('2016-03-18 00:01:23 UTC')
  522. assert(job1.in_timeplan?(time_now))
  523. end
  524. test 'check next_run_at' do
  525. job1 = Job.create_or_update(
  526. name: 'Test Job1',
  527. timeplan: {
  528. days: {
  529. Mon: true,
  530. Tue: false,
  531. Wed: false,
  532. Thu: false,
  533. Fri: false,
  534. Sat: false,
  535. Sun: false,
  536. },
  537. hours: {
  538. '0' => true,
  539. '1' => false,
  540. '2' => false,
  541. '3' => false,
  542. '4' => false,
  543. '5' => false,
  544. '6' => false,
  545. '7' => false,
  546. '8' => false,
  547. '9' => false,
  548. '10' => false,
  549. '11' => false,
  550. '12' => false,
  551. '13' => false,
  552. '14' => false,
  553. '15' => false,
  554. '16' => false,
  555. '17' => false,
  556. '18' => false,
  557. '19' => false,
  558. '20' => false,
  559. '21' => false,
  560. '22' => false,
  561. '23' => false,
  562. },
  563. minutes: {
  564. '0' => true,
  565. '10' => false,
  566. '20' => false,
  567. '30' => false,
  568. '40' => false,
  569. '50' => false,
  570. },
  571. },
  572. condition: {
  573. 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
  574. 'ticket.created_at' => { 'operator' => 'before (relative)', 'value' => '2', 'range' => 'day' },
  575. },
  576. perform: {
  577. 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s }
  578. },
  579. disable_notification: true,
  580. last_run_at: nil,
  581. active: true,
  582. created_by_id: 1,
  583. created_at: Time.zone.now,
  584. updated_by_id: 1,
  585. updated_at: Time.zone.now,
  586. )
  587. time_now = Time.zone.parse('2016-03-17 23:51:23 UTC')
  588. next_run_at = job1.next_run_at_calculate(time_now)
  589. assert_equal('2016-03-21 00:00:00 UTC', next_run_at.to_s)
  590. end
  591. test 'update next run at' do
  592. travel_to Time.zone.local(2017, 11, 10, 22, 0o4, 44)
  593. job1 = Job.create_or_update(
  594. name: 'Test Job1',
  595. timeplan: {
  596. days: {
  597. Mon: false,
  598. Tue: false,
  599. Wed: false,
  600. Thu: false,
  601. Fri: false,
  602. Sat: true,
  603. Sun: false,
  604. },
  605. hours: {
  606. '0' => false,
  607. '1' => false,
  608. '2' => false,
  609. '3' => false,
  610. '4' => false,
  611. '5' => false,
  612. '6' => false,
  613. '7' => false,
  614. '8' => false,
  615. '9' => false,
  616. '10' => false,
  617. '11' => false,
  618. '12' => false,
  619. '13' => false,
  620. '14' => false,
  621. '15' => false,
  622. '16' => false,
  623. '17' => false,
  624. '18' => false,
  625. '19' => false,
  626. '20' => false,
  627. '21' => false,
  628. '22' => false,
  629. '23' => true,
  630. },
  631. minutes: {
  632. '0' => true,
  633. '10' => false,
  634. '20' => false,
  635. '30' => false,
  636. '40' => false,
  637. '50' => false,
  638. },
  639. },
  640. condition: {
  641. 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
  642. },
  643. perform: {
  644. 'ticket.action' => { 'value' => 'delete' },
  645. },
  646. disable_notification: true,
  647. last_run_at: nil,
  648. active: true,
  649. created_by_id: 1,
  650. created_at: Time.zone.now,
  651. updated_by_id: 1,
  652. updated_at: Time.zone.now,
  653. )
  654. assert_equal('2017-11-11 23:00:00 UTC', job1.next_run_at.to_s)
  655. assert_not(job1.last_run_at)
  656. travel_to Time.zone.local(2017, 11, 16, 22, 0o4, 44)
  657. Job.run
  658. job1.reload
  659. assert_equal('2017-11-18 23:00:00 UTC', job1.next_run_at.to_s)
  660. assert_not(job1.last_run_at)
  661. travel_back
  662. end
  663. test 'execute on certain time' do
  664. travel_to Time.zone.local(2017, 11, 16, 22, 0o4, 44)
  665. group1 = Group.lookup(name: 'Users')
  666. ticket1 = Ticket.create!(
  667. title: 'job test 1',
  668. group: group1,
  669. customer_id: 2,
  670. state: Ticket::State.lookup(name: 'new'),
  671. priority: Ticket::Priority.lookup(name: '2 normal'),
  672. created_by_id: 1,
  673. updated_by_id: 1,
  674. )
  675. ticket2 = Ticket.create!(
  676. title: 'job test 2',
  677. group: group1,
  678. customer_id: 2,
  679. state: Ticket::State.lookup(name: 'new'),
  680. priority: Ticket::Priority.lookup(name: '2 normal'),
  681. created_by_id: 1,
  682. updated_by_id: 1,
  683. )
  684. job1 = Job.create_or_update(
  685. name: 'Test Job1',
  686. timeplan: {
  687. days: {
  688. Mon: false,
  689. Tue: false,
  690. Wed: false,
  691. Thu: true,
  692. Fri: false,
  693. Sat: false,
  694. Sun: false,
  695. },
  696. hours: {
  697. '0' => false,
  698. '1' => false,
  699. '2' => false,
  700. '3' => false,
  701. '4' => false,
  702. '5' => false,
  703. '6' => false,
  704. '7' => false,
  705. '8' => false,
  706. '9' => false,
  707. '10' => false,
  708. '11' => false,
  709. '12' => false,
  710. '13' => false,
  711. '14' => false,
  712. '15' => false,
  713. '16' => false,
  714. '17' => false,
  715. '18' => false,
  716. '19' => false,
  717. '20' => false,
  718. '21' => false,
  719. '22' => false,
  720. '23' => true,
  721. },
  722. minutes: {
  723. '0' => true,
  724. '10' => false,
  725. '20' => false,
  726. '30' => false,
  727. '40' => false,
  728. '50' => false,
  729. },
  730. },
  731. condition: {
  732. 'ticket.state_id' => { 'operator' => 'is', 'value' => [Ticket::State.lookup(name: 'new').id.to_s, Ticket::State.lookup(name: 'open').id.to_s] },
  733. },
  734. perform: {
  735. 'ticket.action' => { 'value' => 'delete' },
  736. },
  737. disable_notification: true,
  738. last_run_at: nil,
  739. active: true,
  740. created_by_id: 1,
  741. created_at: Time.zone.now,
  742. updated_by_id: 1,
  743. updated_at: Time.zone.now,
  744. )
  745. Job.run
  746. assert(Ticket.find_by(id: ticket1.id))
  747. assert(Ticket.find_by(id: ticket2.id))
  748. travel_to Time.zone.local(2017, 11, 16, 23, 0o4, 44)
  749. Job.run
  750. assert_not(Ticket.find_by(id: ticket1.id))
  751. assert_not(Ticket.find_by(id: ticket2.id))
  752. travel_back
  753. end
  754. test 'delete based on tag' do
  755. # create ticket
  756. group1 = Group.lookup(name: 'Users')
  757. group2 = Group.create_or_update(
  758. name: 'JobTest2',
  759. updated_by_id: 1,
  760. created_by_id: 1,
  761. )
  762. ticket1 = Ticket.create!(
  763. title: 'job test 1',
  764. group: group1,
  765. customer_id: 2,
  766. state: Ticket::State.lookup(name: 'new'),
  767. priority: Ticket::Priority.lookup(name: '2 normal'),
  768. created_at: Time.zone.now - 3.days,
  769. updated_at: Time.zone.now - 3.days,
  770. created_by_id: 1,
  771. updated_by_id: 1,
  772. )
  773. ticket1.tag_add('spam', 1)
  774. ticket1.tag_add('test1 ', 1)
  775. ticket2 = Ticket.create!(
  776. title: 'job test 2',
  777. group: group1,
  778. customer_id: 2,
  779. state: Ticket::State.lookup(name: 'new'),
  780. priority: Ticket::Priority.lookup(name: '2 normal'),
  781. created_at: Time.zone.now - 1.day,
  782. created_by_id: 1,
  783. updated_at: Time.zone.now - 1.day,
  784. updated_by_id: 1,
  785. )
  786. job1 = Job.create_or_update(
  787. name: 'Test Job1',
  788. timeplan: {
  789. days: {
  790. Mon: true,
  791. Tue: true,
  792. Wed: true,
  793. Thu: true,
  794. Fri: true,
  795. Sat: true,
  796. Sun: true,
  797. },
  798. hours: {
  799. 0 => true,
  800. 1 => true,
  801. 2 => true,
  802. 3 => true,
  803. 4 => true,
  804. 5 => true,
  805. 6 => true,
  806. 7 => true,
  807. 8 => true,
  808. 9 => true,
  809. 10 => true,
  810. 11 => true,
  811. 12 => true,
  812. 13 => true,
  813. 14 => true,
  814. 15 => true,
  815. 16 => true,
  816. 17 => true,
  817. 18 => true,
  818. 19 => true,
  819. 20 => true,
  820. 21 => true,
  821. 22 => true,
  822. 23 => true,
  823. },
  824. minutes: {
  825. 0 => true,
  826. 10 => true,
  827. 20 => true,
  828. 30 => true,
  829. 40 => true,
  830. 50 => true,
  831. },
  832. },
  833. condition: {
  834. 'ticket.tags' => { 'operator' => 'contains one', 'value' => 'spam' },
  835. },
  836. perform: {
  837. 'ticket.action' => { 'value' => 'delete' },
  838. },
  839. disable_notification: true,
  840. last_run_at: nil,
  841. updated_at: Time.zone.now - 15.minutes,
  842. active: true,
  843. updated_by_id: 1,
  844. created_by_id: 1,
  845. )
  846. assert(job1.executable?)
  847. assert(job1.in_timeplan?)
  848. Job.run
  849. assert_not(Ticket.find_by(id: ticket1.id))
  850. assert(Ticket.find_by(id: ticket2.id))
  851. end
  852. test 'delete spam on time' do
  853. # create ticket
  854. group1 = Group.lookup(name: 'Users')
  855. group2 = Group.create_or_update(
  856. name: 'JobTest2',
  857. updated_by_id: 1,
  858. created_by_id: 1,
  859. )
  860. ticket1 = Ticket.create!(
  861. title: 'job test 1',
  862. group: group1,
  863. customer_id: 2,
  864. state: Ticket::State.lookup(name: 'closed'),
  865. priority: Ticket::Priority.lookup(name: '2 normal'),
  866. created_by_id: 1,
  867. updated_by_id: 1,
  868. )
  869. ticket2 = Ticket.create!(
  870. title: 'job test 2',
  871. group: group2,
  872. customer_id: 2,
  873. state: Ticket::State.lookup(name: 'closed'),
  874. priority: Ticket::Priority.lookup(name: '2 normal'),
  875. created_by_id: 1,
  876. updated_by_id: 1,
  877. )
  878. travel_to Time.zone.parse('2018-08-13T23:01:01Z')
  879. job1 = Job.create_or_update(
  880. name: 'Spam entfernen',
  881. timeplan: {
  882. days: {
  883. Mon: true,
  884. Tue: false,
  885. Wed: true,
  886. Thu: false,
  887. Fri: true,
  888. Sat: false,
  889. Sun: false,
  890. },
  891. hours: {
  892. '0' => false,
  893. '1' => false,
  894. '2' => false,
  895. '3' => false,
  896. '4' => false,
  897. '5' => false,
  898. '6' => false,
  899. '7' => false,
  900. '8' => false,
  901. '9' => false,
  902. '10' => false,
  903. '11' => false,
  904. '12' => false,
  905. '13' => false,
  906. '14' => false,
  907. '15' => false,
  908. '16' => false,
  909. '17' => false,
  910. '18' => false,
  911. '19' => false,
  912. '20' => false,
  913. '21' => false,
  914. '22' => false,
  915. '23' => true,
  916. },
  917. minutes: {
  918. '0' => true,
  919. '10' => false,
  920. '20' => false,
  921. '30' => false,
  922. '40' => false,
  923. '50' => false,
  924. }
  925. },
  926. condition: {
  927. 'ticket.group_id' => { 'operator' => 'is', 'value' => group1.id },
  928. 'ticket.state_id' => { 'operator' => 'is', 'value' => Ticket::State.find_by(name: 'closed').id }
  929. },
  930. perform: {
  931. 'ticket.action' => { 'value' => 'delete' }
  932. },
  933. disable_notification: true,
  934. last_run_at: nil,
  935. next_run_at: '2018-08-13 23:00:00',
  936. running: false,
  937. processed: 19_393,
  938. matching: 19_428,
  939. updated_at: Time.zone.now - 15.minutes,
  940. active: true,
  941. updated_by_id: 1,
  942. created_by_id: 1,
  943. )
  944. assert(job1.executable?)
  945. assert(job1.in_timeplan?)
  946. Job.run
  947. assert_not(Ticket.find_by(id: ticket1.id))
  948. assert(Ticket.find_by(id: ticket2.id))
  949. ticket1 = Ticket.create!(
  950. title: 'job test 1',
  951. group: group1,
  952. customer_id: 2,
  953. state: Ticket::State.lookup(name: 'closed'),
  954. priority: Ticket::Priority.lookup(name: '2 normal'),
  955. created_by_id: 1,
  956. updated_by_id: 1,
  957. )
  958. travel_to Time.zone.parse('2018-08-15T23:01:01Z')
  959. job1.running = true
  960. job1.save!
  961. assert_not(job1.executable?)
  962. assert(job1.in_timeplan?)
  963. Job.run
  964. assert(Ticket.find_by(id: ticket1.id))
  965. assert(Ticket.find_by(id: ticket2.id))
  966. travel_to Time.zone.parse('2018-08-17T23:08:01Z')
  967. assert(job1.executable?)
  968. assert(job1.in_timeplan?)
  969. Job.run
  970. assert_not(Ticket.find_by(id: ticket1.id))
  971. assert(Ticket.find_by(id: ticket2.id))
  972. end
  973. end