根据不同的用户时区动态设置 cron 作业
Set cron job dynamically base from different user timezone
在 laravel 7 中,我设置了 运行 基于不同用户特定时区的 cronjobs。
这是确切的代码:
foreach (User::role('admin')->get() as $user) {
/* with queue job */
$schedule->command('weekly-survey:send')->timezone($user->timezone)->weekly()->wednesdays()->at('8:00');
$schedule->command('update:surveys-completed')->timezone($user->timezone)->daily();
$schedule->command('trial:reminder')->timezone($user->timezone)->dailyAt('7:45');
$schedule->command('trial:ends')->timezone($user->timezone)->dailyAt('23:00');
$schedule->command('subscribe:customer')->timezone($user->timezone)->dailyAt('23:45');
$schedule->command('update:user-invoice')->timezone($user->timezone)->everyMinute();
$schedule->command('employees:update-status')->timezone($user->timezone)->everyMinute();
$schedule->command('subscription:update-quantity')->timezone($user->timezone)->daily();
$schedule->command('update:freeze-account')->timezone($user->timezone)->dailyAt('22:45');
$schedule->command('send:upcoming-survey-notification')->timezone($user->timezone)->weeklyOn(1, '8:00');
$schedule->command('surveys:end-soon')->timezone($user->timezone)->dailyAt('8:00');
/* end with queue job */
/* without queue */
$schedule->command('amazon:get-send-qouta')->timezone($user->timezone)->dailyAt('23:55');
$schedule->command('amazon:get-statistics')->timezone($user->timezone)->dailyAt('23:55');
$schedule->command('update:customer-success-table')->timezone($user->timezone)->everyMinute();
$schedule->command('csm:prev-month-active')->timezone($user->timezone)->lastDayOfMonth('23:59');
$schedule->command('update:monthly-earning')->timezone($user->timezone)->lastDayOfMonth('23:59');
$schedule->command('update:subscription-status')->timezone($user->timezone)->everyMinute();
$schedule->command('retrieve:past-due-subscription')->timezone($user->timezone)->dailyAt('23:59');
$user = new User();
$user->accountNotificationsSchedule($schedule);
$schedule->command('horizon:snapshot')->timezone($user->timezone)->everyFiveMinutes();
/* end without queue */
\Log::info("Cron entry successfully executed!");
}
此代码对 运行 安全吗?首先,我遍历所有具有管理员角色的用户,然后将用户时区传递给每个命令。我敢肯定,只要服务器还活着,这段代码就会无限 运行 。我只是想有一个替代的安全方法。
这是我实现的并且有效。
public function handle(TimezoneRepository $timezoneRepository)
{
// get user's unique timezone
$timezones = $timezoneRepository->getUniqueTimezones();
foreach ($timezones as $timezone) {
$date = Carbon::now($timezone);
// send only on Wednesday at 8:00 am
if ($date->isWednesday() && $date->hour == 8 && $date->minute == 0) {
Survey::SendSurvey();
}
}
}
在 laravel 7 中,我设置了 运行 基于不同用户特定时区的 cronjobs。 这是确切的代码:
foreach (User::role('admin')->get() as $user) {
/* with queue job */
$schedule->command('weekly-survey:send')->timezone($user->timezone)->weekly()->wednesdays()->at('8:00');
$schedule->command('update:surveys-completed')->timezone($user->timezone)->daily();
$schedule->command('trial:reminder')->timezone($user->timezone)->dailyAt('7:45');
$schedule->command('trial:ends')->timezone($user->timezone)->dailyAt('23:00');
$schedule->command('subscribe:customer')->timezone($user->timezone)->dailyAt('23:45');
$schedule->command('update:user-invoice')->timezone($user->timezone)->everyMinute();
$schedule->command('employees:update-status')->timezone($user->timezone)->everyMinute();
$schedule->command('subscription:update-quantity')->timezone($user->timezone)->daily();
$schedule->command('update:freeze-account')->timezone($user->timezone)->dailyAt('22:45');
$schedule->command('send:upcoming-survey-notification')->timezone($user->timezone)->weeklyOn(1, '8:00');
$schedule->command('surveys:end-soon')->timezone($user->timezone)->dailyAt('8:00');
/* end with queue job */
/* without queue */
$schedule->command('amazon:get-send-qouta')->timezone($user->timezone)->dailyAt('23:55');
$schedule->command('amazon:get-statistics')->timezone($user->timezone)->dailyAt('23:55');
$schedule->command('update:customer-success-table')->timezone($user->timezone)->everyMinute();
$schedule->command('csm:prev-month-active')->timezone($user->timezone)->lastDayOfMonth('23:59');
$schedule->command('update:monthly-earning')->timezone($user->timezone)->lastDayOfMonth('23:59');
$schedule->command('update:subscription-status')->timezone($user->timezone)->everyMinute();
$schedule->command('retrieve:past-due-subscription')->timezone($user->timezone)->dailyAt('23:59');
$user = new User();
$user->accountNotificationsSchedule($schedule);
$schedule->command('horizon:snapshot')->timezone($user->timezone)->everyFiveMinutes();
/* end without queue */
\Log::info("Cron entry successfully executed!");
}
此代码对 运行 安全吗?首先,我遍历所有具有管理员角色的用户,然后将用户时区传递给每个命令。我敢肯定,只要服务器还活着,这段代码就会无限 运行 。我只是想有一个替代的安全方法。
这是我实现的并且有效。
public function handle(TimezoneRepository $timezoneRepository)
{
// get user's unique timezone
$timezones = $timezoneRepository->getUniqueTimezones();
foreach ($timezones as $timezone) {
$date = Carbon::now($timezone);
// send only on Wednesday at 8:00 am
if ($date->isWednesday() && $date->hour == 8 && $date->minute == 0) {
Survey::SendSurvey();
}
}
}