Is it possible to run a scheduled task each midnight in Shopware? - cron

We have read https://developer.shopware.com/docs/guides/plugins/plugins/plugin-fundamentals/add-scheduled-task which described how to define a scheduled tasks which runs every x minutes.
Is it also possible to specific the execution time, for example each midnight or every day a 2 am, like in a crontab?

There is no such possibility in Shopware 6. It is easier to accomplish this with a CLI command and crontab. But if you have to use Shopware's scheduled task then you can trick it by setting the nextExecutionTime to the time you want to execute the task.
For example if today's date is 10.03.2022 and you want to execute the scheduled task every day at 2 am then set the nextExecutionTime to 2022-03-11 00:02:00.000 (use future date) and runInterval to 86400 (24h). This way Shopware will start the task at 2 am and then set the nextExecutionTime to the next day at 2 am (+- couple of minutes from my experience).

I'm not aware of such a feature in Shopware core.
The probably most straightforward way would be to add a "real" cronjob like you mentioned, which triggers a CLI-command.
You can encapsulate the logic of the task in it's own service, so that the scheduled task and cli-command both can just use the service (if you want to keep both).

Related

Prevent concurrent cron jobs in pg-boss

I’m considering pg-boss for running and distributing event-based jobs between the instances of the same service. One of my use cases, apart from event-based, is scheduled jobs. Some of them can take a while and continue running until it’s time to trigger the next invocation - e.g. a job is set to run every 5 minutes but it can take e.g. 8 to complete. In such case I need the system to realize that the previous run is still in progress and not trigger the same job while the previous invocation of it is still in progress, using the example of every 5 minutes and a job taking 8 minutes - I’d like sth like the following to happen:
13:00 job triggered
13:05 job still runs, system sees it and doesn’t trigger once more even though it’s time
13:08 job done
13:10 next job run triggered
Is there an elegant way to achieve it with pg-boss without implementing my own locking mechanism?

Netsuite Scheduled script can it be run every 3 minutes?

I see on the scheduled script deployment screen on Netsuite, the Repeat dropdown starts with a minimum of every 15 minutes. Can the interval be shortened somehow?
The interval of scheduled scripts cannot be shortened to less than 15 minutes.
What you can do is set up a Suitelet that triggers the scheduled script to start. Once you get the External URL of the Suitelet you can plug it into an external cronjob service such as https://cron-job.org and set it to run every 3 minutes.
Note that the scheduled script would be put on a queue either way so it may not start immediately after.
The answer would be No. I think they are trying to prevent choking the queue so they only allow 15mins.

Calling a specific method on a certain time

I am currently working on a web application and I want to schedule a method that runs every day at 6 AM. Basically, if the clock ticks at 6 AM, this method will run. I have seen some answers on the Internet but none of these are scheduled at a fixed time of the day but rather timed after the app is ran.
I also have an alternative solution which is to check every minute if the time is 6 AM, and if so, I'll run the code. But is there other better answer than this?
Thank you!
On the server side, you could do it with a cron job.
On Linux follow these steps:
crontab -e
Here's an example of how to make a request to the google.com at 6AM (UTC)
# Each day at 6th hour (6 AM depending on your time zone settings, or UTC)
0 6 * * * curl -I http://google.com
:wq # save changes and quit
:q # to quit
:q! # to quit without saving changes
See more about Cron here. Or use a visual crontab entry creator if that is helpful. If you need a solution for Windows machine, you could also use a Windows scheduler to trigger events similar to cron.
Cron job would be a good way of doing it. However, for some reason if you can not schedule a cron job and need your own scheduler and your environment is Java based then you could use the Quartz Scheduler. It is a java based job scheduler like cron. It's usage is pretty simple.

Linux task schedule to Hour, minute, second

I'm trying to run a shell script at a specific time up to it's seconds (H:M:S) , but so far all programs such as at only go up to a specific minute (not second).
I don't want to use sleep since it's not accurate. For some reason it ended couple of hours earlier than it was supposed to!
Your question doesn't seem to define accuracy, but there is always some jitter in scheduling in electronic devices. You might use quartz to schedule to the second. You could also use at or cron to schedule to the minute and then sleep the appropriate number of second(s).

Preventing cronjobs from overlapping

I have 3 different jobs set up in crontab (call them jobA, jobB, jobC) that run at different intervals and start at different times during the day. For example, jobA runs once per hour at 5 mins past the hour, jobB runs every 30 mins at 9 and 39 mins past the hour, and jobC runs every 15 mins. They are not dependent on each other, but for various reasons they can NOT be running at the same time.
The problem is that sometimes one of the jobs takes a long time to run and another one starts before the first one is done, causing issues.
Is there some way to queue or spool these jobs so that one will not start until the current running one has finished? I tried using this solution but this does not guarantee that the pending jobs will resume in the same order they were supposed to start. A queue would be best, but I cannot find anything about how to do this.
You can't do that using cron. Cron is used to run a specific command at specific time. You can do it by the solution you proposed, but that adds a lot more complexity.
I suggest, writing/coding the requirement in high level language like java and use a mutil-thread program to achieve what you need.
Control-m is another scheduling software, with a lot of other features as well. You would be able to integrate the above use-case in it.

Resources