Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/shceduler/job Error:Object of type APScheduler is not JSON serializable #141

Open
RPJack opened this issue Feb 24, 2021 · 7 comments
Open

Comments

@RPJack
Copy link

RPJack commented Feb 24, 2021

When i use the demo from basic example,it worked.
But when i add one job with the args which the initialize scheduler,like this:
@scheduler.task('cron', id='refresh_job', minute='*/1',args=[scheduler]) def refresh_job(scheduler): jobs = scheduler.get_jobs() print(jobs)
the job worked,and when i use Postman,i got an Error
image
this is Error Message in server
[2021-02-24 19:09:51,219] ERROR in app: Exception on /scheduler/jobs [GET] Traceback (most recent call last): File "/Users/goshoho/anaconda3/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "/Users/goshoho/anaconda3/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "/Users/goshoho/anaconda3/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "/Users/goshoho/anaconda3/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise raise value File "/Users/goshoho/anaconda3/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "/Users/goshoho/anaconda3/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/Users/goshoho/anaconda3/lib/python3.8/site-packages/flask_apscheduler/scheduler.py", line 373, in decorated return view_func(*args, **kwargs) File "/Users/goshoho/anaconda3/lib/python3.8/site-packages/flask_apscheduler/api.py", line 82, in get_jobs return jsonify(job_states) File "/Users/goshoho/anaconda3/lib/python3.8/site-packages/flask_apscheduler/json.py", line 23, in jsonify return flask.current_app.response_class(dumps(data, indent=indent), status=status, mimetype='application/json') File "/Users/goshoho/anaconda3/lib/python3.8/site-packages/flask_apscheduler/json.py", line 16, in dumps return json.dumps(obj, indent=indent, cls=JSONEncoder) File "/Users/goshoho/anaconda3/lib/python3.8/json/__init__.py", line 234, in dumps return cls( File "/Users/goshoho/anaconda3/lib/python3.8/json/encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "/Users/goshoho/anaconda3/lib/python3.8/json/encoder.py", line 257, in iterencode return _iterencode(o, 0) File "/Users/goshoho/anaconda3/lib/python3.8/site-packages/flask_apscheduler/json.py", line 34, in default return super(JSONEncoder, self).default(obj) File "/Users/goshoho/anaconda3/lib/python3.8/json/encoder.py", line 179, in default raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type APScheduler is not JSON serializable 127.0.0.1 - - [24/Feb/2021 19:09:51] "GET /scheduler/jobs HTTP/1.1" 500 -

May be the sheduler can't be serializable?
i just want cyclical refresh my job list with timing,if there have another solution,it's also grate for me!

@np3xpu
Copy link

np3xpu commented Feb 25, 2021

Why not use the builtin scheduler API?

Admittedly, I'm having similar issues with the builtin API, will investigate it later.

@viniciuschiele
Copy link
Owner

You cannot pass the scheduler object as an argument for your job because it is not serializable. Btw why are you doing that?

@RPJack
Copy link
Author

RPJack commented Mar 9, 2021

Because i want the scheduler will create job automatic .
I'm so sorry for the late reply,i'm worked on another project few days ago.
I got some message from other website,it will tell me how a job i should create,but won't notify me.
I must refresh the website and create job in the refresh_job function with timing.

And now,i modifyed some code in flask-apscheduler/utils.py 33 row,it worked:

image
Even it's not a good solution,but i can't solve it by other way.

You cannot pass the scheduler object as an argument for your job because it is not serializable. Btw why are you doing that?

@RPJack
Copy link
Author

RPJack commented Mar 9, 2021

Btw,i have another question:
@scheduler.authenticate only worked on /scheduler/... endpoint
it can't worked in my owner endpoint
image
Should i use another moudle like flask-login?

@zexpe
Copy link

zexpe commented Mar 27, 2022

I've started using flask-apscheduler for a project and have encountered the same problem. My jobs have various objects passed to them that are not JSON serialisable and cannot be easily made so. The jobs all run without any problems. However, I cannot see what jobs are currently scheduled from the API due to this error occurring. I have no interest in seeing the argument values passed through to the jobs from the API - only which ids are currently scheduled, and when they are scheduled to run. Is it possible to have an option to display a less verbose set of details on the jobs from the API to avoid the need to JSON serialise the arguments?

@qfgrubby
Copy link

I can access http://ipaddr:port/scheduler, but cannot access http://ipaddr:port/scheduler/jobs
It shows "TypeError: Object of type AppDeploy is not JSON serializable" error.
How to solve this?

@qfgrubby
Copy link

I can access http://ipaddr:port/scheduler, but cannot access http://ipaddr:port/scheduler/jobs It shows "TypeError: Object of type AppDeploy is not JSON serializable" error. How to solve this?

I found the solution myself. It was caused by JSON format. File was json.py that existed in flask_apscheduler package. Commented this return " #return super(JSONEncoder, self).default(obj)". It's ok now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants