当前位置: 首页>>代码示例>>Python>>正文


Python Job.compute_next_run_time方法代码示例

本文整理汇总了Python中apscheduler.job.Job.compute_next_run_time方法的典型用法代码示例。如果您正苦于以下问题:Python Job.compute_next_run_time方法的具体用法?Python Job.compute_next_run_time怎么用?Python Job.compute_next_run_time使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在apscheduler.job.Job的用法示例。


在下文中一共展示了Job.compute_next_run_time方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestJob

# 需要导入模块: from apscheduler.job import Job [as 别名]
# 或者: from apscheduler.job.Job import compute_next_run_time [as 别名]
class TestJob(object):
    RUNTIME = datetime(2010, 12, 13, 0, 8, 0)

    def setup(self):
        self.trigger = SimpleTrigger(self.RUNTIME)
        self.job = Job(self.trigger, dummyfunc, [], {}, 1, False)

    def test_compute_next_run_time(self):
        self.job.compute_next_run_time(self.RUNTIME - timedelta(microseconds=1))
        eq_(self.job.next_run_time, self.RUNTIME)

        self.job.compute_next_run_time(self.RUNTIME)
        eq_(self.job.next_run_time, self.RUNTIME)

        self.job.compute_next_run_time(self.RUNTIME + timedelta(microseconds=1))
        eq_(self.job.next_run_time, None)

    def test_compute_run_times(self):
        expected_times = [self.RUNTIME + timedelta(seconds=1),
                          self.RUNTIME + timedelta(seconds=2)]
        self.job.trigger = IntervalTrigger(timedelta(seconds=1), self.RUNTIME)
        self.job.compute_next_run_time(expected_times[0])
        eq_(self.job.next_run_time, expected_times[0])

        run_times = self.job.get_run_times(self.RUNTIME)
        eq_(run_times, [])

        run_times = self.job.get_run_times(expected_times[0])
        eq_(run_times, [expected_times[0]])

        run_times = self.job.get_run_times(expected_times[1])
        eq_(run_times, expected_times)

    def test_max_runs(self):
        self.job.max_runs = 1
        self.job.runs += 1
        self.job.compute_next_run_time(self.RUNTIME)
        eq_(self.job.next_run_time, None)

    def test_eq_num(self):
        # Just increasing coverage here
        assert not self.job == 'dummyfunc'

    def test_getstate(self):
        state = self.job.__getstate__()
        eq_(state, dict(trigger=self.trigger,
                        func_ref='testjob:dummyfunc',
                        name='dummyfunc', args=[],
                        kwargs={}, misfire_grace_time=1,
                        coalesce=False, max_runs=None,
                        max_instances=1, runs=0))

    def test_setstate(self):
        trigger = SimpleTrigger('2010-12-14 13:05:00')
        state = dict(trigger=trigger, name='testjob.dummyfunc',
                     func_ref='testjob:dummyfunc',
                     args=[], kwargs={}, misfire_grace_time=2, max_runs=2,
                     coalesce=True, max_instances=2, runs=1)
        self.job.__setstate__(state)
        eq_(self.job.trigger, trigger)
        eq_(self.job.func, dummyfunc)
        eq_(self.job.max_runs, 2)
        eq_(self.job.coalesce, True)
        eq_(self.job.max_instances, 2)
        eq_(self.job.runs, 1)
        assert not hasattr(self.job, 'func_ref')
        assert isinstance(self.job._lock, lock_type)

    def test_jobs_equal(self):
        assert self.job == self.job

        job2 = Job(SimpleTrigger(self.RUNTIME), lambda: None, [], {}, 1, False)
        assert self.job != job2

        job2.id = self.job.id = 123
        eq_(self.job, job2)

        assert self.job != 'bleh'

    def test_instances(self):
        self.job.max_instances = 2
        eq_(self.job.instances, 0)

        self.job.add_instance()
        eq_(self.job.instances, 1)

        self.job.add_instance()
        eq_(self.job.instances, 2)

        assert_raises(MaxInstancesReachedError, self.job.add_instance)

        self.job.remove_instance()
        eq_(self.job.instances, 1)

        self.job.remove_instance()
        eq_(self.job.instances, 0)

        assert_raises(AssertionError, self.job.remove_instance)

    def test_repr(self):
#.........这里部分代码省略.........
开发者ID:Pythonz,项目名称:PIRB,代码行数:103,代码来源:testjob.py

示例2: JobStoreTest

