Commit graph

146 commits

Author SHA1 Message Date
David
d87b786687
chore: fix all invocation of init for singledispatch -> min 1 positional arg 2024-09-13 11:53:52 +02:00
Gavin D'souza
aee278b6ae
fix: Pass string method path to q.enqueue_call
Passing a method results in rq doing some internal gymnastics to
generate this string. In our case, this function never changes so we can
get rid of those steps
2024-07-12 20:10:30 +02:00
Ankush Menat
148b3f771d
build: bump RQ to latest (#26576)
* build: Bump RQ

Latest version simplifies workerpool extension a bit

* fix: Disable RQ's scheduler

It's now enabled by default with no easy way to disable it except
upstream change or overriding the run_worker method. So better to
disable it with custom worker class.
2024-05-27 13:54:34 +00:00
Ankush Menat
d13178755b refactor: keep scheduler+worker behaviour to workerpool
- Single worker adds some extra costs to each worker (importing scheduler)
- Workerpool can import it all once and share
- This way we need not have many different
2024-05-27 12:29:41 +05:30
Ankush Menat
59bbbd7b56 perf: optimize scheduling
- Randomize scheduling order
- Cache dormant status for 1 hour (this gets checked every second, too much work)
2024-05-27 12:21:11 +05:30
Ankush Menat
433e7281f7 feat: make running scheduler with worker optional 2024-05-27 12:14:15 +05:30
Ankush Menat
f7ff829ea7 perf: Merge worker and scheduler 2024-05-26 12:44:00 +05:30
Ankush Menat
0692496fd3
fix: log unhandled exceptions in monitor log (#25897) 2024-04-10 08:04:32 +00:00
Akhil Narang
8658196650
fix(enqueue): pass the original method argument here
Don't pass the stringified version - this is what goes to RQ, and the string we construct isn't always "correct"

For example, 87d121f47a/frappe/email/doctype/email_queue/email_queue.py (L735-L736) generates `frappe.email.doctype.email_queue.email_queue.QueueBuilder.send_emails` which will result in `ModuleNotFoundError: No module named 'frappe.email.doctype.email_queue.email_queue.QueueBuilder'; 'frappe.email.doctype.email_queue.email_queue' is not a package`

Signed-off-by: Akhil Narang <me@akhilnarang.dev>
2024-03-29 14:05:33 +05:30
Akhil Narang
bd6eaab00e
feat: drop sentry's RQ integration
It overrides what we set with information that isn't really useful for us.
Set a more readable method name, and add in some additional job metadata.

Signed-off-by: Akhil Narang <me@akhilnarang.dev>
2024-03-22 18:51:38 +05:30
Ankush Menat
0d6ec13b0b perf: preload modules in worker pool 2024-03-15 11:23:04 +05:30
Ankush Menat
322671766d perf: avoid importing posthog if not required
Avoids 7MB of overhead and cleanup costs for each background job 🎉
2024-03-15 10:51:17 +05:30
Ankush Menat
f7bff58935
fix: use name for RQ worker instead of PID (#25175)
Also avoid complex naming schemes.
2024-02-29 14:38:48 +00:00
Akhil Narang
16489b342b
Merge pull request #25125 from akhilnarang/bg-job-docstring
refactor: update docstring and typehints for functions
2024-02-28 11:30:43 +05:30
Akhil Narang
1ca98566d4
fix(background_jobs): update type hint and docstring
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
2024-02-27 12:20:13 +05:30
Ankush Menat
a19fd6a8b9
fix: Don't init site if already init-ed during truncate (#25033) 2024-02-23 11:37:38 +00:00
Akhil Narang
bf12be6928
fix: 755 -> 644
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
2024-02-23 16:54:10 +05:30
Akhil Narang
3f1e19de85
refactor(treewide): enable RUF rules
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
2024-02-21 16:20:28 +05:30
Akhil Narang
26ae0f3460
fix: ruff fixes
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
2024-02-07 17:04:31 +05:30
Ankush Menat
de9ac89748 style: re-format with ruff 2024-02-05 18:53:33 +05:30
mergify[bot]
483fcf89c3
Merge pull request #24428 from blaggacao/chore/cleanup-frappe-connect
chore: cleanup frappe connect (I/II)
2024-01-18 12:15:42 +00:00
Ankush Menat
f36a753c5a
refactor: Start sentry before freezing gc and use in pool (#24436)
- Workerpool wasn't using sentry
- Sentry was started after freezing GC which causes sentry imports to
  not be shared. Freeze GC after most memory intensive work is done.
2024-01-18 05:34:34 +00:00
David Arnold
d87092936f
chore: simplify frappe.connect & require explicit site initialization 2024-01-17 16:53:30 +01:00
Ankush Menat
b3eaec4347
fix: misc (#24303)
* fix(DX): improve logging in various places

* fix(ux): misc RQ worker and job fixes
2024-01-13 10:54:38 +05:30
Akhil Narang
35ea093b51
feat: support background jobs in recorder
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
2024-01-04 15:51:26 +05:30
Ankush Menat
00f20f43c6
build: add setproctitle as dependency (#24007)
* build: add setproctitle as dependency

RQ and other tools use it to automatically set a useful proc title

* ci: print all bench logs after running tests

This can help reveal failures from background jobs etc
2023-12-28 20:50:27 +05:30
Akhil Narang
067104ca9c
refactor(sentry): sync up with FC implementation
Co-authored-by: Aditya Hase <aditya@adityahase.com>
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
2023-12-26 12:30:10 +05:30
Hussain Nagaria
8d2137c265 docs: consistent doc strings 2023-12-18 18:27:39 +05:30
Ankush Menat
089985e53b feat: frappe.job local for reading job info 2023-11-04 14:06:31 +05:30
Corentin Flr
f88df82820
chore(rq): Pass rq.Callback objects to enqueue_call (#22271)
> DeprecationWarning: Passing a string or function for `on_failure` is deprecated, pass `Callback` instead
2023-09-02 11:22:33 +05:30
Ankush Menat
56b409d069
fix: limit job count in RQ failed registry (#22162)
* fix: limit job count in RQ failed registry

* chore: remove unnecessary test

This just checks if func is called with right values, which keep
changing as things evolve.
Everything is individually tested now so need for this test.
2023-08-23 09:21:51 +00:00
Ankush Menat
ef51dde052
perf: set lower priority for background processes (#21841) 2023-07-28 13:01:46 +05:30
Ankush Menat
adf30693a9 ci: update pyupgrade 2023-07-14 14:24:08 +05:30
Ankush Menat
d57c552e26 feat: frappe.enqueue with deduplication
use deduplicate=True and set job_id for automatic and mostly sane job deduplication.
2023-06-29 18:05:07 +05:30
Ankush Menat
1668ba7d9e feat: Namespace all RQ jobs to site 2023-06-29 16:35:10 +05:30
Ankush Menat
95e49193c8
fix: dont retry if redis not available for realtime (#21517)
* fix: reduce retries for rq connection

10 seconds of retries when connection isn't available is too much, just
   failing might be beneficial.

- BusyLoadingError only occurs when redis is restarting
- ConnectionError mostly means redis is dead, no amount of retries will
  bring it back.

* fix: dont retry if redis is down for realtime
2023-06-28 15:21:04 +05:30
Ankush Menat
73bca16d77 feat: RQ WorkerPool
RQ now has experimental support for workerpools.

When to use this?

Roughly when you have more than 2 workers a workerpool might make
sense, below 2 it's overhead as master "pool" process will need to run
to manager workerpool itself.

Why is it any better?

Currently we just let supervisor duplicate the worker process N number
of times. This is inefficient from shared memory POV. Forking the
original process to create workers enables sharing of more memory thus
leading upwards of 60-70% reduction in memory usage with pool size of 8
workers.
2023-06-26 18:10:00 +05:30
Ankush Menat
7fbc6e8175 refactor: Simplify dequeue_strategy selection
Classes arent required anymore, it can just be a parm to worker class
isntead.
2023-06-26 17:42:18 +05:30
Ankush Menat
ca95b591ae refactor: Pass redis connection directly 2023-06-26 17:36:53 +05:30
Ankush Menat
150c36c74d fix: collect before freezing 2023-06-24 17:36:10 +05:30
Ankush Menat
29d28a460f perf: Freeze GC right before starting background worker
BG worker forks are not CoW friendly. Freezing right before we start
worker should lessen overall memory usage. Though this isn't useful much
because at max you're sharing with 2 processes - master and horse.

WorkerPool can improve this benefit a lot by forking each worker from
master process and horse from forked processes. TBD when WorkerPool is
out of beta.
2023-06-24 17:06:23 +05:30
Ankush Menat
eb17c12dda fix: reuse redis connection if not rq auth 2023-06-11 11:40:22 +05:30
Ankush Menat
07e1d34568 refactor: RQ enqueue after commit and tests 2023-06-03 18:44:19 +05:30
Ankush Menat
6519651002 refactor: change implementation of enqueue_after_commit
if enqueue_after_commit then pass partial function after commit instead
of storing it in flags. SLIGHTLY less efficient, but uses consistent API.
2023-06-03 18:44:19 +05:30
Ankush Menat
ef6f4b5956
refactor!: Use job_id for bg job deduplication (#20951) 2023-05-10 12:45:05 +05:30
Ankush Menat
ecce4ba5a7 test: deduplication with unique job id 2023-05-09 14:23:07 +05:30
Ankush Menat
bc3871b657 refactor: standardize assigning job_id 2023-05-09 14:23:04 +05:30
Ankush Menat
4bd02a4ed1 perf: Faster scheduled job deduplication
O(n) to O(1) dedup by specifying unique IDs on scheduled RQ jobs.
2023-05-08 18:28:09 +05:30
Sagar Vora
8418d3c7e2 fix: only call RQ synchronously as fallback during migration 2023-05-02 13:46:50 +05:30
Bernhard Sirlinger
c5cfe8f5aa
feat(minor): log datetime in worker log (#20414) 2023-03-21 12:55:03 +05:30