How can I set unique recurring intervals for sidekiq-scheduler? - redis

I am surprised there isn't more documentation on this, or maybe I am looking in the wrong place. I have set up sidekiq and sidekiq-scheduler in my rails app to handle recurring scheduled tasks. It works like a charm for basic jobs like documented:
Sidekiq.set_schedule('heartbeat', { 'every' => ['1m'], 'class' => 'HeartbeatWorker' })
However, I require more unique and dynamic schedules. Something along the lines of:
Sidekiq.set_schedule('heartbeat', { 'every' => ['1month'], 'class' => 'HeartbeatWorker' })
or even better:
Sidekiq.set_schedule('heartbeat', { 'every' => ['first_of_month'], 'class' => 'HeartbeatWorker' })
Is there a way to handle this complexity with my current setup or do I require another gem?
This is my first time working with sidekiq and cron.

Related

FluentValidation testing with RuleSets

Im writing some test for a FluentValidator that contains RuleSets like this:
RuleSet("Patch", () =>
{
RuleFor(x => x.Name)
.NotEmpty();
});
and I test this with FluentValidation.TestHelper, the test look like this:
_sut.ShouldHaveValidationErrorFor(x => x.Name, string.Empty);
The problem is that the ShouldHaveValidationErrorFor dont go in the RuleSet therefore the NotEmpty check dont fires.
How do I specify the test to use the the RuleSet "Patch"?
Thanks!

Yii The view file does not exist - where do mail layouts go

I am using basic template but have adapted the advanced user password reset functionality, for some reason I can't get it to find the mail layouts.
So in \mail\layouts I have
- passwordResetToken-html.php
- passwordResteToken-text.php
In web.php I have
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => 'app\mail\layouts',
...
The advanced template uses
'viewPath' => '#common/mail',
But as i'm using basic template, its not in the common/mail folder.
In sendMail function in PasswordResetRequestForm.php i have
return \Yii::$app->mailer->compose(['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], ['user' => $user])
...
However getting error
The view file does not exist: app\mail\layouts\passwordResetToken-html.php
I know this is going to be something small but for the life of me i cannot see it
Removed the 'viewPath' of config, as the doc don't use it.
So it works.

Routing not working in phalcon framework

I have many modules and depending upon modules routes are generated dynamically. For explaining purpose I m adding route for just one module. Following are the routes for the users module.
<?php
//1
$router->add('/users',array(
'module' =>'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action' => 'index'
));
//2
$router->add('/users/:params/',array(
'module' => 'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action' => 'index',
'params'=>1
));
//3
$router->add('/users/:action',array(
'module' => 'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action'=>1
));
//4
$router->add('/users/:action/:params',array(
'module' => 'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action'=>1,
'params'=>2
));
?>
Lets say the url for the user module is
http://www.example.com/admin/users/
This url matches the very first route and its working as expected. But when we navigate to next page, my url looks like
http://www.example.com/admin/users/2
now the problem is it should match 2nd route, but it matches 4th route. If I move 2nd route all the way down, the above url work, but the url
http://www.example.com/admin/users/search/1 will not work
Can anybody help me make it work?
Thanks
:params is tricky because it will match anything. So /users/:params/ matches both /users/:action and /users/:action/:params, which itself has :params in it - making it a routing mind blow.
As a general rule avoid the mind blow scenario. For example you can put :params at the end of the longest possible match (/users/:action/:params) and then rewrite the shorter routes without any :params in them.

Yii form builder

I've been working on Yii-based application. And I've faced with weird thing...
What I'm trying to do is to add input(type=file) into form. Form is created via form builder(CForm class). But input is not going to appear.
My code. Controller/action:
$model=MyModel::model()->findByPk(700);
$model->scenario='my-scenario';
$form=new CForm('path.to.forms.my-form', $model);
$this->render('view', array('form'=>$form));
View:
echo $form
Form config:
return array(
'attributes' => array(
'enctype' => 'multipart/form-data',
),
'elements' => array(
'name' => array(
'type' => 'text',
),
'image' => array(
'type' => 'file',
),
),
'buttons' => array(
'save' => array(
'type' => 'submit',
'label' => 'Save',
),
),
);
Model:
//.....
public $image;
// ....
public function rules()
{
return array(
//...
array('image', 'file', 'types'=>'png', 'on'=>'my-scenario'),
);
}
With code above I expected to see two fields - text and file. But only text one appears.
If I change file validator to, say, required - it works, but I need file validator.
I'm using Yii version 1.1.13.
The most intresting that code above works as expected with earlier Yii(1.1.9). Is this a known bug in new version? If yes - is there a solution? or do I have to rollback to previous version?
Thanks in advance.
UPDATE:
If you add a second validator (one for file and one for required) does it work?
No, it doesn't. I believe I found why. See bellow.
It seems to be caused by this line in CForm..
Yes, correct. Yesterday, armed with debugger I went deeper:)
CFormElement::getVisible() eventually calls CModel::isAttributeSafe() and CModel::getSafeAttributeNames().
CForm::getSafeAttributeNames() gets all model validators and leaves only safe ones. As we can see CFileValidator is not safe.
So, it doesn't matter how many safe validators(required or any other) have attribute assigned. CForm::getSafeAttributeNames() removes it from whitelist if there is at least one unsafe(file).
File validator is unsafe since 1.1.12 version. That is why it worked perfectly for me in 1.1.9 :)
Hence the problem is in CFileValidator(or at least connected with it) and not in CForm.
The only one solution I can see so far is creating own validator extended from CFileValidator marked safe and using it instead of built in. But I can't even imagine what problems it may cause(I believe Yii developers had a good reason for making it unsafe).
I hope this will be helpful for somebody.
UPDATE 2
array('image', 'file', 'safe'=>true, 'types'=>'png', 'on'=>'my-scenario')
this validation rule(explicit safe=true) also works.
If you add a second validator (one for file and one for required) does it work? I ran into this recently myself.
It seems to be caused by this line in CForm:
if($element->getVisible())
{
...
}
The getVisible checks the active validators for this current model scenario and leaves out any inputs that aren't used in the current validator rules. I've ended up commenting things out in our custom CForm model for our system, but if let me know if you find something that works better for you.
Issue has been posted on github
https://github.com/yiisoft/yii/issues/2089