# 需要导入模块: from apscheduler.job import Job [as 别名]
# 或者: from apscheduler.job.Job import compute_next_run_time [as 别名]
class JobStoreTest(TestCase):
    def setUp(self):
        self.redis = StrictRedis(**settings.REDISES['default'])
        self.store = JobStore(self.redis, 'Asia/Chongqing')
        self.store.clear()
        self.redis.delete('active_jobs_pool')
        self.init()

    def tearDown(self):
        self.redis.delete('active_jobs_pool')
        self.store.clear()

    def init(self):
        tz = self.store.timezone
        trigger = IntervalTrigger({}, minutes=5, timezone=tz)
        url     = 'http://t.cn'
        now = datetime.now(tz)

        self.job1 = Job(1, trigger, url)
        self.job1.compute_next_run_time(now)
        self.job2 = Job(2, trigger, url)
        self.job2.compute_next_run_time(now)

        self.store.add_job(self.job1)
        self.store.add_job(self.job2)


        self.now = now
        self.url = url
        self.trigger = trigger

    def testLoadJob(self):
        Task.objects.all().delete()
        task = self.createTask()
        task.save()
        job = self.store.load_job(task.id, self.now)
        self.assertEqual(self.url, job.func)

    def testLoadJobs(self):
        Task.objects.all().delete()
        self.store.clear()

        for i in range(3):
            task = self.createTask()
            task.save()
            self.redis.sadd('active_jobs_pool', task.id)

        self.assertEqual(3, self.store.count())
        self.assertEqual(0, len(self.store.jobs))
        self.store.load_jobs()
        self.assertEqual(3, len(self.store.jobs))


    def testAddJob(self):
        self.assertEqual(2, self.store.count())
        job = Job(3, self.trigger, self.url)
        job.compute_next_run_time(self.now)
        self.store.add_job(job)
        self.assertEqual(3, self.store.count())

    def testRemoveJob(self):
        self.assertEqual(2, self.store.count())
        self.store.remove_job(id=1)
        self.assertEqual(1, self.store.count())
        self.store.remove_job(id=3)
        self.assertEqual(1, self.store.count())

    def createTask(self):
        import json
        task = Task()
        task.run_time = json.dumps({'minutes':10})
        task.name = 'name'
        task.run_entry = self.url
        return task

    def testUpdateJob(self):
        task = self.createTask()
        task.name = 'name1'
        task.save()
        self.store.clear()
        self.redis.sadd(self.store.active_jobs_key, task.id)
        self.store.load_jobs()
        job = self.store.find_job(task.id)
        self.assertEqual(task.name, job.name)

        task.name = 'name2'
        task.save()
        self.store.update_job(id=task.id)
        job = self.store.find_job(task.id)
        self.assertEqual(task.name, job.name)



    def testFindJob(self):
        self.assertEqual(2, self.store.count())
        job = self.store.find_job(1)
        self.assertEqual(1, job.id)
        self.assertIsNone(self.store.find_job(3))

    def testHasJob(self):
#.........这里部分代码省略.........
开发者ID:reedboat,项目名称:dcron,代码行数:103,代码来源:tests.py

示例3: testAddJob

# 需要导入模块: from apscheduler.job import Job [as 别名]
# 或者: from apscheduler.job.Job import compute_next_run_time [as 别名]
 def testAddJob(self):
     self.assertEqual(2, self.store.count())
     job = Job(3, self.trigger, self.url)
     job.compute_next_run_time(self.now)
     self.store.add_job(job)
     self.assertEqual(3, self.store.count())
开发者ID:reedboat,项目名称:dcron,代码行数:8,代码来源:tests.py

示例4: HotQueue

# 需要导入模块: from apscheduler.job import Job [as 别名]
# 或者: from apscheduler.job.Job import compute_next_run_time [as 别名]
from apscheduler.jobstores.sqlalchemy_store import SQLAlchemyJobStore
from dateutil.tz import gettz
from datetime import datetime, timedelta
from apscheduler.triggers import IntervalTrigger, DateTrigger
from apscheduler.scripts import HttpScript, CommandScript


if __name__ == '__main__':

    queue = HotQueue('job_changes')
    script = HttpScript(url='http://baidu.comm')
    store = SQLAlchemyJobStore(url='sqlite:////tmp/task.db', tablename='tasks')
    #script = CommandScript(command='ping -c 3 www.baidu.com')
    local_tz = gettz('Asia/Chongqing')
    defaults = {'timezone': local_tz}
    trigger = IntervalTrigger(defaults, seconds=60)
    #trigger = DateTrigger(defaults, run_date=datetime(2013,12,11, 8, 11))

    job = Job(name=u'BaiduCurlWithWrongUrl', script=script, trigger=trigger)

    #print job.run()
    now = datetime.now(local_tz)
    next_run_time = job.compute_next_run_time(now)
    print job.get_run_times(now+timedelta(seconds=60))
    
    if next_run_time:
        print "add job"
        print job
        store.add_job(job)
        queue.put({'opt_type':'add', 'job_id':job.id})
开发者ID:reedboat,项目名称:dcron,代码行数:32,代码来源:newtask.py


注:本文中的apscheduler.job.Job.compute_next_run_time方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。