Chrome Extension MV3 – How to run job scheduler to run the task periodically to sync with backend system.

339 views
Skip to first unread message

iMan addon POC

unread,
Sep 28, 2023, 8:31:24 AM9/28/23
to Chromium Extensions

Hi Chrome Extension Team, 

 

Scenario: 

We have chrome extension built using MV2 and runs job every 10 minutes in background script which will invoke HTTP based REST API end points and it works. 

 

Issue: 

We are redesigning the extension using MV3 and we tried to run job every 10 minutes in service worker but service worker gets terminated after 30 seconds (As per service worker working policy). 

 

Workaround Tried: 

Used Chrome Alarm API to execute the code for every 1 minute to keep the service worker alive so that the job runs for every 1 minute to invoke HTTP based REST API end point. It will work for some time and service worker gets terminated randomly like after 2 / 3 / 4 / 6 hours. 

 

Hence service worker in MV3 is very inconsistent and not reliable to run job for given specific interval of time. 

 

Need Clarification:

  1. Could you please suggest any workaround to run job periodically to invoke HTTP based REST API end point in MV3. 
  2. Does google had any plan to make MV3 service worker run persistently similar to background script in MV2. 

 

Thanks in Advance, 

Shashikiran 

Patrick Kettner

unread,
Sep 28, 2023, 8:45:29 AM9/28/23
to iMan addon POC, Chromium Extensions
Im sorry to hear you are having problems. May I ask why are you running a command every minute to do something that you need to do every 10 minutes? chrome.alarm will wake it up every N minutes (assuming the device is online and chrome is open), and an active network connection (i.e. something being backed up to a remote server) keeps the service worker running until completed.

> Could you please suggest any workaround to run job periodically to invoke HTTP based REST API end point in MV3.
chrome.alarms is the correct way to be doing this. If you are seeing specific situations where it is failing, you should file a bug at crbug.com

> Does google had any plan to make MV3 service worker run persistently similar to background script in MV2.
No.


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/385235f1-58af-4e5b-81da-dc9c46f5da6dn%40chromium.org.

Pavel Exarkhopoulo

unread,
Sep 28, 2023, 12:32:54 PM9/28/23
to Patrick Kettner, Chromium Extensions
Patrick Kettner>> Does google had any plan to make MV3 service worker run persistently similar to background script in MV2.
Patrick Kettner> No.

Is there by any chance a publicly available design document that explains the rationale behind this irrevocable decision?

When I google, I mostly get (ignoring complaints and frustration) either refrains of how to work around it (like https://stackoverflow.com/questions/66618136/persistent-service-worker-in-chrome-extension) or restatements of MV3 service worker behavior and its seemingly arbitrary limits (https://developer.chrome.com/docs/extensions/mv3/service_workers/service-worker-lifecycle/#idle-shutdown). It would be helpful to finally understand if there are any deep technical reasons behind both the decision and particular limits chosen.


iMan addon POC

unread,
Sep 29, 2023, 3:10:12 AM9/29/23
to Chromium Extensions, Pavel Exarkhopoulo, Chromium Extensions, Patrick Kettner
@ Patrick Kettner>> Thanks for reply.  
I tried chrome.alarms API to keep the service worker alive but service worker gets terminated abruptly.
FYI that similar experience other extension developers reported in chromium bug portal already, please refer the link https://bugs.chromium.org/p/chromium/issues/detail?id=1152255

NEED CLARIFICATION:
I wonder if there is any other way to run the job periodically in MV3 framework (other than relying on service worker).

@Pavel Exarkhopoulo >> Thanks for reply. Even I found the same stackoverflow link but none of the approach described is solving my problem in my context (Like invoking HTTP REST end point using fetch() API periodically without user interaction with extension).

Thanks in Advance,
Shashikiran 

wOxxOm

unread,
Sep 29, 2023, 6:56:46 AM9/29/23
to Chromium Extensions, iMan addon POC, Pavel Exarkhopoulo, Chromium Extensions, Patrick Kettner
> I found the same stackoverflow link

No, you either found a different one or you haven't used the main answer in that thread, which isn't about `fetch`.

> Is there by any chance a publicly available design document that explains the rationale behind this irrevocable decision?

There have been only completely unwarranted statements that it's better and that it moves the platform forward, which is a meaningless PR exaggeration in most cases and factually untrue for a lot of non-negligible cases reported over the past few years. Some of such cases we'll be eventually addressed, see https://crbug.com/1418780 for example. Some probably won't, e.g. there won't be anything close to the flexibility of webRequestBlocking, at least for the observable future.

Patrick Kettner

unread,
Sep 29, 2023, 8:25:12 AM9/29/23
to iMan addon POC, Chromium Extensions, Pavel Exarkhopoulo
Hi iman 
Reading the bug you shared, there hasn’t been a change to it since February, as the issue has been fixed for some time. If you are still hitting a problem, it would be a different issue, and you would want to open a new bug with a reproducible example.

> Could you please suggest any workaround to run job periodically to invoke HTTP based REST API end point in MV3.

chrome.alarms IS the way to do it. There isn’t another option for periodic jobs. You can force an extension to constantly stay awake (woxxom's answer on stackoverflow covers this extensively), but it should not be necessary for what you have described.
Reply all
Reply to author
Forward
0 new messages