viniciuschiele / flask-apscheduler

Adds APScheduler support to Flask
Apache License 2.0
1.12k stars 185 forks source link

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

Open RPJack opened 3 years ago

RPJack commented 3 years ago

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 commented 3 years ago

Why not use the builtin scheduler API?

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

viniciuschiele commented 3 years ago

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

RPJack commented 3 years ago

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 commented 3 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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.