test_beat.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. from __future__ import absolute_import, unicode_literals
  2. import errno
  3. from datetime import datetime, timedelta
  4. from pickle import dumps, loads
  5. import pytest
  6. from case import Mock, call, patch, skip
  7. from celery import __version__, beat, uuid
  8. from celery.beat import event_t
  9. from celery.five import keys, string_t
  10. from celery.schedules import crontab, schedule
  11. from celery.utils.objects import Bunch
  12. class MockShelve(dict):
  13. closed = False
  14. synced = False
  15. def close(self):
  16. self.closed = True
  17. def sync(self):
  18. self.synced = True
  19. class MockService(object):
  20. started = False
  21. stopped = False
  22. def __init__(self, *args, **kwargs):
  23. pass
  24. def start(self, **kwargs):
  25. self.started = True
  26. def stop(self, **kwargs):
  27. self.stopped = True
  28. class test_ScheduleEntry:
  29. Entry = beat.ScheduleEntry
  30. def create_entry(self, **kwargs):
  31. entry = {
  32. 'name': 'celery.unittest.add',
  33. 'schedule': timedelta(seconds=10),
  34. 'args': (2, 2),
  35. 'options': {'routing_key': 'cpu'},
  36. 'app': self.app,
  37. }
  38. return self.Entry(**dict(entry, **kwargs))
  39. def test_next(self):
  40. entry = self.create_entry(schedule=10)
  41. assert entry.last_run_at
  42. assert isinstance(entry.last_run_at, datetime)
  43. assert entry.total_run_count == 0
  44. next_run_at = entry.last_run_at + timedelta(seconds=10)
  45. next_entry = entry.next(next_run_at)
  46. assert next_entry.last_run_at >= next_run_at
  47. assert next_entry.total_run_count == 1
  48. def test_is_due(self):
  49. entry = self.create_entry(schedule=timedelta(seconds=10))
  50. assert entry.app is self.app
  51. assert entry.schedule.app is self.app
  52. due1, next_time_to_run1 = entry.is_due()
  53. assert not due1
  54. assert next_time_to_run1 > 9
  55. next_run_at = entry.last_run_at - timedelta(seconds=10)
  56. next_entry = entry.next(next_run_at)
  57. due2, next_time_to_run2 = next_entry.is_due()
  58. assert due2
  59. assert next_time_to_run2 > 9
  60. def test_repr(self):
  61. entry = self.create_entry()
  62. assert '<ScheduleEntry:' in repr(entry)
  63. def test_reduce(self):
  64. entry = self.create_entry(schedule=timedelta(seconds=10))
  65. fun, args = entry.__reduce__()
  66. res = fun(*args)
  67. assert res.schedule == entry.schedule
  68. def test_lt(self):
  69. e1 = self.create_entry(schedule=timedelta(seconds=10))
  70. e2 = self.create_entry(schedule=timedelta(seconds=2))
  71. # order doesn't matter, see comment in __lt__
  72. res1 = e1 < e2 # noqa
  73. try:
  74. res2 = e1 < object() # noqa
  75. except TypeError:
  76. pass
  77. def test_update(self):
  78. entry = self.create_entry()
  79. assert entry.schedule == timedelta(seconds=10)
  80. assert entry.args == (2, 2)
  81. assert entry.kwargs == {}
  82. assert entry.options == {'routing_key': 'cpu'}
  83. entry2 = self.create_entry(schedule=timedelta(minutes=20),
  84. args=(16, 16),
  85. kwargs={'callback': 'foo.bar.baz'},
  86. options={'routing_key': 'urgent'})
  87. entry.update(entry2)
  88. assert entry.schedule == schedule(timedelta(minutes=20))
  89. assert entry.args == (16, 16)
  90. assert entry.kwargs == {'callback': 'foo.bar.baz'}
  91. assert entry.options == {'routing_key': 'urgent'}
  92. class mScheduler(beat.Scheduler):
  93. def __init__(self, *args, **kwargs):
  94. self.sent = []
  95. beat.Scheduler.__init__(self, *args, **kwargs)
  96. def send_task(self, name=None, args=None, kwargs=None, **options):
  97. self.sent.append({'name': name,
  98. 'args': args,
  99. 'kwargs': kwargs,
  100. 'options': options})
  101. return self.app.AsyncResult(uuid())
  102. class mSchedulerSchedulingError(mScheduler):
  103. def send_task(self, *args, **kwargs):
  104. raise beat.SchedulingError('Could not apply task')
  105. class mSchedulerRuntimeError(mScheduler):
  106. def is_due(self, *args, **kwargs):
  107. raise RuntimeError('dict modified while itervalues')
  108. class mocked_schedule(schedule):
  109. def __init__(self, is_due, next_run_at):
  110. self._is_due = is_due
  111. self._next_run_at = next_run_at
  112. self.run_every = timedelta(seconds=1)
  113. self.nowfun = datetime.utcnow
  114. def is_due(self, last_run_at):
  115. return self._is_due, self._next_run_at
  116. always_due = mocked_schedule(True, 1)
  117. always_pending = mocked_schedule(False, 1)
  118. class test_Scheduler:
  119. def test_custom_schedule_dict(self):
  120. custom = {'foo': 'bar'}
  121. scheduler = mScheduler(app=self.app, schedule=custom, lazy=True)
  122. assert scheduler.data is custom
  123. def test_apply_async_uses_registered_task_instances(self):
  124. @self.app.task(shared=False)
  125. def foo():
  126. pass
  127. foo.apply_async = Mock(name='foo.apply_async')
  128. assert foo.name in foo._get_app().tasks
  129. scheduler = mScheduler(app=self.app)
  130. scheduler.apply_async(scheduler.Entry(task=foo.name, app=self.app))
  131. foo.apply_async.assert_called()
  132. def test_should_sync(self):
  133. @self.app.task(shared=False)
  134. def not_sync():
  135. pass
  136. not_sync.apply_async = Mock()
  137. s = mScheduler(app=self.app)
  138. s._do_sync = Mock()
  139. s.should_sync = Mock()
  140. s.should_sync.return_value = True
  141. s.apply_async(s.Entry(task=not_sync.name, app=self.app))
  142. s._do_sync.assert_called_with()
  143. s._do_sync = Mock()
  144. s.should_sync.return_value = False
  145. s.apply_async(s.Entry(task=not_sync.name, app=self.app))
  146. s._do_sync.assert_not_called()
  147. def test_should_sync_increments_sync_every_counter(self):
  148. self.app.conf.beat_sync_every = 2
  149. @self.app.task(shared=False)
  150. def not_sync():
  151. pass
  152. not_sync.apply_async = Mock()
  153. s = mScheduler(app=self.app)
  154. assert s.sync_every_tasks == 2
  155. s._do_sync = Mock()
  156. s.apply_async(s.Entry(task=not_sync.name, app=self.app))
  157. assert s._tasks_since_sync == 1
  158. s.apply_async(s.Entry(task=not_sync.name, app=self.app))
  159. s._do_sync.assert_called_with()
  160. self.app.conf.beat_sync_every = 0
  161. def test_sync_task_counter_resets_on_do_sync(self):
  162. self.app.conf.beat_sync_every = 1
  163. @self.app.task(shared=False)
  164. def not_sync():
  165. pass
  166. not_sync.apply_async = Mock()
  167. s = mScheduler(app=self.app)
  168. assert s.sync_every_tasks == 1
  169. s.apply_async(s.Entry(task=not_sync.name, app=self.app))
  170. assert s._tasks_since_sync == 0
  171. self.app.conf.beat_sync_every = 0
  172. @patch('celery.app.base.Celery.send_task')
  173. def test_send_task(self, send_task):
  174. b = beat.Scheduler(app=self.app)
  175. b.send_task('tasks.add', countdown=10)
  176. send_task.assert_called_with('tasks.add', countdown=10)
  177. def test_info(self):
  178. scheduler = mScheduler(app=self.app)
  179. assert isinstance(scheduler.info, string_t)
  180. def test_maybe_entry(self):
  181. s = mScheduler(app=self.app)
  182. entry = s.Entry(name='add every', task='tasks.add', app=self.app)
  183. assert s._maybe_entry(entry.name, entry) is entry
  184. assert s._maybe_entry('add every', {'task': 'tasks.add'})
  185. def test_set_schedule(self):
  186. s = mScheduler(app=self.app)
  187. s.schedule = {'foo': 'bar'}
  188. assert s.data == {'foo': 'bar'}
  189. @patch('kombu.connection.Connection.ensure_connection')
  190. def test_ensure_connection_error_handler(self, ensure):
  191. s = mScheduler(app=self.app)
  192. assert s._ensure_connected()
  193. ensure.assert_called()
  194. callback = ensure.call_args[0][0]
  195. callback(KeyError(), 5)
  196. def test_install_default_entries(self):
  197. self.app.conf.result_expires = None
  198. self.app.conf.beat_schedule = {}
  199. s = mScheduler(app=self.app)
  200. s.install_default_entries({})
  201. assert 'celery.backend_cleanup' not in s.data
  202. self.app.backend.supports_autoexpire = False
  203. self.app.conf.result_expires = 30
  204. s = mScheduler(app=self.app)
  205. s.install_default_entries({})
  206. assert 'celery.backend_cleanup' in s.data
  207. self.app.backend.supports_autoexpire = True
  208. self.app.conf.result_expires = 31
  209. s = mScheduler(app=self.app)
  210. s.install_default_entries({})
  211. assert 'celery.backend_cleanup' not in s.data
  212. def test_due_tick(self):
  213. scheduler = mScheduler(app=self.app)
  214. scheduler.add(name='test_due_tick',
  215. schedule=always_due,
  216. args=(1, 2),
  217. kwargs={'foo': 'bar'})
  218. assert scheduler.tick() == 0
  219. @patch('celery.beat.error')
  220. def test_due_tick_SchedulingError(self, error):
  221. scheduler = mSchedulerSchedulingError(app=self.app)
  222. scheduler.add(name='test_due_tick_SchedulingError',
  223. schedule=always_due)
  224. assert scheduler.tick() == 0
  225. error.assert_called()
  226. def test_pending_tick(self):
  227. scheduler = mScheduler(app=self.app)
  228. scheduler.add(name='test_pending_tick',
  229. schedule=always_pending)
  230. assert scheduler.tick() == 1 - 0.010
  231. def test_honors_max_interval(self):
  232. scheduler = mScheduler(app=self.app)
  233. maxi = scheduler.max_interval
  234. scheduler.add(name='test_honors_max_interval',
  235. schedule=mocked_schedule(False, maxi * 4))
  236. assert scheduler.tick() == maxi
  237. def test_ticks(self):
  238. scheduler = mScheduler(app=self.app)
  239. nums = [600, 300, 650, 120, 250, 36]
  240. s = {'test_ticks%s' % i: {'schedule': mocked_schedule(False, j)}
  241. for i, j in enumerate(nums)}
  242. scheduler.update_from_dict(s)
  243. assert scheduler.tick() == min(nums) - 0.010
  244. def test_ticks_schedule_change(self):
  245. # initialise schedule and check heap is not initialized
  246. scheduler = mScheduler(app=self.app)
  247. assert scheduler._heap is None
  248. # set initial schedule and check heap is updated
  249. schedule_5 = schedule(5)
  250. scheduler.add(name='test_schedule', schedule=schedule_5)
  251. scheduler.tick()
  252. assert scheduler._heap[0].entry.schedule == schedule_5
  253. # update schedule and check heap is updated
  254. schedule_10 = schedule(10)
  255. scheduler.add(name='test_schedule', schedule=schedule(10))
  256. scheduler.tick()
  257. assert scheduler._heap[0].entry.schedule == schedule_10
  258. def test_schedule_no_remain(self):
  259. scheduler = mScheduler(app=self.app)
  260. scheduler.add(name='test_schedule_no_remain',
  261. schedule=mocked_schedule(False, None))
  262. assert scheduler.tick() == scheduler.max_interval
  263. def test_interface(self):
  264. scheduler = mScheduler(app=self.app)
  265. scheduler.sync()
  266. scheduler.setup_schedule()
  267. scheduler.close()
  268. def test_merge_inplace(self):
  269. a = mScheduler(app=self.app)
  270. b = mScheduler(app=self.app)
  271. a.update_from_dict({'foo': {'schedule': mocked_schedule(True, 10)},
  272. 'bar': {'schedule': mocked_schedule(True, 20)}})
  273. b.update_from_dict({'bar': {'schedule': mocked_schedule(True, 40)},
  274. 'baz': {'schedule': mocked_schedule(True, 10)}})
  275. a.merge_inplace(b.schedule)
  276. assert 'foo' not in a.schedule
  277. assert 'baz' in a.schedule
  278. assert a.schedule['bar'].schedule._next_run_at == 40
  279. @patch('celery.beat.Scheduler._when', return_value=1)
  280. def test_populate_heap(self, _when):
  281. scheduler = mScheduler(app=self.app)
  282. scheduler.update_from_dict(
  283. {'foo': {'schedule': mocked_schedule(True, 10)}}
  284. )
  285. scheduler.populate_heap()
  286. assert scheduler._heap == [event_t(1, 5, scheduler.schedule['foo'])]
  287. def create_schedule_entry(self, schedule=None, args=(), kwargs={},
  288. options={}, task=None):
  289. entry = {
  290. 'name': 'celery.unittest.add',
  291. 'schedule': schedule,
  292. 'app': self.app,
  293. 'args': args,
  294. 'kwargs': kwargs,
  295. 'options': options,
  296. 'task': task
  297. }
  298. return beat.ScheduleEntry(**dict(entry))
  299. def test_schedule_equal_schedule_vs_schedule_success(self):
  300. scheduler = beat.Scheduler(app=self.app)
  301. a = {'a': self.create_schedule_entry(schedule=schedule(5))}
  302. b = {'a': self.create_schedule_entry(schedule=schedule(5))}
  303. assert scheduler.schedules_equal(a, b)
  304. def test_schedule_equal_schedule_vs_schedule_fail(self):
  305. scheduler = beat.Scheduler(app=self.app)
  306. a = {'a': self.create_schedule_entry(schedule=schedule(5))}
  307. b = {'a': self.create_schedule_entry(schedule=schedule(10))}
  308. assert not scheduler.schedules_equal(a, b)
  309. def test_schedule_equal_crontab_vs_crontab_success(self):
  310. scheduler = beat.Scheduler(app=self.app)
  311. a = {'a': self.create_schedule_entry(schedule=crontab(minute=5))}
  312. b = {'a': self.create_schedule_entry(schedule=crontab(minute=5))}
  313. assert scheduler.schedules_equal(a, b)
  314. def test_schedule_equal_crontab_vs_crontab_fail(self):
  315. scheduler = beat.Scheduler(app=self.app)
  316. a = {'a': self.create_schedule_entry(schedule=crontab(minute=5))}
  317. b = {'a': self.create_schedule_entry(schedule=crontab(minute=10))}
  318. assert not scheduler.schedules_equal(a, b)
  319. def test_schedule_equal_crontab_vs_schedule_fail(self):
  320. scheduler = beat.Scheduler(app=self.app)
  321. a = {'a': self.create_schedule_entry(schedule=crontab(minute=5))}
  322. b = {'a': self.create_schedule_entry(schedule=schedule(5))}
  323. assert not scheduler.schedules_equal(a, b)
  324. def test_schedule_equal_different_key_fail(self):
  325. scheduler = beat.Scheduler(app=self.app)
  326. a = {'a': self.create_schedule_entry(schedule=schedule(5))}
  327. b = {'b': self.create_schedule_entry(schedule=schedule(5))}
  328. assert not scheduler.schedules_equal(a, b)
  329. def test_schedule_equal_args_vs_args_success(self):
  330. scheduler = beat.Scheduler(app=self.app)
  331. a = {'a': self.create_schedule_entry(args='a')}
  332. b = {'a': self.create_schedule_entry(args='a')}
  333. assert scheduler.schedules_equal(a, b)
  334. def test_schedule_equal_args_vs_args_fail(self):
  335. scheduler = beat.Scheduler(app=self.app)
  336. a = {'a': self.create_schedule_entry(args='a')}
  337. b = {'a': self.create_schedule_entry(args='b')}
  338. assert not scheduler.schedules_equal(a, b)
  339. def test_schedule_equal_kwargs_vs_kwargs_success(self):
  340. scheduler = beat.Scheduler(app=self.app)
  341. a = {'a': self.create_schedule_entry(kwargs={'a': 'a'})}
  342. b = {'a': self.create_schedule_entry(kwargs={'a': 'a'})}
  343. assert scheduler.schedules_equal(a, b)
  344. def test_schedule_equal_kwargs_vs_kwargs_fail(self):
  345. scheduler = beat.Scheduler(app=self.app)
  346. a = {'a': self.create_schedule_entry(kwargs={'a': 'a'})}
  347. b = {'a': self.create_schedule_entry(kwargs={'b': 'b'})}
  348. assert not scheduler.schedules_equal(a, b)
  349. def test_schedule_equal_options_vs_options_success(self):
  350. scheduler = beat.Scheduler(app=self.app)
  351. a = {'a': self.create_schedule_entry(options={'a': 'a'})}
  352. b = {'a': self.create_schedule_entry(options={'a': 'a'})}
  353. assert scheduler.schedules_equal(a, b)
  354. def test_schedule_equal_options_vs_options_fail(self):
  355. scheduler = beat.Scheduler(app=self.app)
  356. a = {'a': self.create_schedule_entry(options={'a': 'a'})}
  357. b = {'a': self.create_schedule_entry(options={'b': 'b'})}
  358. assert not scheduler.schedules_equal(a, b)
  359. def test_schedule_equal_task_vs_task_success(self):
  360. scheduler = beat.Scheduler(app=self.app)
  361. a = {'a': self.create_schedule_entry(task='a')}
  362. b = {'a': self.create_schedule_entry(task='a')}
  363. assert scheduler.schedules_equal(a, b)
  364. def test_schedule_equal_task_vs_task_fail(self):
  365. scheduler = beat.Scheduler(app=self.app)
  366. a = {'a': self.create_schedule_entry(task='a')}
  367. b = {'a': self.create_schedule_entry(task='b')}
  368. assert not scheduler.schedules_equal(a, b)
  369. def create_persistent_scheduler(shelv=None):
  370. if shelv is None:
  371. shelv = MockShelve()
  372. class MockPersistentScheduler(beat.PersistentScheduler):
  373. sh = shelv
  374. persistence = Bunch(
  375. open=lambda *a, **kw: shelv,
  376. )
  377. tick_raises_exit = False
  378. shutdown_service = None
  379. def tick(self):
  380. if self.tick_raises_exit:
  381. raise SystemExit()
  382. if self.shutdown_service:
  383. self.shutdown_service._is_shutdown.set()
  384. return 0.0
  385. return MockPersistentScheduler, shelv
  386. def create_persistent_scheduler_w_call_logging(shelv=None):
  387. if shelv is None:
  388. shelv = MockShelve()
  389. class MockPersistentScheduler(beat.PersistentScheduler):
  390. sh = shelv
  391. persistence = Bunch(
  392. open=lambda *a, **kw: shelv,
  393. )
  394. def __init__(self, *args, **kwargs):
  395. self.sent = []
  396. beat.PersistentScheduler.__init__(self, *args, **kwargs)
  397. def send_task(self, task=None, args=None, kwargs=None, **options):
  398. self.sent.append({'task': task,
  399. 'args': args,
  400. 'kwargs': kwargs,
  401. 'options': options})
  402. return self.app.AsyncResult(uuid())
  403. return MockPersistentScheduler, shelv
  404. class test_PersistentScheduler:
  405. @patch('os.remove')
  406. def test_remove_db(self, remove):
  407. s = create_persistent_scheduler()[0](app=self.app,
  408. schedule_filename='schedule')
  409. s._remove_db()
  410. remove.assert_has_calls(
  411. [call('schedule' + suffix) for suffix in s.known_suffixes]
  412. )
  413. err = OSError()
  414. err.errno = errno.ENOENT
  415. remove.side_effect = err
  416. s._remove_db()
  417. err.errno = errno.EPERM
  418. with pytest.raises(OSError):
  419. s._remove_db()
  420. def test_setup_schedule(self):
  421. s = create_persistent_scheduler()[0](app=self.app,
  422. schedule_filename='schedule')
  423. opens = s.persistence.open = Mock()
  424. s._remove_db = Mock()
  425. def effect(*args, **kwargs):
  426. if opens.call_count > 1:
  427. return s.sh
  428. raise OSError()
  429. opens.side_effect = effect
  430. s.setup_schedule()
  431. s._remove_db.assert_called_with()
  432. s._store = {str('__version__'): 1}
  433. s.setup_schedule()
  434. s._store.clear = Mock()
  435. op = s.persistence.open = Mock()
  436. op.return_value = s._store
  437. s._store[str('tz')] = 'FUNKY'
  438. s.setup_schedule()
  439. op.assert_called_with(s.schedule_filename, writeback=True)
  440. s._store.clear.assert_called_with()
  441. s._store[str('utc_enabled')] = False
  442. s._store.clear = Mock()
  443. s.setup_schedule()
  444. s._store.clear.assert_called_with()
  445. def test_get_schedule(self):
  446. s = create_persistent_scheduler()[0](
  447. schedule_filename='schedule', app=self.app,
  448. )
  449. s._store = {str('entries'): {}}
  450. s.schedule = {'foo': 'bar'}
  451. assert s.schedule == {'foo': 'bar'}
  452. assert s._store[str('entries')] == s.schedule
  453. def test_run_all_due_tasks_after_restart(self):
  454. scheduler_class, shelve = create_persistent_scheduler_w_call_logging()
  455. shelve['tz'] = 'UTC'
  456. shelve['utc_enabled'] = True
  457. shelve['__version__'] = __version__
  458. cur_seconds = 20
  459. def now_func():
  460. return datetime(2018, 1, 1, 1, 11, cur_seconds)
  461. app_schedule = {
  462. 'first_missed': {'schedule': crontab(
  463. minute='*/10', nowfun=now_func), 'task': 'first_missed'},
  464. 'second_missed': {'schedule': crontab(
  465. minute='*/1', nowfun=now_func), 'task': 'second_missed'},
  466. 'non_missed': {'schedule': crontab(
  467. minute='*/13', nowfun=now_func), 'task': 'non_missed'}
  468. }
  469. shelve['entries'] = {
  470. 'first_missed': beat.ScheduleEntry(
  471. 'first_missed', 'first_missed',
  472. last_run_at=now_func() - timedelta(minutes=2),
  473. total_run_count=10,
  474. schedule=app_schedule['first_missed']['schedule']),
  475. 'second_missed': beat.ScheduleEntry(
  476. 'second_missed', 'second_missed',
  477. last_run_at=now_func() - timedelta(minutes=2),
  478. total_run_count=10,
  479. schedule=app_schedule['second_missed']['schedule']),
  480. 'non_missed': beat.ScheduleEntry(
  481. 'non_missed', 'non_missed',
  482. last_run_at=now_func() - timedelta(minutes=2),
  483. total_run_count=10,
  484. schedule=app_schedule['non_missed']['schedule']),
  485. }
  486. self.app.conf.beat_schedule = app_schedule
  487. scheduler = scheduler_class(self.app)
  488. max_iter_number = 5
  489. for i in range(max_iter_number):
  490. delay = scheduler.tick()
  491. if delay > 0:
  492. break
  493. assert {'first_missed', 'second_missed'} == {
  494. item['task'] for item in scheduler.sent}
  495. # ensure next call on the beginning of next min
  496. assert abs(60 - cur_seconds - delay) < 1
  497. class test_Service:
  498. def get_service(self):
  499. Scheduler, mock_shelve = create_persistent_scheduler()
  500. return beat.Service(app=self.app, scheduler_cls=Scheduler), mock_shelve
  501. def test_pickleable(self):
  502. s = beat.Service(app=self.app, scheduler_cls=Mock)
  503. assert loads(dumps(s))
  504. def test_start(self):
  505. s, sh = self.get_service()
  506. schedule = s.scheduler.schedule
  507. assert isinstance(schedule, dict)
  508. assert isinstance(s.scheduler, beat.Scheduler)
  509. scheduled = list(schedule.keys())
  510. for task_name in keys(sh[str('entries')]):
  511. assert task_name in scheduled
  512. s.sync()
  513. assert sh.closed
  514. assert sh.synced
  515. assert s._is_stopped.isSet()
  516. s.sync()
  517. s.stop(wait=False)
  518. assert s._is_shutdown.isSet()
  519. s.stop(wait=True)
  520. assert s._is_shutdown.isSet()
  521. p = s.scheduler._store
  522. s.scheduler._store = None
  523. try:
  524. s.scheduler.sync()
  525. finally:
  526. s.scheduler._store = p
  527. def test_start_embedded_process(self):
  528. s, sh = self.get_service()
  529. s._is_shutdown.set()
  530. s.start(embedded_process=True)
  531. def test_start_thread(self):
  532. s, sh = self.get_service()
  533. s._is_shutdown.set()
  534. s.start(embedded_process=False)
  535. def test_start_tick_raises_exit_error(self):
  536. s, sh = self.get_service()
  537. s.scheduler.tick_raises_exit = True
  538. s.start()
  539. assert s._is_shutdown.isSet()
  540. def test_start_manages_one_tick_before_shutdown(self):
  541. s, sh = self.get_service()
  542. s.scheduler.shutdown_service = s
  543. s.start()
  544. assert s._is_shutdown.isSet()
  545. class test_EmbeddedService:
  546. @skip.unless_module('_multiprocessing', name='multiprocessing')
  547. def xxx_start_stop_process(self):
  548. from billiard.process import Process
  549. s = beat.EmbeddedService(self.app)
  550. assert isinstance(s, Process)
  551. assert isinstance(s.service, beat.Service)
  552. s.service = MockService()
  553. class _Popen(object):
  554. terminated = False
  555. def terminate(self):
  556. self.terminated = True
  557. with patch('celery.platforms.close_open_fds'):
  558. s.run()
  559. assert s.service.started
  560. s._popen = _Popen()
  561. s.stop()
  562. assert s.service.stopped
  563. assert s._popen.terminated
  564. def test_start_stop_threaded(self):
  565. s = beat.EmbeddedService(self.app, thread=True)
  566. from threading import Thread
  567. assert isinstance(s, Thread)
  568. assert isinstance(s.service, beat.Service)
  569. s.service = MockService()
  570. s.run()
  571. assert s.service.started
  572. s.stop()
  573. assert s.service.stopped
  574. class test_schedule:
  575. def test_maybe_make_aware(self):
  576. x = schedule(10, app=self.app)
  577. x.utc_enabled = True
  578. d = x.maybe_make_aware(datetime.utcnow())
  579. assert d.tzinfo
  580. x.utc_enabled = False
  581. d2 = x.maybe_make_aware(datetime.utcnow())
  582. assert d2.tzinfo
  583. def test_to_local(self):
  584. x = schedule(10, app=self.app)
  585. x.utc_enabled = True
  586. d = x.to_local(datetime.utcnow())
  587. assert d.tzinfo is None
  588. x.utc_enabled = False
  589. d = x.to_local(datetime.utcnow())
  590. assert d.tzinfo