jcass77 / django-apscheduler Goto Github PK
View Code? Open in Web Editor NEWAPScheduler for Django
License: MIT License
APScheduler for Django
License: MIT License
What is the URL of the generated interface?
I'm using this library to help me manage tasks in django admin.
Now I want a new feature that task can be triggered to run after one click of a button. But I have no idea how to implement it. Any advice?
Updated:
I have another 2 table: task, trigger
I add a action in task's admin page, select the action and click 'go' button, the associated function will create a trigger with current time. This new trigger will create a job
table record to the django-apscheduler
, and then this job will be handle by django-apscheduler
.
Hi, one question. I have posted another similar bug, is when MySQL loses connection, but actually there ready to work, DjangoJobStore will try to reconnect, and all works fine.
But now imagine, that the MySQL is really gone. Then, when trying to reconnect, it sees the MySQL is not there and exception. Then, APScheduler will not execute the job, and thus no update to "next_run_date" of the job.
Exception:
WARNING:django_apscheduler:Mysql closed the connection. Perform reconnect...
ERROR:root:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/django_apscheduler/jobstores.py", line 64, in get_due_jobs
out = self._get_jobs(next_run_time__lte=serialize_dt(now))
File "/usr/lib/python2.7/site-packages/django_apscheduler/jobstores.py", line 142, in _get_jobs
for job_id, job_state in job_states:
File "/usr/lib64/python2.7/site-packages/django/db/models/query.py", line 250, in __iter__
self._fetch_all()
File "/usr/lib64/python2.7/site-packages/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/usr/lib64/python2.7/site-packages/django/db/models/query.py", line 122, in __iter__
for row in compiler.results_iter():
File "/usr/lib64/python2.7/site-packages/django/db/models/sql/compiler.py", line 828, in results_iter
results = self.execute_sql(MULTI)
File "/usr/lib64/python2.7/site-packages/django/db/models/sql/compiler.py", line 874, in execute_sql
cursor = self.connection.cursor()
File "/usr/lib64/python2.7/site-packages/django/db/backends/base/base.py", line 254, in cursor
return self._cursor()
File "/usr/lib64/python2.7/site-packages/django/db/backends/base/base.py", line 229, in _cursor
self.ensure_connection()
File "/usr/lib64/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/usr/lib64/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/lib64/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/usr/lib64/python2.7/site-packages/django/db/backends/base/base.py", line 189, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/lib64/python2.7/site-packages/django/db/backends/mysql/base.py", line 274, in get_new_connection
conn = Database.connect(**conn_params)
File "/usr/lib64/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")
Exception in thread APScheduler:
Traceback (most recent call last):
File "/usr/lib64/python2.7/threading.py", line 812, in __bootstrap_inner
self.run()
File "/usr/lib64/python2.7/threading.py", line 765, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/lib64/python2.7/site-packages/apscheduler/schedulers/blocking.py", line 30, in _main_loop
wait_seconds = self._process_jobs()
File "/usr/lib64/python2.7/site-packages/apscheduler/schedulers/base.py", line 945, in _process_jobs
for job in due_jobs:
TypeError: 'NoneType' object is not iterable
Then, after some time, we find the problem and restart MySQL. Now, in the table of django_apscheduler_djangojob
, next_run_time
will be some moment ago, and, missed forever, because it is earlier than now. Then, the job has the state of running
in the APScheduler, the APScheduler is running too, but this job enters a state of "zombie".
I think, we must take this into consideration, right?
I am not saying that it is very possible(the db fails when the machine still runs). It is a very particular corner case.
I followed the instruction in README.md
(locate jobs.py
with settings.py
and urls.py
in my project folder).
When I run python manage.py runserver 0.0.0.0:8001
, the task didn't start!
It is also not registered in admin page, either....
How can I make it run? or registered in admin?
hi I am changing previous issue content as I solved that issue in my project but after solving that issue I am encountering other issue with my project please provide resolution if possible.
Internal Server Error: /jobs/
Traceback (most recent call last):
File "/Users/gauravyadav/Python/env4/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/Users/gauravyadav/Python/env4/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/gauravyadav/Python/env4/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/gauravyadav/Python/env4/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/gauravyadav/Python/env4/lib/python3.6/site-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/gauravyadav/Python/env4/lib/python3.6/site-packages/rest_framework/views.py", line 483, in dispatch
response = self.handle_exception(exc)
File "/Users/gauravyadav/Python/env4/lib/python3.6/site-packages/rest_framework/views.py", line 443, in handle_exception
self.raise_uncaught_exception(exc)
File "/Users/gauravyadav/Python/env4/lib/python3.6/site-packages/rest_framework/views.py", line 480, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/gauravyadav/Python/env4/Scheduler3/distribute_jobs/views.py", line 298, in post
scheduler.start()
File "/Users/gauravyadav/Python/env4/lib/python3.6/site-packages/apscheduler/schedulers/blocking.py", line 19, in start
self._main_loop()
File "/Users/gauravyadav/Python/env4/lib/python3.6/site-packages/apscheduler/schedulers/blocking.py", line 30, in _main_loop
wait_seconds = self._process_jobs()
File "/Users/gauravyadav/Python/env4/lib/python3.6/site-packages/apscheduler/schedulers/base.py", line 987, in _process_jobs
jobstore_next_run_time = jobstore.get_next_run_time()
File "/Users/gauravyadav/Python/env4/lib/python3.6/site-packages/django_apscheduler/jobstores.py", line 29, in inner
return func(*a, **k)
File "/Users/gauravyadav/Python/env4/lib/python3.6/site-packages/django_apscheduler/jobstores.py", line 79, in get_next_run_time
return deserialize_dt(DjangoJob.objects.filter(next_run_time__isnull=False).earliest('next_run_time').next_run_time)
File "/Users/gauravyadav/Python/env4/lib/python3.6/site-packages/django/db/models/query.py", line 597, in earliest
return self._earliest_or_latest(*fields, field_name=field_name)
File "/Users/gauravyadav/Python/env4/lib/python3.6/site-packages/django/db/models/query.py", line 594, in _earliest_or_latest
return obj.get()
File "/Users/gauravyadav/Python/env4/lib/python3.6/site-packages/django/db/models/query.py", line 403, in get
self.model._meta.object_name
django_apscheduler.models.DoesNotExist: DjangoJob matching query does not exist.
[04/Jul/2018 12:19:05] "POST /jobs/ HTTP/1.1" 500 120459
Running into this exception in a caller of remove_job because the job_id does not exist because of which the filter method returns None.
So handling JobLookupError in caller of remove_job doesnt actually report a non-existent job situation.
def remove_job(self, job_id):
deleted, _ = DjangoJob.objects.filter(id=job_id).delete()
if deleted == 0:
raise JobLookupError(job_id)
How to install ?
just like: stackoverflow
django-apscheduler-0.2.13
The runtime timestamps that are displayed for django-apscheduler in the Django admin site are always rendered using the same "%Y-%m-%d %H:%M:%S"
time format.
This can be confusing as date times rendered elsewhere by standard Django uses the format specified in the DATE_TIME setting instead, which defaults to 'N j, Y, P'
.
The current implementation also does not cater for displaying date times in the user's local or preferred format.
As an example, the UCT time 2018-11-19 06:10:00
should be displayed as Nov. 19, 2018, 8:10:00 a.m.
if the user's local time is UCT+2, to align with standard Django.
Using django-apscheduler==0.2.13:
The following error is presented:
AttributeError at /admin/django_apscheduler/djangojob/
'NoneType' object has no attribute 'strftime'
It happens in: lib\site-packages\django_apscheduler\admin.py in next_run_time_sec, line 35
This method:
def next_run_time_sec(self, obj):
return obj.next_run_time.strftime("%Y-%m-%d %H:%M:%S")
when you add a job into scheduler, this job is in a class ,get an error
scheduler.add_job(saltapi.remote_cmd, trigger='interval', start_date=start_datetime, days=days, hours=hours,
minutes=minutes, kwargs={"tgt": target, "fun": module, "arg": arg}, id=job_name)
get an error:
TypeError: remote_cmd() missing 1 required positional argument: 'self'
Hi There, this exception came from django-apschedule and I try many ways like adding DJANGO_SETTINGS_MODULE but not working.
Please let me know if need to do more step to make works.
I used python3.6.2
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django_apscheduler\jobstores.py", line 15, in <module>
from django_apscheduler.models import DjangoJobExecution
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django_apscheduler\models.py", line 8, in <module>
class DjangoJob(models.Model):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django_apscheduler\models.py", line 9, in DjangoJob
name = models.CharField(max_length=255, unique=True)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\fields\__init__.py", line 1061, in __init__
super(CharField, self).__init__(*args, **kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\fields\__init__.py", line 172, in __init__
self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\conf\__init__.py", line 56, in __getattr__
self._setup(name)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\conf\__init__.py", line 39, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Similar: https://code.djangoproject.com/ticket/22420
Using the DjangoJobStore causes TestCase runs to fail with the following error:
django.db.utils.OperationalError: database "<name>" is being accessed by other users
DETAIL: There is 1 other session using the database.
I've tried the decorator suggested in that thread for closing all database connections, but the error still remains unless I use a different job storage. Using the current versions of all related libraries.
in my project,i use django 2.0.1 and django-apscheduler (0.2.3),i register one job by interval,but in certain time it execute more once,also use apscheduler without django-apscheduler,it work very well
Hi jarekwg,
When i try to follow the steps test the code encounter this issue, I am using python 2.7:
Can you help me take a look?
In [7]: scheduler.add_jobstore(DjangoJobStore(), 'djangojobstore')
TypeError Traceback (most recent call last)
in ()
----> 1 scheduler.add_jobstore(DjangoJobStore(), 'djangojobstore')
/home/sunjiali/workspace1/Timer/Timer/django_apscheduler/jobstores.pyc in init(self, pickle_protocol)
17
18 def init(self, pickle_protocol=pickle.HIGHEST_PROTOCOL):
---> 19 super().init()
20 self.pickle_protocol = pickle_protocol
21
TypeError: super() takes at least 1 argument (0 given)
In [8]:
Hello author, how can I call up to multi-threaded scheduled tasks by django?
Using the loop timed task,the mysql default connection time is set to 25 hours.But sometimes there will still be a mysql connection error。Perhaps the solution is to use a method similar to mysql_ping() to do a check before each data entry, wake up the connection, what should I do?
Details of my error:
2019-03-18 08:00:00 base.run_job: Running job "get_goods (trigger: cron[hour='8', minute='0', second='0'], next run at: 2019-03-18 08:00:00 CST)" (scheduled at 2019-03-18 08:00:00+08:00)
2019-03-18 08:00:00 get_goods: ***********************************mytask start
2019-03-18 08:00:00 base.run_job: Job "get_goods (trigger: cron[hour='8', minute='0', second='0'], next run at: 2019-03-19 08:00:00 CST)" raised an exception
Traceback (most recent call last):
......
django.db.utils.OperationalError: (2006, 'MySQL server has gone away')
whether I use AsyncIOScheduler or BackgroundScheduler, when I run my django server, it just raise exception.
a short pic:
a long text info :
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0376C3D8> Traceback (most recent call last): File "C:\Users\Administrator\.virtualenvs\justjoke-0zIsqtwi\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper fn(*args, **kwargs) File "C:\Users\Administrator\.virtualenvs\justjoke-0zIsqtwi\lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run self.check(display_num_errors=True) File "C:\Users\Administrator\.virtualenvs\justjoke-0zIsqtwi\lib\site-packages\django\core\management\base.py", line 359, in check include_deployment_checks=include_deployment_checks, File "C:\Users\Administrator\.virtualenvs\justjoke-0zIsqtwi\lib\site-packages\django\core\management\base.py", line 346, in _run_checks return checks.run_checks(**kwargs) File "C:\Users\Administrator\.virtualenvs\justjoke-0zIsqtwi\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks new_errors = check(app_configs=app_configs) File "C:\Users\Administrator\.virtualenvs\justjoke-0zIsqtwi\lib\site-packages\django\core\checks\urls.py", line 16, in check_url_config return check_resolver(resolver) File "C:\Users\Administrator\.virtualenvs\justjoke-0zIsqtwi\lib\site-packages\django\core\checks\urls.py", line 26, in check_resolver return check_method() File "C:\Users\Administrator\.virtualenvs\justjoke-0zIsqtwi\lib\site-packages\django\urls\resolvers.py", line 254, in check for pattern in self.url_patterns: File "C:\Users\Administrator\.virtualenvs\justjoke-0zIsqtwi\lib\site-packages\django\utils\functional.py", line 35, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "C:\Users\Administrator\.virtualenvs\justjoke-0zIsqtwi\lib\site-packages\django\urls\resolvers.py", line 405, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "C:\Users\Administrator\.virtualenvs\justjoke-0zIsqtwi\lib\site-packages\django\utils\functional.py", line 35, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "C:\Users\Administrator\.virtualenvs\justjoke-0zIsqtwi\lib\site-packages\django\urls\resolvers.py", line 398, in urlconf_module return import_module(self.urlconf_name) File "C:\Users\Administrator\.virtualenvs\justjoke-0zIsqtwi\lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 986, in _gcd_import File "<frozen importlib._bootstrap>", line 969, in _find_and_load File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 673, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 673, in exec_module File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed File "F:\works\study\WorkSpace\justjoke\AJoke\AJoke\urls.py", line 48, in <module> import records.interfaceprocess File "F:\works\study\WorkSpace\justjoke\AJoke\records\interfaceprocess.py", line 76, in <module> main() File "F:\works\study\WorkSpace\justjoke\AJoke\records\interfaceprocess.py", line 71, in main loop = asyncio.get_event_loop() File "C:\python35\Lib\asyncio\events.py", line 671, in get_event_loop return get_event_loop_policy().get_event_loop() File "C:\python35\Lib\asyncio\events.py", line 583, in get_event_loop % threading.current_thread().name) RuntimeError: There is no current event loop in thread 'Dummy-1'.
I want to start apscheduler when django start
I met a bug
when code below
from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events
# Create your tests here.
class Task(object):
desc = 'Task'
def task_start(self, name):
print(self.desc, name)
def run():
task = Task()
scheduler = BackgroundScheduler()
# scheduler.add_jobstore(DjangoJobStore(), "default")
register_events(scheduler)
scheduler.start()
scheduler.add_job(task.task_start, 'date', run_date=dt.now(), args=['test'])
the result is
Task test
Job with id d1c2578c36e64cf2bf84542bf9d2984d not found in database
Job with id d1c2578c36e64cf2bf84542bf9d2984d not found in database
But after add DjangoJobStorae
from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events
# Create your tests here.
class Task(object):
desc = 'Task'
def task_start(self, name):
print(self.desc, name)
def run():
task = Task()
scheduler = BackgroundScheduler()
scheduler.add_jobstore(DjangoJobStore(), "default")
register_events(scheduler)
scheduler.start()
scheduler.add_job(task.task_start, 'date', run_date=dt.now(), args=['test'])
the result is
Job "Task.task_start (trigger: date[2018-07-09 10:16:58 CST], next run at: 2018-07-09 10:16:58 CST)" raised an exception
Traceback (most recent call last):
File "D:\Anaconda3\envs\clearserver\lib\site-packages\apscheduler\executors\base.py", line 125, in run_job
retval = job.func(*job.args, **job.kwargs)
TypeError: task_start() missing 1 required positional argument: 'name'
Job with id 8e0a8b2dde5942028d88cb93f3b504f2 not found in database
Job with id 8e0a8b2dde5942028d88cb93f3b504f2 not found in database
What's question? Thx
@jarekwg
@register_job(scheduler, "interval", days=1)
What should we be providing in the decorator so as to achieve this task?
Looks pip isn't seeing your latest changes to the code (I did a "pip install django_apscheduler" and it didn't come with the register_job function. Might be a gotcha for other coders.
env:
Django==1.8.17
django-apscheduler==0.2.7
code:
job_id=test
scheduler.add_job(_execute_job, 'date', run_date=run_date, args=[workflowId, url], id=job_id,
replace_existing=True)
scheduler.remove_job(job_id)
[2018-04-28 21:44:50,884][Thread-22:123145460006912][task_id:default][exception_logging_middleware.py:10] [ERROR]- Traceback (most recent call last):
File "/Users/lihuanhuan/PycharmProjects/venv4archer/lib/python3.6/site-packages/django/core/handlers/base.py", line 132, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/lihuanhuan/PycharmProjects/archer-github/sql/views.py", line 410, in cancel
del_sqlcronjob(job_id)
File "/Users/lihuanhuan/PycharmProjects/archer-github/sql/jobs.py", line 53, in del_sqlcronjob
return scheduler.remove_job(job_id)
File "/Users/lihuanhuan/PycharmProjects/venv4archer/lib/python3.6/site-packages/apscheduler/schedulers/base.py", line 608, in remove_job
store.remove_job(job_id)
File "/Users/lihuanhuan/PycharmProjects/venv4archer/lib/python3.6/site-packages/django_apscheduler/jobstores.py", line 27, in inner
return func(*a, **k)
File "/Users/lihuanhuan/PycharmProjects/venv4archer/lib/python3.6/site-packages/django_apscheduler/jobstores.py", line 117, in remove_job
deleted, _ = DjangoJob.objects.filter(name=job_id).delete()
TypeError: 'NoneType' object is not iterable
@ignore_database_error
def remove_job(self, job_id):
deleted, _ = DjangoJob.objects.filter(name=job_id).delete()
if deleted == 0:
raise JobLookupError(job_id)
what is this requirement?
-e [email protected]:sallyruthstruik/django-apscheduler.git@9cb356e#egg=django_apscheduler
I do not fully get what the line in requremnts_dev.txt trying to do
File "/home/a-askerka/Projects/offerwall/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/a-askerka/Projects/offerwall/venv/lib/python3.6/site-packages/django_apscheduler/models.py", line 17, in get_queryset
self.__ping()
File "/home/a-askerka/Projects/offerwall/venv/lib/python3.6/site-packages/django_apscheduler/models.py", line 28, in __ping
self.__reconnect()
File "/home/a-askerka/Projects/offerwall/venv/lib/python3.6/site-packages/django_apscheduler/models.py", line 34, in __reconnect
connection.connection.close()
AttributeError: 'NoneType' object has no attribute 'close'
django==2.0.4
django-apscheduler==0.2.6
python==3.6
I'm tried collect static without any db connection.
File "./task_execute_monitor/jobs.py", line 30, in do_mon_task
scheduler.remove_job(job_id)
File "/home/env/sentry/local/lib/python2.7/site-packages/apscheduler/schedulers/base.py", line 608, in remove_job
store.remove_job(job_id)
File "/home/env/sentry/local/lib/python2.7/site-packages/django_apscheduler/jobstores.py", line 27, in inner
return func(*a, **k)
File "/home/env/sentry/local/lib/python2.7/site-packages/django_apscheduler/jobstores.py", line 117, in remove_job
deleted, _ = DjangoJob.objects.filter(name=job_id).delete()
TypeError: 'NoneType' object is not iterable
Though a job that is created after scheduler.start()
is scheduled, it is not added to the DjangoJobStore on the database.
Seeing this in case where the job has no triggers so it can be run on demand.
Hey ,, I am not sure how to send you DM.
but I can help you out. Let me know
code:
jobstores = {
'default': DjangoJobStore()
}
scheduler = BackgroundScheduler(jobstores=jobstores, timezone=timezone('Asia/Shanghai'))
scheduler.start()
class CommandView(BaseView):
def post(self, request):
@register_job(scheduler, "interval", seconds=10)
def test_job():
print('123')
register_events(scheduler)
return self.base_json_response({'1': 123})
log:
Traceback (most recent call last):
File "/work/apps/task_execute/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/work/apps/task_execute/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/work/apps/task_execute/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/work/apps/task_execute/venv/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/work/apps/task_execute/venv/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/work/apps/task_execute/schedule/views.py", line 27, in post
@register_job(scheduler, "interval", seconds=10)
File "/work/apps/task_execute/venv/lib/python3.6/site-packages/django_apscheduler/jobstores.py", line 247, in inner
scheduler.add_job(func, *a, **k)
File "/work/apps/task_execute/venv/lib/python3.6/site-packages/apscheduler/schedulers/base.py", line 439, in add_job
self._real_add_job(job, jobstore, replace_existing)
File "/work/apps/task_execute/venv/lib/python3.6/site-packages/apscheduler/schedulers/base.py", line 864, in _real_add_job
store.add_job(job)
File "/work/apps/task_execute/venv/lib/python3.6/site-packages/django_apscheduler/jobstores.py", line 29, in inner
return func(*a, **k)
File "/work/apps/task_execute/venv/lib/python3.6/site-packages/django_apscheduler/jobstores.py", line 95, in add_job
job_state=pickle.dumps(job.getstate(), self.pickle_protocol)
File "/work/apps/task_execute/venv/lib/python3.6/site-packages/apscheduler/job.py", line 237, in getstate
'instead.' % (self.func,))
ValueError: This Job cannot be serialized since the reference to its callable (<function CommandView.post..test_job at 0x7f2491ef2d08>) could not be determined. Consider giving a textual reference (module:function name) instead.
Hi jarekwg,
I use django-apscheduler
in my Django
project with uWSGI
. When I try use the Mule option, the scheduler just do not work.
Please check it, Thanks.
uwsgi.ini
......
# use mule
mule = cron_jobs.py
cron_jobs.py
Workable codes, which can print Before
and After
and the job log
print("Before")
scheduler = BackgroundScheduler()
# scheduler.add_jobstore(DjangoJobStore(), "default") <--------
register_events(scheduler)
scheduler.add_job(solve__job, 'interval', seconds=10)
scheduler.start()
print("After")
Non-workable codes, which just print Before
twice and do nothing
print("Before")
scheduler = BackgroundScheduler()
scheduler.add_jobstore(DjangoJobStore(), "default") <--------
register_events(scheduler)
scheduler.add_job(solve__job, 'interval', seconds=10)
scheduler.start()
print("After")
Hi,
Didn't find your contacts so raised an issue just to let you know that I am open to help you out.
Code:
import time
from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job
scheduler = BackgroundScheduler()
scheduler.add_jobstore(DjangoJobStore(), "default")
def test_job(text):
print(text)
scheduler.add_job(test_job, 'cron', year=2019, month=3, day=18, hour=18, minute=6, second=50, args=['some text'])
register_events(scheduler)
scheduler.start()
print("Scheduler started!")
Returns the following:
Internal Server Error: /admin/django_apscheduler/djangojob/
Traceback (most recent call last):
File "...site-packages/django/db/models/options.py", line 564, in get_field
return self.fields_map[field_name]
KeyError: 'next_run_time_sec'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "...site-packages/django/contrib/admin/utils.py", line 278, in lookup_field
f = _get_non_gfk_field(opts, name)
File "...site-packages/django/contrib/admin/utils.py", line 309, in _get_non_gfk_field
field = opts.get_field(name)
File "...site-packages/django/db/models/options.py", line 566, in get_field
raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: DjangoJob has no field named 'next_run_time_sec'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "...site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "...site-packages/django/core/handlers/base.py", line 156, in _get_response
response = self.process_exception_by_middleware(e, request)
File "...site-packages/django/core/handlers/base.py", line 154, in _get_response
response = response.render()
File "...site-packages/django/template/response.py", line 106, in render
self.content = self.rendered_content
File "...site-packages/django/template/response.py", line 83, in rendered_content
content = template.render(context, self._request)
File "...site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "...site-packages/django/template/base.py", line 171, in render
return self._render(context)
File "...site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "...site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "...site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "...site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "...site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "...site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "...site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "...site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "...site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "...site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "...site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "...site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "...site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "...site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "...site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "...site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "...site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "...site-packages/django/contrib/admin/templatetags/base.py", line 33, in render
return super().render(context)
File "...site-packages/django/template/library.py", line 214, in render
_dict = self.func(*resolved_args, **resolved_kwargs)
File "...site-packages/django/contrib/admin/templatetags/admin_list.py", line 341, in result_list
'results': list(results(cl))}
File "...site-packages/django/contrib/admin/templatetags/admin_list.py", line 318, in results
yield ResultList(None, items_for_result(cl, res, None))
File "...site-packages/django/contrib/admin/templatetags/admin_list.py", line 309, in __init__
super().__init__(*items)
File "...site-packages/django/contrib/admin/templatetags/admin_list.py", line 228, in items_for_result
f, attr, value = lookup_field(field_name, result, cl.model_admin)
File "...site-packages/django/contrib/admin/utils.py", line 287, in lookup_field
value = attr(obj)
File "...site-packages/django_apscheduler/admin.py", line 35, in next_run_time_sec
return obj.next_run_time.strftime("%Y-%m-%d %H:%M:%S")
AttributeError: 'NoneType' object has no attribute 'strftime'
I fixed the issue by adding the following lines to django_apscheduler/admin.py
def next_run_time_sec(self, obj):
try:
return obj.next_run_time.strftime("%Y-%m-%d %H:%M:%S")
except AttributeError:
return None
I'm happy to submit a PR with a more suitable fix
It seems the interval is ignored. No matter what I put in there, the job is executed every second. Not, sure whether that is the purpose of 'seconds' cause it is not explained in the readme.
import time
from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job
scheduler = BackgroundScheduler()
scheduler.add_jobstore(DjangoJobStore(), 'djangojobstore')
@register_job(scheduler, "interval", minutes=60, id="job1")
def test_job():
#time.sleep(10)
print("I'm the first test job!")
# raise ValueError("Olala!")
register_events(scheduler)
scheduler.start()
print("Scheduler started!")
Hi jarekwg,
I just using apscheduler for develop a timer, but when I trying test looks the task do not excute.
For example I put scheduler.start() in views.py, its transience worked. My question is how to let the scheduler.start() always work until django project down?
Thanks for your help?
What is the difference between these two?
I run it in urls.py
I also run it using the commands below.
class Command(BaseCommand):
"""
Management command for APScheduler
"""
def handle(self, *args, **kwargs):
from scheduler import jobs
It registers well with django_apscheduler_djangojob but does not run. How do I resolve this?
(id : rank)
Met the error when using pause_job()
and setting USE_TZ=False
.
Can you fix it ? @jarekwg
When there is only one task and it is paused, my project raise 'DjangoJob matching query does not exist.'
May be the problem is here:
Now the next_run_time for all jobs is Null, there are several places where next_run_time are needed.
The version is 0.2.12,thanx for help
I have seen your example, your job is very useful and good. I found that you have to import example_apscheduler.jobs in urls.py, but it seems to be not aesthetical, how could I initial and start the example_apscheduler.jobs.py
or scheduler = BackgroundScheduler()
in the setting.py.
django_apscheduler/result_storage.py line 32 :
job_execution.duration = float(job_execution.finished) - float(job_execution.started)
job_execution.started or job_execution.finished is possible to be empty,so,float will raise except.
You can simply change to:
try: job_execution.duration = float(job_execution.finished) - float(job_execution.started) except: job_execution.duration = None
django_apscheduler/models.py line 40:
next_run_time = models.DateTimeField(db_index=True)
Fields should not be set as required, job.pause() will not work
You can simply change to:
next_run_time = models.DateTimeField(db_index=True,blank=True,null=True)
I find that if I keep Apscheduler busy, i.e., keep adding jobs to it, it will stay happy and active. But, if I leave it without job for a time, say, 12 hours or more, and add job again, even it is running (scheduler.state
returns 1
, which is RUNNING
, when I consult with a web service), it accepts job, but does not execute it, when the run_date
arrives. I have configured django-apscheduler
as the jobstore.
I am using APScheduler along with Apache and Django.
Apache/2.4.6 (CentOS) PHP/5.4.16 mod_wsgi/3.4 Python/2.7.5
APScheduler (3.4.0)
django-apscheduler (0.2.3)
Yesterday, at 10:00 AM, I plannified three jobs, job A at 23:30 yesterday, job B at 09:00 today, and job C 09:50 today, and after that, I left the server there, no more jobs from yesterday from 1000 to 2330.
Checking the logs of yesterday, at 2330, this error happened:
[Wed Jan 31 23:30:00.010202 2018] [:error] [pid 17924] DEBUG:apscheduler.scheduler:Looking for jobs to run
[Wed Jan 31 23:30:00.019000 2018] [:error] [pid 17924] ERROR:root:
[Wed Jan 31 23:30:00.019013 2018] [:error] [pid 17924] Traceback (most recent call last):
[Wed Jan 31 23:30:00.019031 2018] [:error] [pid 17924] File "/usr/lib/python2.7/site-packages/django_apscheduler/jobstores.py", line 40, in get_due_jobs
[Wed Jan 31 23:30:00.019034 2018] [:error] [pid 17924] return self._get_jobs(next_run_time__lte=serialize_dt(now))
[Wed Jan 31 23:30:00.019036 2018] [:error] [pid 17924] File "/usr/lib/python2.7/site-packages/django_apscheduler/jobstores.py", line 100, in _get_jobs
[Wed Jan 31 23:30:00.019038 2018] [:error] [pid 17924] for job_id, job_state in job_states:
[Wed Jan 31 23:30:00.019040 2018] [:error] [pid 17924] File "/usr/lib64/python2.7/site-packages/django/db/models/query.py", line 250, in __iter__
[Wed Jan 31 23:30:00.019042 2018] [:error] [pid 17924] self._fetch_all()
[Wed Jan 31 23:30:00.019044 2018] [:error] [pid 17924] File "/usr/lib64/python2.7/site-packages/django/db/models/query.py", line 1118, in _fetch_all
[Wed Jan 31 23:30:00.019046 2018] [:error] [pid 17924] self._result_cache = list(self._iterable_class(self))
[Wed Jan 31 23:30:00.019048 2018] [:error] [pid 17924] File "/usr/lib64/python2.7/site-packages/django/db/models/query.py", line 122, in __iter__
[Wed Jan 31 23:30:00.019050 2018] [:error] [pid 17924] for row in compiler.results_iter():
[Wed Jan 31 23:30:00.019052 2018] [:error] [pid 17924] File "/usr/lib64/python2.7/site-packages/django/db/models/sql/compiler.py", line 828, in results_iter
[Wed Jan 31 23:30:00.019054 2018] [:error] [pid 17924] results = self.execute_sql(MULTI)
[Wed Jan 31 23:30:00.019056 2018] [:error] [pid 17924] File "/usr/lib64/python2.7/site-packages/django/db/models/sql/compiler.py", line 886, in execute_sql
[Wed Jan 31 23:30:00.019058 2018] [:error] [pid 17924] raise original_exception
[Wed Jan 31 23:30:00.019060 2018] [:error] [pid 17924] OperationalError: (2006, 'MySQL server has gone away')
[Wed Jan 31 23:30:00.020039 2018] [:error] [pid 17924] Exception in thread APScheduler:
[Wed Jan 31 23:30:00.020050 2018] [:error] [pid 17924] Traceback (most recent call last):
[Wed Jan 31 23:30:00.020053 2018] [:error] [pid 17924] File "/usr/lib64/python2.7/threading.py", line 811, in __bootstrap_inner
[Wed Jan 31 23:30:00.020055 2018] [:error] [pid 17924] self.run()
[Wed Jan 31 23:30:00.020057 2018] [:error] [pid 17924] File "/usr/lib64/python2.7/threading.py", line 764, in run
[Wed Jan 31 23:30:00.020059 2018] [:error] [pid 17924] self.__target(*self.__args, **self.__kwargs)
[Wed Jan 31 23:30:00.020061 2018] [:error] [pid 17924] File "/usr/lib64/python2.7/site-packages/apscheduler/schedulers/blocking.py", line 30, in _main_loop
[Wed Jan 31 23:30:00.020063 2018] [:error] [pid 17924] wait_seconds = self._process_jobs()
[Wed Jan 31 23:30:00.020065 2018] [:error] [pid 17924] File "/usr/lib64/python2.7/site-packages/apscheduler/schedulers/base.py", line 943, in _process_jobs
[Wed Jan 31 23:30:00.020067 2018] [:error] [pid 17924] for job in due_jobs:
[Wed Jan 31 23:30:00.020069 2018] [:error] [pid 17924] TypeError: 'NoneType' object is not iterable
[Wed Jan 31 23:30:00.020071 2018] [:error] [pid 17924]
And then, at 0900 and 0950, this error does not appear in log, and jobs not executed.
I think django-apscheduler should wake the connection to DB before it is going to execute the job, because at that moment, we cannot be sure if the connection is alive or not.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.