test_beat.py 19 KB

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