Zend Framework - how to rewrite url to seo friendly url

I got website on Zend Framework (im total noob in Zend). For example I would like to make one URL "somewebsite.com/test/about" to look like this "somewebsite.com/for-fun-link". How do i achieve this in Zend ? Im newbie in Zend and Apache server. Where i do rewrites in Zend ? I want static URL somewebsite.com/page/about rewrite to somewebsite.com/about-product
And last question: where do i usually create rewrites ? its depends of sever/technology ?
In your bootstrap, you'll need to configure some "routes". So, if your bootstrap ends something like this:
$frontController = Zend_Controller_Front::getInstance();
$frontController->dispatch();
you can just add in some route definitions like this:
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute( 'mylovelyroute',
new Zend_Controller_Router_Route_Static( 'for-fun-link',
array( 'controller' => 'test', 'action' => 'about' )
)
);
$router->addRoute( 'myotherroute',
new Zend_Controller_Router_Route_Static( 'about-product',
array( 'controller' => 'page', 'action' => 'about' )
)
);
$router->addRoute( 'justonemore',
new Zend_Controller_Router_Route_Static( 'another/longer/path',
array( 'controller' => 'mycontroller',
'action' => 'myaction',
'someparameter' => 'foo'
)
)
);
$frontController->dispatch();
The first parameter of addRoute() is just a unique name. Zend_Controller_Router_Route_Static takes the path you'd like to capture, and then an array of parameters, including a controller and action (and module if it applies).
If you wanted to add complexity, you could load the routes out of a database, or start using more dynamic routes. The docs here are a useful next step: http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard